沃梦达 / 编程问答 / php问题 / 正文

Yii2 - 在多个条件下左连接

Yii2 - left join on multiple condition(Yii2 - 在多个条件下左连接)

本文介绍了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 - 在多个条件下左连接

基础教程推荐