I use phinx to set-up my databases in a new project. I have the folowing configuration for one of my tables that I use.
use Phinx\Migration\AbstractMigration;
class NewsV1 extends AbstractMigration
{
public function change()
{
$this->table( 'news', [ 'id' => false, 'primary_key' => [ 'id' ]])
->addColumn( 'id', 'integer', [ 'length' => 10, 'signed' => false, 'identity' => true ])
->addColumn( 'thumbnail_id', 'integer', [ 'length' => 10, 'signed' => false, 'null' => true ])
->addColumn( 'title', 'string', [ 'length' => 64 ])
->addColumn( 'excerpt', 'string', [ 'length' => 255 ])
->addColumn( 'content', 'text' )
->addTimestamps()
->create();
}
}
The function addTimestamps() creates a created_at and an updated_at column in the table.
The created_at column is created as a timestamp with on create 'CURRENT_TIMESTAMP' and that is working as expected. As soon as you create a new record the created_at is set to the current timestamp.
The updated_at column is created without an update set to CURRENT_TIMESTAMP. So I changed the table configuration so that the updated_at now upadtes the column when the row is updated.
If I change the row in navicat (my tool to manage a database) the updated_at is changed. But when I issue a save or update in the controller, the news model is used in, the field does not get updated.
ofcourse I can force it in the model using somthing like this, but why should I have to when the database is configured to set the updated field.
// todo: this should not be needed as the database has an update
protected function beforeValidationOnUpdate()
{
$this->updated_at =date( "Y-m-d H:i:s", time( ));
}
Am I doing something wrong or ?
Any help is appriciated