Amitesh Kumar
Amitesh Kumar

Reputation: 3079

having error while implementing TimestampBehavior in yii2

In activerecord class i put this code

use yii\db\ActiveRecord;
use yii\behaviors\TimestampBehavior;
use yii\db\Expression;

public function behaviors()
{
  return [
    'class' => TimestampBehavior::className(),
        'attributes' => [
           ActiveRecord::EVENT_BEFORE_INSERT => ['create_time'],
           ActiveRecord::EVENT_BEFORE_UPDATE => ['create_time'],
    ],
  ];
}

but it is throwing me an error

Invalid Configuration – yii\base\InvalidConfigException
Object configuration must be an array containing a "class" element.

I dont know where i am doing wrong .
i also follow this link TimestampBehavior is not working in Yii2 but this is also not solving my problem.

Upvotes: 1

Views: 927

Answers (2)

arogachev
arogachev

Reputation: 33538

Here is the corrected version:

public function behaviors()
{
    return [
        [
            'class' => TimestampBehavior::className(),
            'attributes' => [
               ActiveRecord::EVENT_BEFORE_INSERT => ['create_time'],
               ActiveRecord::EVENT_BEFORE_UPDATE => ['create_time'],
            ],
        ],
    ];
}

Here is deeper explanation.

The main problem is you forgot to wrap behavior config into its own array, that's why the error is thrown. This config as all others is processed by Yii::createObject() method. So you need array containing at least class element. In case you want default config you can omit this for brevity with just specifying class name, for example:

public function behaviors()
{
    return [
        TimestampBehavior::className(),            
    ],
  ];
}

Besides that, you also forgot to close square bracket after attribute.

And finally, you should not specify attributes like that. It was like that at initial stage of this behavior, then it was refactored. Now you only need to specify according attribute names, for example:

public function behaviors()
{
    return [
        [
            'class' => TimestampBehavior::className(),
            'createdAtAttribute' => 'created_at', // This is by default, you can omit that
            'updatedAtAttribute' => 'updated_at', // This is by default, you can omit that
        ],
    ],
  ];
}

Also I don't think that having the same column for tracking create and update time is a good idea.

Additionally you can specify value used to update these columns:

use yii\db\Expression;

...

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

This will involve database to calculate time, if you want to do it on server side via PHP, you can do use closure for example like this:

'value' => function () {
    return date('Y-m-d H:i:s');
}

And one more thing even if it's not directly related with this problem - you can set alias for every behavior by setting a key of configuration array. This is called named behavior:

public function behaviors()
{
    return [
        'timestamp' => [
            ...
        ],
    ],
  ];
}

This was mentioned by @GAMITG in his answer.

Upvotes: 2

GAMITG
GAMITG

Reputation: 3818

You can try this code.

public function behaviors()
{   
    return [
          'timestamp' => [
            'class' => 'yii\behaviors\TimestampBehavior',
            'attributes' => [
                ActiveRecord::EVENT_BEFORE_INSERT => ['create_time'],
                ActiveRecord::EVENT_BEFORE_UPDATE => ['create_time'],
            ],
          ],
    ];
}

Upvotes: 1

Related Questions