Yii\behaviors\autotimestamp Not Working For Me

Hi All,

I am currently following the post tutorial for Yii2 using the basic app. it says to add:





    public function behaviors()

{

    return array(

        'timestamp' => array(

            'class' => 'yii\behaviors\AutoTimestamp'

        )

    );

}



Below that but it doesnot work:




    public function rules()

    {

        return [

            [['title', 'data', 'create_time', 'update_time'], 'required'],

            [['data'], 'string'],

            [['create_time', 'update_time'], 'integer'],

            [['title'], 'string', 'max' => 255]

        ];

    }




I keep getting this error when I create a post:




ReflectionException


Class yii\behaviors\AutoTimestamp does not exist

1. in C:\wamp\www\yii2\vendor\yiisoft\yii2\di\Container.php at line 408

399400401402403404405406407408409410411412413414415416417     * @return array the dependencies of the specified class.

     */

    protected function getDependencies($class)

    {

        if (isset($this->_reflections[$class])) {

            return [$this->_reflections[$class], $this->_dependencies[$class]];

        }

 

        $dependencies = [];

        $reflection = new ReflectionClass($class);

 

        $constructor = $reflection->getConstructor();

        if ($constructor !== null) {

            foreach ($constructor->getParameters() as $param) {

                if ($param->isDefaultValueAvailable()) {

                    $dependencies[] = $param->getDefaultValue();

                } else {

                    $c = $param->getClass();

                    $dependencies[] = Instance::of($c === null ? null : $c->getName());




Any idea why please?

Thank you,

Ben

Behavior class was renamed, it’s called TimestampBehavior now.

Here’s the example:


use yii\db\Expression;


public function behaviors()

{

    return [

        'timestamp' => [

            'class' => TimestampBehavior::className(),

            'attributes' => [

                ActiveRecord::EVENT_BEFORE_INSERT => 'creation_time',

                ActiveRecord::EVENT_BEFORE_UPDATE => 'update_time',

            ],

            'value' => new Expression('NOW()'),

        ],

    ];

}

Thanks for when I add your code I get this error:

PHP Fatal Error – yii\base\ErrorException

Trait ‘Yii\db\Expression’ not found

You don’t need write “use Yii\db\Expression”. Just “use Yii”

And in behaviors :

return [

     'timestamp' => [


          'class' => TimestampBehavior::className(),


          'attributes' => [


              ActiveRecord::EVENT_BEFORE_INSERT => 'field_name',


              


          ],


          'value' => new Expression('NOW()'),


      ],


  ];

Check letter case, seems like you have "Y" uppercased for some reason.

Ok Sorted, this was the code that worked for me




public function behaviors()

    {

        return [

            'timestamp' => [

                'class' => TimestampBehavior::className(),

                'attributes' => [

                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],

                    ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_at',

                ],

                'value' => new Expression('NOW()'),

            ],

        ];

    }