Yii2 QueryBuilder遇到很多麻烦

时间:2016-06-16 23:35:27

标签: model-view-controller yii2

    public function getOrderItemStatus($magentoorderitemid) {
    $query = (new \yii\db\Query())
        ->select(["COALESCE(ShipTracking.ShipCarrier, '') AS ShipCarrier", "COALESCE(ShipTracking.ShipVendor, '') AS ShipMethod", "COALESCE(ShipTracking.ShipCarrier, '') AS ShipCarrier","COALESCE(ShipTracking.TrackingNumber, '') AS TrackingNumber"])
        ->from('OrderDetails')
        ->leftJoin('PickTicketOrders','PickTicketOrders.OrderId = OrderDetails.OrderId')
        ->leftJoin('ShipTracking', 'ShipTracking.PickTicketId = PickTicketOrders.Id')
        ->leftJoin('Products', 'Products.ISBN = OrderDetails.ISBN')
        ->where('OrderDetails.MagentoOrderItemId=:magentoorderitemid', [':magentoorderitemid' => $magentoorderitemid]);
    $result = Yii::$app->db->createCommand($query)->execute();
    return $result;
}

即使我删除joinswhere,我也会将选择更改为*。它不起作用

2 个答案:

答案 0 :(得分:1)

  $query = (new \yii\db\Query())
  ->select(["COALESCE(ShipTracking.ShipCarrier, '') AS ShipCarrier", "COALESCE(ShipTracking.ShipVendor, '') AS ShipMethod", "COALESCE(ShipTracking.ShipCarrier, '') AS ShipCarrier","COALESCE(ShipTracking.TrackingNumber, '') AS TrackingNumber"])
  ->from('OrderDetails')
  ->leftJoin('PickTicketOrders','PickTicketOrders.OrderId = OrderDetails.OrderId')
  ->leftJoin('ShipTracking', 'ShipTracking.PickTicketId = PickTicketOrders.Id')
  ->leftJoin('Products', 'Products.ISBN = OrderDetails.ISBN')
  ->where('OrderDetails.MagentoOrderItemId=:magentoorderitemid', [':magentoorderitemid' => $magentoorderitemid]);
  $command = $query->createCommand();
  $result = $command->queryAll();

以这种方式使用此查询

答案 1 :(得分:0)

我最终使用以下内容并且有效

        $query = (new \yii\db\Query())
        ->select(["COALESCE(ShipTracking.ShipCarrier, '') AS ShipCarrier", "COALESCE(ShipTracking.ShipVendor, '') AS ShipMethod", "COALESCE(ShipTracking.ShipCarrier, '') AS ShipCarrier","COALESCE(ShipTracking.TrackingNumber, '') AS TrackingNumber"])
        ->from('OrderDetails')
        ->leftJoin('PickTicketOrders','PickTicketOrders.OrderId = OrderDetails.OrderId')
        ->leftJoin('ShipTracking', 'ShipTracking.PickTicketId = PickTicketOrders.Id')
        ->leftJoin('Products', 'Products.ISBN = OrderDetails.ISBN')
        ->where('OrderDetails.MagentoOrderItemId=:magentoorderitemid', [':magentoorderitemid' => $magentoorderitemid]);
    $result = $query->all();