cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

[BUG]: Timestampable behavior does not work

Open ekmst opened this issue 7 years ago • 4 comments

Describe the bug Timestampable behavior does not work. The field created_at is not filled when creating or saving a record and methods create(), save() return false

To Reproduce Steps to reproduce the behavior:

class Test extends Model
{
    public $id;
    public $data;
    public $created_at;

    public function initialize()
    {
        $this->addBehavior(new Timestampable(
            [
                'beforeCreate' => [  // and onCreate
                    'field' => 'created_at',
                ],
            ]
        ));
    }
}

Details

  • Phalcon version: 4.0.0-rc.1
    • Build Date: Sep 29 2019 18:59:05
    • Powered by Zephir: Version 0.12.4-b386980
  • PHP Version: 7.3.9
  • Operating System: linux
  • Installation type: installing via package manager
  • Other related info (Database, table schema): mysql 8.0.17
Directive => Local Value => Master Value
phalcon.db.escape_identifiers => On => On
phalcon.db.force_casting => Off => Off
phalcon.orm.case_insensitive_column_map => Off => Off
phalcon.orm.cast_last_insert_id_to_int => Off => Off
phalcon.orm.cast_on_hydrate => Off => Off
phalcon.orm.column_renaming => On => On
phalcon.orm.disable_assign_setters => Off => Off
phalcon.orm.enable_implicit_joins => On => On
phalcon.orm.enable_literals => On => On
phalcon.orm.events => On => On
phalcon.orm.exception_on_failed_save => Off => Off
phalcon.orm.exception_on_failed_metadata_save => On => On
phalcon.orm.ignore_unknown_columns => Off => Off
phalcon.orm.late_state_binding => Off => Off
phalcon.orm.not_null_validations => On => On
phalcon.orm.update_snapshot_on_save => On => On
phalcon.orm.virtual_foreign_keys => On => On

ekmst avatar Sep 30 '19 21:09 ekmst

Try to add 'format' => 'Y-m-d H:i:s',

'beforeCreate' => [  // and onCreate
    'field' => 'created_at',
    'format' => 'Y-m-d H:i:s',
],

Jeckerson avatar Sep 30 '19 21:09 Jeckerson

@Jeckerson It does not help. The format option is optional and everything should have worked without it

ekmst avatar Sep 30 '19 21:09 ekmst

@ekmst, It's not a bug.

Try to change 'beforeCreate' to 'beforeValidationOnCreate' I know that it resolve your problem.

class Test extends Model
{
    public $id;
    public $data;
    public $created_at;

    public function initialize()
    {
        $this->addBehavior(new Timestampable(
            [
                'beforeValidationOnCreate' => [  // and onCreate
                    'field' => 'created_at',
                    'format' => 'Y-m-d H:i:s'
                ],
            ]
        ));
    }
}

Don't forget about validation please. If the create or update operations return false You can get error messages by calling the "getMessages()" method.

TimurFlush avatar Oct 01 '19 00:10 TimurFlush

@TimurFlush Yes, I am aware of getMessages(), but in my case, he did not return anything.

But beforeValidationOnCreate helped. Thank you!

ekmst avatar Oct 01 '19 10:10 ekmst