0

I have form with two input fields like name,userid and the table columns are id(autoincrement,name,userid). If i save a record, the auto increment value of id will save in a column of userid. I have used a trigger for this, But this is not suitable because i'm using more than 10 table, So i need to write trigger for all the table.

So suggest me how to do this in yii2 model without write trigger.

JeevaRekha
  • 383
  • 1
  • 7
  • 21

1 Answers1

0

If you want to have a custom autoincrement field. You can override beforeSave function in your model.

/**
 * @inheritdoc
 */
public function beforeSave($insert)
{
    if (parent::beforeSave($insert)) {
        if(!$this->id)
        {
            $this->id = yourFunctionToCreateAutoincrementValue();
        }
        return true;
    } else {
        return false;
    }
}
Ngô Văn Thao
  • 3,671
  • 1
  • 20
  • 24