Trying to use the Yii2 TimestampBehavior but when saving it will return a doesn't have default value error.
The values are not required in the rules. The DB column is a INT(11).
As I needed to save several rows I use batchInsert() for this.
When I add a the value with time() it will save everything to the DB.
public function behaviors()
{
return [
'timestamp' => [
'class' => TimestampBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => 'created_at',
ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_at',
],
'value' => new Expression('NOW()'),
],
'blameable' => [
'class' => BlameableBehavior::className(),
'createdByAttribute' => 'created_by',
'updatedByAttribute' => 'updated_by',
],
];
}
Yii::$app->db->createCommand()->batchInsert( 'table_cms',
[
//all fields but not the timestamps
],
[
//rows here
])
->execute();