简体   繁体   English

PHP Lithium框架-如何在模型中使用xSQL FUNC

[英]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 - 例如,在PHP Lithium框架中,如果我们要创建一个新的用户对象并将其添加到db中,则可以使用此-

$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 : 现在我想使用MySQL FUNC UUID()替换PHP中的uniqid(),SQL与此类似:

INSERT INTO users SET user_guid=UUID();

How to do this? 这个怎么做? Thanks. 谢谢。

Note that lithium\\util\\String has a uuid() generator: 注意lithium\\util\\String有一个uuid()生成器:

https://github.com/UnionOfRAD/lithium/blob/master/util/String.php#L61 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.) (恐怕我的锂电池太生锈了,以至于不记得如何使用sql表达式,但我很确定这也是可能的。)

If you want to use some specific SQL functions like NOW() or UUID() you should be able to use them using the following notation: 如果要使用某些特定的SQL函数(例如NOW()UUID() ,则应该可以使用以下符号来使用它们:

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

This way, the string won't be "escaped". 这样,字符串就不会被“转义”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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