更新
我有一个使用Yii插入的记录,我的模型名为Shipment,它实现了:
/**
* @inheritdoc
*/
public function behaviors()
{
return [
[
'class' => BlameableBehavior::className(),
],
[
'class' => TimestampBehavior::className(),
],
];
}假设这些是数据库中的记录,如下所示:
DATABASE (1:1)
+----+--------------------+--------------------+------------+------------+
| id | freight_created_at | freight_updated_at | created_at | updated_at |
+----+--------------------+--------------------+------------+------------+
| XX | NULL | NULL | 1597223608 | 1597315472 |
+----+--------------------+--------------------+------------+------------+然后,我需要更新另一个名为freight_created_at & freight_updated_at的专栏。这是因为在相同的记录中,所以我不能再次使用EVENT_BEFORE_INSERT。
我的行动是把运费,目标是:
如果列freight_updated_at
freight_created_at为空,则只填写freight_created_at和freight_created_at更新freight_updated_at,只填写。
然后在Yii2
控制器
public function actionPutFreightDitawarkan($id) {
$model = $this->findModel($id);
$model->scenario = Shipment::SCENARIO_FREIGHT_DITAWARKAN;
$model->attachBehaviors([FreightDitawarkanTimestamp::class]);
...
}如果我想使用一种行为,我如何实现它?到目前为止,这种行为是这样的。
行为
class FreightDitawarkanTimestamp extends AttributeBehavior {
public $createdAtAttribute = 'freight_created_at';
public $updatedAtAttribute = 'freight_updated_at';
public $value;
public function init() {
if (empty($this->attributes)) {
$this->attributes = [
BaseActiveRecord::EVENT_BEFORE_UPDATE =>
[
$this->createdAtAttribute,
$this->updatedAtAttribute
]
];
}
parent::init();
}
protected function getValue($event) {
$this->value = date('Y-m-d H:i');
return parent::getValue($event);
}
}发布于 2020-08-14 09:22:37
只有当您想在多个模型中使用它时,行为才适合这里,否则您可以只使用$model->touch() (即$model->touch('freight_created_at')),因为您可以通过已经附加的TimestampBehavior访问该方法。
但是,如果这是一个多模型的案例,那么你的方法是好的。为了满足您的要求,我也会覆盖evaluateAttributes(),例如:
public function evaluateAttributes($event)
{
if ($this->skipUpdateOnClean
&& $event->name == ActiveRecord::EVENT_BEFORE_UPDATE
&& empty($this->owner->dirtyAttributes)
) {
return;
}
if (!empty($this->attributes[$event->name])) {
$attributes = (array) $this->attributes[$event->name];
$value = $this->getValue($event);
foreach ($attributes as $attribute) {
if ($attribute === $this->createdAtAttribute && !empty($this->owner->$attribute)) {
// don't update "created_at" value if already set
continue;
}
$this->owner->$attribute = $value;
}
}
}getValue()可以简化为:
protected function getValue($event)
{
return date('Y-m-d H:i');
}https://stackoverflow.com/questions/63393839
复制相似问题