如何获取内部joinWith()yii2中的基表列

时间:2016-04-20 11:17:31

标签: php mysql yii2 yii2-advanced-app yii2-model

订单和订单产品。如何获取订单表列joinWith()。

      Order::find()
        ->select(['order.*'])
        ->joinWith(['orderProducts' => function($q){
                $q->select("order_id, product_id, product_name, product_price, quantity, CONCAT(order.currency, total_price) AS total_price");
            }])

在上面的查询中,它显示了以下错误:

 SQLSTATE[42S22]: Column not found: 1054 Unknown column 'order.currency' in 'field list'\nThe SQL being executed was: SELECT order.currency, id, CONCAT('http://192.168.1.114:1090/backend/web/images/products/',image) AS image, sku FROM `POS_1hj2gfru`.`product` WHERE `id`='3'"  

1 个答案:

答案 0 :(得分:1)

请使用

Order::find()
    ->select(["order.*","orderProducts.order_id" , "orderProducts.product_id" , "orderProducts.product_name" , "orderProducts.product_price" , "orderProducts.quantity" , "orderProducts.CONCAT(orderProducts.order.currency, orderProducts.total_price) AS total_price"])
    ->joinWith('orderProducts', false)
    ->asArray()
    ->all();