-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ActiveRecord refresh() is failed when using transactions and default attrib is by Expression #8390
Comments
Is it possible to get the value from last_insert_id? |
@mootensai what database do you use? It should work for PostgreSQL and Oracle. It won't work for any other database, this is just not possible as the database evaluates the expression on INSERT but there's no way to return the value of the PK. You'd have to run that expression yourself and use a value in your filter. |
$model->save();
print_r(mysql_insert_id());//output : 0
print_r($model->getDb()->lastInsertID); //output : 0 @nineinchnick I use MySQL. but when i try $model->save(), it insert to my table with generated UUID. class MyModel extends ActiveRecord{
public function rules(){
return ['id', 'default', 'value' => $this->generateUUID()] // PHP generated UUID
}
} $model->refresh() is working. Can't I use the DB generated UUID? Is there any other work around? |
@mootensai that's the way MySQL works. If you issue an MySQL only allows you to associate last inserted record if using an autoincrementing column and the lastInsertId value, assuming you insert just one row. Other databases, like PostgreSQL and Oracle, can return values from the insert statement, even if they're expressions. This is supported by Yii since the latest version, 2.0.4. I think this issue should be closed. |
@mootensai a workaround is to generate the UUID like this: class MyModel extends ActiveRecord{
public function rules(){
return ['id', 'default', 'value' => $this->getDb()->createCommand("REPLACE(UUID(),'-','')")->queryScalar()];
}
} |
@nineinchnick : your code works!! Thanks a lot! |
still it does not belong into |
@cebe Ok, Thanks! |
I try to implement UUID on insert, so i use
but when i do this
NB :
even if i use
it's not working.
i still have to use
It's not the same as the documentation here :
http://www.yiiframework.com/doc-2.0/guide-db-dao.html#performing-transactions
The text was updated successfully, but these errors were encountered: