简体   繁体   中英

PHP Lithium framework - How to use xSQL FUNC in model

For example, in PHP Lithium framework, if we want to create a new user object and add it to db, we can use this -

$model = 'app\models\Users';
$user = $model::create();
$user->user_guid = uniqid ();

$user->save ();

Now I want to use MySQL FUNC UUID() to replace uniqid() in PHP, the SQL is similar to this :

INSERT INTO users SET user_guid=UUID();

How to do this? Thanks.

Note that lithium\\util\\String has a uuid() generator:

https://github.com/UnionOfRAD/lithium/blob/master/util/String.php#L61

(I'm afraid my lithium-fu is a bit too rusty to remember how to use an sql expression, but I'm pretty certain that is possible too.)

If you want to use some specific SQL functions like NOW() or UUID() you should be able to use them using the following notation:

$user->user_guid = (object) 'UUID()';

This way, the string won't be "escaped".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM