This is the one with MySQL.
select uuid();
What to do with the ActiveRecord.
id = 'uuid()';...$model->save();
Problem: Obviously the above method is not possible, there is no other way to deal with
Reply content:
This is the one with MySQL.
select uuid();
What to do with the ActiveRecord.
id = 'uuid()';...$model->save();
Problem: Obviously the above method is not possible, there is no other way to deal with
It is possible to add a primarykeybehavior to the activerecord::behaviors () to deal with Activerecord::event_before_insert.
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()');
}
}
Called in the model.
class Test extends \yii\db\ActiveRecord
{
...
public function behaviors()
{
return [
\common\behaviors\PrimaryKeyBehavior::className(),
];
}
...
}
This approach allows for flexible invocation, called in the required model, and does not include $model->id = uuid when writing logic
Determine if the ID is a string type, and then try this:
$model->id = new \yii\db\Expression('uuid()')