使用 via 或 viaTable 关系从关系生成 mysql 查询

generate mysql query from relations using via or viaTable relations

考虑下面的例子...

class Customers extends ActiveRecord
{
    public function getOrders()
    {
        return $this->hasMany(Orders::className(), ['customer_id' => 'id']);
    }
    public function getOrderItems()
    {
        return $this->hasMany(OrderItems::className(), ['order_id' => 'id'])
                    ->via('orders');
    }
}

我如何从 getOrderItems() 关系

生成以下任何一个查询
SELECT * FROM `order-items`
LEFT JOIN `orders` ON `orders`.`id` = `order-items`.`order_id`
LEFT JOIN `customers` ON `customer`.`id` = `orders`.`customer_id`

SELECT `order-items`.* FROM `order-items`,`orders`,`customers`
WHERE `customer`.`id` = `orders`.`customer_id` AND `orders`.`id` = `order-items`.`order_id`

SELECT * FROM `order-items` WHERE `order_id` IN(
    SELECT * FROM `orders` WHERE `customer_id` IN(
        SELECT * FROM `customers`
    )
)

我使用以下代码来执行此操作。

$customers = Customers::findAll();
$query = $customers[0]->getOrderItems()->createCommand()->rawSql;

但它只生成

SELECT * FROM `order-items`

怎么办...???

使用这个:

$q = Customers::findAll()->innerJoinWith('orderItems')->createCommand()->rawSql;

你必须像这样使用关系名称而不是函数