This {code...} comes with Mysql ...} how to deal with {code...} In ActiveRecord ...} problem: Obviously, this method is not feasible. Is there any other way to handle this problem that comes with Mysql?
select uuid();
What should I do in ActiveRecord?
id = 'uuid()';...$model->save();
Problem: Obviously, this method is not feasible. Is there any other processing method?
Reply content:
This is the built-in Mysql
select uuid();
What should I do in ActiveRecord?
id = 'uuid()';...$model->save();
Problem: Obviously, this method is not feasible. Is there any other processing method?
Add a PrimaryKeyBehavior in ActiveRecord: behaviors () to process ActiveRecord: EVENT_BEFORE_INSERT. This method is feasible.
class PrimaryKeyBehavior extends AttributeBehavior{ public $primaryKeyAttribute = 'id'; public $value; public function init() { parent::init(); if (empty($this->attributes)) { $this->attributes = [ BaseActiveRecord::EVENT_BEFORE_INSERT => [$this->primaryKeyAttribute], ]; } } protected function getValue($event) { return new Expression('UUID()'); }}
Call in model
class Test extends \yii\db\ActiveRecord{ ... public function behaviors() { return [ \common\behaviors\PrimaryKeyBehavior::className(), ]; } ...}
This method can be flexibly called, and called in a Model that is needed, without adding $ model-> id = uuid () when writing logic ()
Make sure that the id is of the string type and try again as follows:
$model->id = new \yii\db\Expression('uuid()');