How would I use a MySQL function?

Hello!

I am just getting started with Yii but I ran into a little problem.

I would like to use the UUID() function provided by MySQL and don't know what's the best way to access it.

I guess writing my own insert method would be a bit too much.

If I made a method that returns the UUID, so that I can assign it to the active record in beforeValidate or beforeSave … how would I have to do that?

Am I supposed to instance a new CDbConnection and execute a "SELECT UUID()" or can I re-use something that is already there?

Inside your beforeValidate/beforeSave, you can access the DB connection via: $this->dbConnection and then create a SQL comment to call your function.

Thank you!

That's how I did it:

$command = $this->dbConnection->createCommand("SELECT UUID();");


$uuid = $command->queryScalar();


$this->id = $uuid;