1. <tfoot id='pjxvg'></tfoot>

      <small id='pjxvg'></small><noframes id='pjxvg'>

      <legend id='pjxvg'><style id='pjxvg'><dir id='pjxvg'><q id='pjxvg'></q></dir></style></legend>

      <i id='pjxvg'><tr id='pjxvg'><dt id='pjxvg'><q id='pjxvg'><span id='pjxvg'><b id='pjxvg'><form id='pjxvg'><ins id='pjxvg'></ins><ul id='pjxvg'></ul><sub id='pjxvg'></sub></form><legend id='pjxvg'></legend><bdo id='pjxvg'><pre id='pjxvg'><center id='pjxvg'></center></pre></bdo></b><th id='pjxvg'></th></span></q></dt></tr></i><div id='pjxvg'><tfoot id='pjxvg'></tfoot><dl id='pjxvg'><fieldset id='pjxvg'></fieldset></dl></div>

        • <bdo id='pjxvg'></bdo><ul id='pjxvg'></ul>

      1. 如何在 Yii2 Active Record 中将 JOIN 用于关系模型?

        时间:2023-10-16

              • <bdo id='bFRUT'></bdo><ul id='bFRUT'></ul>
                  <tbody id='bFRUT'></tbody>
                <legend id='bFRUT'><style id='bFRUT'><dir id='bFRUT'><q id='bFRUT'></q></dir></style></legend>

              • <i id='bFRUT'><tr id='bFRUT'><dt id='bFRUT'><q id='bFRUT'><span id='bFRUT'><b id='bFRUT'><form id='bFRUT'><ins id='bFRUT'></ins><ul id='bFRUT'></ul><sub id='bFRUT'></sub></form><legend id='bFRUT'></legend><bdo id='bFRUT'><pre id='bFRUT'><center id='bFRUT'></center></pre></bdo></b><th id='bFRUT'></th></span></q></dt></tr></i><div id='bFRUT'><tfoot id='bFRUT'></tfoot><dl id='bFRUT'><fieldset id='bFRUT'></fieldset></dl></div>
                <tfoot id='bFRUT'></tfoot>

                <small id='bFRUT'></small><noframes id='bFRUT'>

                  本文介绍了如何在 Yii2 Active Record 中将 JOIN 用于关系模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有 2 个表,名为 Books 和 Reviews.Books 表与 Reviews 是一对多的关系.

                  我想搜索书籍并按评论对它们进行排序.

                  例如,如果有 10 本书可用并且书在 Reviews 中有评论,那么我想使用 WHERE 子句查找所有书籍并计算那里的评论,然后根据评论编号对所有书籍进行排序.

                  我的 SQL 查询如下:

                   Books::find()->哪里(['和',['like', 'books.bookName', $bookName],['like', 'books.status', 'Enabled']])->joinWith(['reviews' => function ($q){$q->select(['COUNT(*) as cnt']);}])-> orderBy(['cnt' => 'DESC'])-> 全部();

                  它给了我以下错误消息:

                  <块引用>

                  SQLSTATE[42S22]:未找到列:1054 'order 子句'中的未知列 'cnt'

                  我在这里遗漏了什么?

                  解决方案

                  使用 joinWith.更多见

                  例如,对于这样的案例代码:

                  书籍::find()->joinWith(['reviews' => function ($q) {$q->select(['COUNT(*) as cnt']);}])-> orderBy(['cnt' => 'DESC'])-> 全部();

                  我找到了更好的解决方案.

                  书籍::find()->joinWith(['评论'])->select(['*', 'COUNT(reviews.*) as cnt'])->groupBy('RELATION_FIELD(例如:reviews.book_id)')-> orderBy(['cnt' => 'DESC'])-> 全部();

                  I have 2 tables called Books and Reviews. Books table has a one-to-many relationship with Reviews.

                  I want to search books and sort them by Reviews.

                  For example, if there are 10 books available and books has review in Reviews then I want to find all books by using WHERE clause and count there reviews and then order all books based on the review number.

                  My SQL query is like following:

                   Books::find()
                     ->where([
                       'and', 
                       ['like', 'books.bookName', $bookName],
                       ['like', 'books.status', 'Enabled'] 
                      ])
                    ->joinWith(['reviews' => function ($q){
                          $q->select(['COUNT(*) as cnt']);
                      }])
                    ->orderBy(['cnt' => 'DESC'])
                    ->all();
                  

                  It's giving me following error message:

                  SQLSTATE[42S22]: Column not found: 1054 Unknown column 'cnt' in 'order clause'

                  What am I missing here?

                  解决方案

                  Use joinWith. For more see

                  For example, for your case code like that:

                  Books::find()
                      ->joinWith(['reviews' => function ($q) {
                          $q->select(['COUNT(*) as cnt']);
                      }])
                      ->orderBy(['cnt' => 'DESC'])
                      ->all();
                  

                  EDIT: I find better solution.

                  Books::find()
                      ->joinWith(['reviews'])
                      ->select(['*', 'COUNT(reviews.*) as cnt'])
                      ->groupBy('RELATION_FIELD(Example: reviews.book_id)')
                      ->orderBy(['cnt' => 'DESC'])
                      ->all();
                  

                  这篇关于如何在 Yii2 Active Record 中将 JOIN 用于关系模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 yii2 中迁移时出现异常“找不到驱动程序" 下一篇:Yii2 - 在多个条件下左连接

                  相关文章

                  <small id='mCJCQ'></small><noframes id='mCJCQ'>

                  1. <tfoot id='mCJCQ'></tfoot>

                    <legend id='mCJCQ'><style id='mCJCQ'><dir id='mCJCQ'><q id='mCJCQ'></q></dir></style></legend>

                    <i id='mCJCQ'><tr id='mCJCQ'><dt id='mCJCQ'><q id='mCJCQ'><span id='mCJCQ'><b id='mCJCQ'><form id='mCJCQ'><ins id='mCJCQ'></ins><ul id='mCJCQ'></ul><sub id='mCJCQ'></sub></form><legend id='mCJCQ'></legend><bdo id='mCJCQ'><pre id='mCJCQ'><center id='mCJCQ'></center></pre></bdo></b><th id='mCJCQ'></th></span></q></dt></tr></i><div id='mCJCQ'><tfoot id='mCJCQ'></tfoot><dl id='mCJCQ'><fieldset id='mCJCQ'></fieldset></dl></div>

                      • <bdo id='mCJCQ'></bdo><ul id='mCJCQ'></ul>