Yii2:ActiveQuery “with"不工作 [英] Yii2: ActiveQuery "with" not working

查看:42
本文介绍了Yii2:ActiveQuery “with"不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个与 1:n 相关的模型/db-tables:一个 order 有多个 commissions,一个 Commission 有多个 commission_positions.因此,commission_position 有一个包含佣金 id 的 FK 字段.佣金本身有一个包含订单 ID 的 FK 字段.

I have three models/db-tables related with 1:n each: An order has multiple commissions and a commission has multiple commission_positions. Therefore the commission_position has an FK-field containing a commission id. The commission itself has an FK-field containing the id of an order.

订单 > 佣金 > 佣金位置

Order > Commission > CommissionPositions

我现在需要做的是在相关的订单模型中选择所有具有特定值的 CommissionPositions.明显的解决方案是使用我用命名范围扩展的 CommissionPosition 的查询对象.命名范围如下所示:

What I need to do now is select all the CommissionPositions having a certain value in the related Order-Model. Obvious solution is to use the Query-Object of CommissionPosition which I extended with a named scope. The named scope looks like this:

class CommissionPositionQuery extends ActiveQuery
{
   /**
     * Named scope to filter positions of a certain alpha order id
     * @param integer $id the alpha order id
     * @return \common\models\query\CommissionPositionQuery
     */
    public function alphaOrderId($id)
    {
        //TODO: with not working
        $this->with(['commission.order']);
        $this->andWhere(['alpha_order_id'=>$id]);
        return $this;
    }
}

关系commission 是在Commission-Model 上定义的并且可以工作.第二个关系 order 是在佣金模型上定义的,并且同样有效.过滤字段 alpha_order_id 位于订单表中.现在我像这样执行查询:

The relation commission is defined on the Commission-Model and working. The second relation order is defined on the commission-model and working as well. The filtered field alpha_order_id is in the Order-Table. Now I execute the query like this:

$filteredPositions = CommissionPosition::find()->alphaOrderId(17)->all();

成功调用了范围并使用了 where-part,但是当我检查生成的 SQL 时,我看不到任何连接语句,即使我使用 with-方法告诉 yii 获取关系在一起.响应是未知列 alpha_order_id",这是有道理的,因为没有连接到相关表.这是生成的 SQL:

The scope is called successfully and the where-part is used, but when I check the generated SQL I see no join-statements even though I use the with-method to tell yii to fetch the relation together. The response is 'unknown column alpha_order_id' which makes sense as there is no join to the related tables. This is the generated SQL:

SELECT * FROM `commission_position` WHERE (`alpha_order_id`=17)

我错过了什么?这是Yii2的bug吗?

What am I missing? Is this a bug of Yii2?

推荐答案

我自己找到了解决方案.Yii1 和 Yii2 之间的命名变化导致了一些混乱.为了防止其他人在这里浪费时间,详细信息:

Found the soution myself. The naming changes between Yii1 and Yii2 lead to a little confusion. To prevent others from wasting time here the details:

在 yii 1 中,您可以像这样直接加入关系(例如:佣金):

In yii 1 you would join in a relation (exemplary: commission) directly like this:

$query->with = 'commission'
$query->together = true;

Yii2/差异

当调用问题中显示的 with 方法时,该关系已成功添加到 ActiveQuery 的 with 数组中.但是,在执行查询时,缺少连接部分.

Yii2 / difference

When calling the with-method like showed in the question the relation was successfully added to the with-array of the ActiveQuery. However, when executing the query, the join part was missing.

似乎 with-方法不是要走的路.相反,我使用了名为 joinWith 的方法,其签名如下:

Seems like the with-method is NOT the way to go. Instead I used the method called joinWith with the following signature:

public function joinWith($with, $eagerLoading = true, $joinType = 'LEFT JOIN')

现在,如答案中所述,我将关系定义为第一个参数 ('commission.order'),其余部分保持原样,因为默认值非常好.注意第二个参数的默认值.这可以确保直接加入关系!

Now as described in the answer I defined the relation as the first argument ('commission.order') and left the rest as is, because the default values are perfectly fine. Pay attention to the default value of the second parameter. this makes sure the relations are joined in directly!

Voilà...生成的 sql 包含所需的连接.但是要考虑一个陷阱:模糊的列命名当然要由我们自己处理!链接到该方法的文档:

Voilà...the resulting sql contains the needed joins. One pitfall is to be considered though: Ambigious column namings is of course to be handled by ourselves! Link to the documentation of the method:

http://www.yiiframework.com/doc-2.0/yii-db-activequery.html#joinWith()-detail

这篇关于Yii2:ActiveQuery “with"不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆