Yii 框架中的模型?

发布于 2024-08-20 02:53:45 字数 288 浏览 2 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离旧人 2024-08-27 02:53:45

这是《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

半﹌身腐败 2024-08-27 02:53:45

好吧,我认为你可以通过使用关系 '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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文