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

  • <small id='dYWuH'></small><noframes id='dYWuH'>

  • <legend id='dYWuH'><style id='dYWuH'><dir id='dYWuH'><q id='dYWuH'></q></dir></style></legend>
    <tfoot id='dYWuH'></tfoot>

      <bdo id='dYWuH'></bdo><ul id='dYWuH'></ul>

      1. Yii2 - 在多个条件下左连接

        时间:2023-10-16

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

            • <small id='WpShX'></small><noframes id='WpShX'>

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

                  <tfoot id='WpShX'></tfoot>
                  本文介绍了Yii2 - 在多个条件下左连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有三个表具有以下关系,

                  I have three tables with the following relations,

                    ------- 1        0..* ------------
                   |Product|-------------|Availability|
                    -------               ------------
                      1 |
                        |
                      1 |
                    --------
                   |MetaData|
                    --------
                  

                  我的原始 sql 看起来像这样

                  my raw sql looks like this

                  SELECT p.ID FROM product p 
                  LEFT JOIN availability a ON a.productID=p.ID 
                            AND a.start>=DATE_ADD(DATE(now()), INTERVAL 7 DAY)
                  LEFT JOIN meta_data m ON m.ID=p.meta_dataID
                  WHERE a.ID IS NULL
                  AND m.published_state=1;
                  

                  也就是说,找到每个 ProductMetaData.published_state 等于 1 而没有 Availability 这样的Availability.startnow() 开始超过 7 天.

                  That is, find each Product with a MetaData.published_state equal to 1 and with no Availability such that Availability.start more than 7 days from now().

                  我正在尝试使用 ActiveRecord 方法来完成相同的操作,使用类似以下内容,

                  I'm trying to accomplish the same using ActiveRecord methods, using something like the following,

                  $products = Product::find()
                              ->joinWith('metaData')
                              ->joinWith('availability')
                              ->onCondition(['>=', 'availability.start', strtotime('+7 days')])
                              ->where(['is', 'availability.ID', NULL])
                              ->andWhere(['=', 'meta_data.published_state', 1])
                              ->all();
                  

                  然而,这没有返回任何结果.使用 Connection::createCommand() 运行原始 sql 会返回我期望的行,因此数据没有问题.

                  however, this is returning no results. Using Connection::createCommand() to run the raw sql returns the rows I'd expect so there is no issue with the data.

                  我怀疑问题是由 join 条件和 where 条件相互渗透"引起的;join 和 where 都应用于 join 或 where 而不是分开.

                  I suspect the issue is being caused by the join conditions and the where conditions 'bleeding' into each other; both join and where being applied to either the joining or the where rather than separately.

                  如何输出正在运行的实际 sql 查询?这是从控制台控制器调用的操作.

                  How can I output the actual sql query being run? this is in an action being called from a console controller.

                  如何更改我的代码以返回所需的Products?

                  How can I alter my code to return the desired Products?

                  推荐答案

                  我相信这是更好的解决方案.不要使用像 leftJoin 这样的原始查询,你应该用 andOnCondition 补充你的 joinWith 关系(这会在你的 join 语句中添加需要的 where 条件).

                  I believe this one is better solution. Instead of using Raw queries like leftJoin you should complement your joinWith relations with andOnCondition (which adds needed where conditions into your join statement).

                  $products = Product::find()
                      ->joinWith(['metaData' => function (ActiveQuery $query) {
                          return $query
                              ->andWhere(['=', 'meta_data.published_state', 1]);
                      }])
                      ->joinWith(['availability' => function (ActiveQuery $query) {
                          return $query
                              ->andOnCondition(['>=', 'availability.start', strtotime('+7 days')])
                              ->andWhere(['IS', 'availability.ID', NULL]);
                      }])
                      ->all();
                  

                  此外,当您在关系中编写 where 子句时,它看起来更干净.它的工作原理和在外面写一样(如果我没记错的话),但是在重构查询时,您可以轻松删除整个关系,而不会忘记外面的关系条件.

                  In addition it looks cleaner when you write where clauses inside relations. It works the same as writing it outside (if I'm not wrong), but when refactoring your query, you can easily delete the whole relation without forgetting relation conditions outside.

                  这篇关于Yii2 - 在多个条件下左连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 Yii2 Active Record 中将 JOIN 用于关系模型? 下一篇:Yii2 QueryBuilder 更新与加入

                  相关文章

                • <tfoot id='oM7jD'></tfoot>
                    <bdo id='oM7jD'></bdo><ul id='oM7jD'></ul>

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

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