omidgh88
(Omidgholami88)
1
hi i want fill a field named "pid" for example based on auto increment id like this :
$this->pid = "PID/H".($this->id + 1200);
my aftersave code is like this :
/**
* @inheritdoc
*/
public function afterSave($insert, $changedAttributes){
parent::afterSave($insert, $changedAttributes);
$this->pid = "PID/H".($this->id + 1200);
}
that doesn’t work i tried with
/**
* @inheritdoc
*/
public function afterSave($insert, $changedAttributes){
parent::afterSave($insert, $changedAttributes);
$this->pid = "PID/H".($this->id + 1200);
$this->save();
}
that make an infinite loop (completely make sense)
any help ?
dimis283
(Dimis283)
2
As I understand you want $this->pid to be saved,so did you tried the beforeSave($insert) and not aftersave?
http://www.yiiframework.com/doc-2.0/yii-db-baseactiverecord.html#beforeSave()-detail
omidgh88
(Omidgholami88)
3
because id is autoincrement in db in beforesave() it doesn’t exist yet.
i dont think this approach works
phtamas
(Phtamas)
4
yii\db\ActiveRecord::updateAttributes()
Hope it helps.
omidgh88
(Omidgholami88)
5
thanks for comment but i did it this way :
/**
* @inheritdoc
*/
public function afterSave($insert, $changedAttributes){
parent::afterSave($insert, $changedAttributes);
if(!$this->pid)
{
$this->pid = "PID/H".($this->id + 1200);
$this->save();
}
}