1
The only question here is to save the name I catch with var $newname. I can get the name already modified and even print on the screen, but when I step on set() to save in DB nothing happens. The problem itself is all here:
$news_model->set('img_name', $newName);
Here’s all the code:
<?php namespace App\Controllers;
class Publicar extends BaseController
{
public function index()
{
$news_model = new \App\Models\NewsModel();
$data['news'] = $news_model->find();
$image_news = new \App\Models\ImageModel();
$data['image_news'] = $image_news->find();
$data['title'] = 'news social - Add Notícia';
$data['msg'] = '';
$data['class_error'] = '';
$data['erros'] = '';
date_default_timezone_set('America/Sao_Paulo');
$data_create = date('Y-m-d H:i');
if($this->request->getMethod() == 'post'){
$news_model = new \App\Models\NewsModel();
$news_model->set('title', $this->request->getPost('title_news'));
$news_model->set('type_news', $this->request->getPost('type_news'));
$news_model->set('description', $this->request->getPost('description'));
$news_model->set('date_posted', $data_create);
if ($news_model->insert()) {
// Upload images
if($file = $this->request->getFile('img_name'))
{
$newName = $file->getRandomName();
$file->move('upload', $newName);
// Save name
$news_model->set('img_name', $newName);
}
$data['erros'] = ['Notícia adicionada com sucesso. Obrigado fulano' . $newName];
$data['class_error'] = 'success';
} else {
$data['erros'] = $news_model->errors();
$data['class_error'] = 'danger';
}
}
echo view('publicar_form', $data);
}
}
Plus, I thought it was an argument problem, but here I can save the good date using a var:
$news_model->set('date_posted', $data_create);