1

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.

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks now i understand what i am doing wrong thanks again.
now error is gone , but 'create_time' is required and it not saving into database
1

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'],
            ],
          ],
    ];
}

2 Comments

How it's different from existing answer?
just i make group of class and attributes as timestamp.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.