Yii 框架中的模型?
我使用 Yii 框架构建一个网站应用程序。
我想将模型与查询一起使用,如下所示:
SELECT u.id, u.username, u.score, (SELECT COUNT(ownerId) FROM post p WHERE p.ownerId = u.id) AS totalPost
FROM users u
ORDER BY u.score DESC, totalPost DESC LIMIT 10
请帮助我将查询转换为 Yii Framework 中的模型。
I build a website application using Yii Framework.
I want to use model with query like this:
SELECT u.id, u.username, u.score, (SELECT COUNT(ownerId) FROM post p WHERE p.ownerId = u.id) AS totalPost
FROM users u
ORDER BY u.score DESC, totalPost DESC LIMIT 10
Please help me to convert the query into models in Yii Framework.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是《Yii 明确指南》的相关章节 - http://www.yiiframework .com/doc/guide/database.arr
Here is related chapter from "The Definite Guide To Yii" - http://www.yiiframework.com/doc/guide/database.arr
好吧,我认为你可以通过使用关系 'TotalPost' => 来简化它array(self::STAT,'Post','OwnerId')
检查 CStatRelation 了解更多详细信息。请注意,在该类中,select 属性是统计表达式,默认为 COUNT(*)。
此外,只要您有一个公共 $TotalPost;,您的查询就可以映射到用户模型中。在模型类 User.
Well I think you can simplify this by using a relation 'TotalPost' => array(self::STAT,'Post','OwnerId')
Check CStatRelation for more details. Note that in this class, the select property is the statistical expression which is by default COUNT(*).
Also, your query can be mapped into User model as long as you have a public $TotalPost; in model class User.