fill field in aftersave(or another alternative) based on auto increment id

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 ?

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

because id is autoincrement in db in beforesave() it doesn’t exist yet.

i dont think this approach works

yii\db\ActiveRecord::updateAttributes()

Hope it helps.

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();

        }

    }