ljames
(James Luke318)
May 30, 2016, 8:51pm
1
I have some models that are only created and never updated. They just have a "created_at" timestamp. However, when I implement the TimestampBehavior I get errors thrown if there is no "EVENT_BEFORE_UPDATE" action set.
I want to be able to set the created_at attribute using the TimestampBehavior without having to add an "updated_at" attribute.
I have tried the following but it did not work…
public function behaviors()
{
return [
TimestampBehavior::className(),
'timestamp' => [
'class' => 'yii\behaviors\TimestampBehavior',
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
ActiveRecord::EVENT_BEFORE_UPDATE => false,
],
],
];
}
Any idea how to achieve this?
softark
(Softark)
May 30, 2016, 11:07pm
2
Try either
public function behaviors()
{
return [
'timestamp' => [
TimestampBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', false],
ActiveRecord::EVENT_BEFORE_UPDATE => false,
],
],
];
}
or
public function behaviors()
{
return [
'timestamp' => [
TimestampBehavior::className(),
'createdAtAttribute' => 'created_at'
'updatedAtAttribute' => false,
],
];
}
http://www.yiiframework.com/doc-2.0/yii-behaviors-timestampbehavior.html
ljames
(James Luke318)
May 31, 2016, 4:29pm
3
Thanks for the shot Softark but I couldn’t get either of these ways, or any combination of them to work.
I tried this…
public function behaviors()
{
return [
TimestampBehavior::className(),
'timestamp' => [
'class' => 'yii\behaviors\TimestampBehavior',
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', false],
ActiveRecord::EVENT_BEFORE_UPDATE => false,
],
],
];
}
and,
public function behaviors()
{
return [
TimestampBehavior::className(),
'timestamp' => [
'class' => 'yii\behaviors\TimestampBehavior',
'attributes' => [
'createdAtAttribute' => 'created_at',
'updatedAtAttribute' => false,
],
],
];
}
They both still threw an error saying "Setting unknown property: common\models\UserAccountCredit::updated_at"
Bizley
(Bizley)
May 31, 2016, 6:20pm
4
This should be
public function behaviors()
{
return [
'timestamp' => [
'class' => TimestampBehavior::className(),
'updatedAtAttribute' => false,
],
];
}
softark
(Softark)
May 31, 2016, 10:17pm
5
public function behaviors()
{
return [
/* TimestampBehavior::className(), ... This line should be omitted */
'timestamp' => [
'class' => 'yii\behaviors\TimestampBehavior',
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', false],
ActiveRecord::EVENT_BEFORE_UPDATE => false,
],
],
];
}
or
public function behaviors()
{
return [
/* TimestampBehavior::className(), ... This line should be omitted */
'timestamp' => [
'class' => 'yii\behaviors\TimestampBehavior',
'attributes' => [
'createdAtAttribute' => 'created_at',
'updatedAtAttribute' => false,
],
],
];
}
ljames
(James Luke318)
May 31, 2016, 10:31pm
6
public function behaviors()
{
return [
'timestamp' => [
'class' => TimestampBehavior::className(),
'updatedAtAttribute' => false,
],
];
}
I tried this but I’m still getting the same error. It’s weird because I swear I have achieved this before in an old AR model that is now deleted. I looked through the GitHub history but couldn’t find it.
I also tried this, but no success…
public function behaviors()
{
return [
'timestamp' => [
'class' => TimestampBehavior::className(),
'updatedAtAttribute' => false,
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
ActiveRecord::EVENT_BEFORE_UPDATE => false,
],
],
];
}
Renka
(Renkas)
May 31, 2016, 10:48pm
7
Try this. If you set attributes in config then the default attributes that include updated_at should not be set at all - so there is nothing for you to unset.
public function behaviors()
{
return [
'timestamp' => [
'class' => TimestampBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
],
],
];
}
Try this out
public function behaviors()
{
return [
[
'class' => TimestampBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
],
// if you're using datetime instead of UNIX timestamp:
'value' => new Expression('NOW()'),
],
];
}
arshadcomp
(arshadcomp)
February 14, 2020, 6:55pm
9
Try this:
public function behaviors()
{
return [
[
'class' => TimestampBehavior::className(),
'createdAtAttribute' => false,
],
];
}