05-07-2020, 08:16 PM
I have a question about the protected "function" in Model.
I was watching a sequence of videos on YouTube about the "Create news items" tutorial and decided to follow the step-by-step instructions. At a certain point I was unable to proceed, just at the moment when I would update the table with the option $useTimestamps.
The first thing I noticed is that in the videos the version used was "beta 4". I made several attempts but couldn't, so I decided to go to the basics: list a field from the table:
foreach ($news as $row);
echo $row['title'];
I did not make it!
See attached image with the description of the error.
Only after removing this field with "false" and the others adding a comment sign did I get it.
According to the manual:
"This boolean value determines whether the current date is automatically added to all inserts and updates. If true, will set the current time in the format specified by $ dateFormat. This requires that the table have columns named 'created_at' and 'updated_at' in the appropriate data type. "
Model:
Controller:
In the video, https://www.youtube.com/watch?v=FVCXhqWF4CM, despite some confusion with coding, at 15:00, everything works perfectly.
In the attached image of the table, the only record was created manually.
I was watching a sequence of videos on YouTube about the "Create news items" tutorial and decided to follow the step-by-step instructions. At a certain point I was unable to proceed, just at the moment when I would update the table with the option $useTimestamps.
The first thing I noticed is that in the videos the version used was "beta 4". I made several attempts but couldn't, so I decided to go to the basics: list a field from the table:
foreach ($news as $row);
echo $row['title'];
I did not make it!
See attached image with the description of the error.
Only after removing this field with "false" and the others adding a comment sign did I get it.
According to the manual:
"This boolean value determines whether the current date is automatically added to all inserts and updates. If true, will set the current time in the format specified by $ dateFormat. This requires that the table have columns named 'created_at' and 'updated_at' in the appropriate data type. "
Model:
PHP Code:
<?php namespace App\Models;
use CodeIgniter\Model;
class NewsModel extends Model
{
protected $table = 'news';
protected $primaryKey = 'id';
protected $returnType = 'array';
protected $allowedFields = ['id', 'title', 'body'];
protected $useSoftDeletes = false;
protected $useTimestamps = false;
//protected $createdField = 'created_at';
//protected $updatedField = 'updated_at';
//protected $deletedField = 'deleted_at';
public function getNews($id = null) {
if ($id === null)
{
return $this->findAll();
}
return $this->asArray()
->where(['id' => $id])
->first();
}
}
Controller:
PHP Code:
<?php namespace App\Controllers;
// 20200507
use CodeIgniter\Controller;
use App\Models\NewsModel;
class News extends Controller {
public function index() {
$model = new NewsModel();
$data = [
'news' => $model->getNews()
];
$header = array (
'icon' => 'codeigniter',
'css' => 'test',
'title' => 'Teste - wdeda',
'action' => '/ci4/search/allmedia/',
'placeholder' => 'Search'
);
echo view('templates/header', $header);
echo view('content/teste/teste', $data);
echo view('templates/footer');
}
}
In the video, https://www.youtube.com/watch?v=FVCXhqWF4CM, despite some confusion with coding, at 15:00, everything works perfectly.
In the attached image of the table, the only record was created manually.