douba05167 2017-02-14 05:55
浏览 375

在Linux中的joinWith(YII2)中添加别名不起作用

I'm working in this query.

$data = RtvReqDet::find()
            ->joinWith(['header', 'rtvCard.serial sn', 'rtvCard.serial.item si', 'rtvCard.item ri' ])
            ->select([
                'header.requested_by as classification',
                'ifnull(sn.no, rtvCard.serial_no) AS serial',
                'ifnull(si.no, ri.no) AS item',
                'ifnull(si.uom, ri.uom) AS uom',
                'COUNT(ifnull(sn.no, rtvCard.serial_no)) AS qty',
            ])
            ->where(['rtv_spare_parts_requisition_id' => $record->id ])
            ->groupBy(['serial'])->all();

It's working on Windows. But is not working on Linux and giving this error:

Exception 'yii\base\InvalidParamException' with message 'app\models\RtvCard has no relation named "serial AS sn".' 

I tried lowering my php version and mysql version exactly with the Linux. but I can still transact on my windows. So it doesn't have any conflict with php and mysql version. what do you think is wrong with this?

this is the rtvCard model. This was customized by my colleague whom I've worked with before. I deleted some relations where I think not needed in this discussion.

<?php

namespace app\models;

use app\components\ActiveRecord;
use app\components\FieldException;
use yii\base\Exception;

class RtvCard extends ActiveRecord  {

    static function tableName() {
        return "rtv_card";
    }

    static function objectName()    {
        return "rtv card";
    }

    // RELATIONS
    function getCustomer()  {
        return $this->hasOne(Customer::className(), [ "id" => "customer_id" ])->from([ "customer" => Customer::tableName() ]);
    }
    function getSerial()    {
        return $this->hasOne(SerialNumbers::className(), [ "id" => "serial_id" ])->from([ "serial" => SerialNumbers::tableName() ]);
    }
    function getItem()  {
        return $this->hasOne(Items::className(), [ "id" => "item_id" ])->from([ "item" => Items::tableName() ]);
    }
    function getTransfer()  {
        return $this->hasOne(RtvTransferDetail::className(), [ "rtv_card_id" => "id" ])->from([ "transfer" => RtvTransferDetail::tableName() ]);
    }

    // USED FOR ERROR MESSAGES
    static $attributeLabels = [
        "id" => "ID",
        "no" => "RTV No",
        "item_id" => "Item",
        "serial_id" => "Serial no.",
        "defect_id" => "Defect",
        "description" => "Complaints",
        "date_received" => "Date received",
        "received_by" => "Received by",
        "customer_id" => "Dealer",
        "reference_no" => "Reference no.",
        "tag" => "Tag",
        "technician_id" => "Technician",
        "warehouse" => "Warehouse",
    ];
    // FIELD NAMES IN VIEW IF NOT THE SAME WITH ATTRIBUTE
    // USE STATIC::GET-FIELD($ATTRIBUTE-NAME)
    // ATTRIBUTE NAME SHOULD BE THE TABLE ATTRIBUTES ITSELF ( NOT THE FIELD NAME IN VIEWS )
    // ATTRIBUTE NAME => FIELD NAME
    static $attributeFields = [
        "item_id" => "item",
        "serial_id" => "serial",
        "defect_id" => "defect",
        "customer_id" => "dealer",
        "received_by" => "received-by",
        "date_received" => "received-date",
        "reference_no" => "reference-no",
        "open_serial_id_no" => "no",
        "open_serial_id_item" => "item",
        "technician_id" => "technician",
    ];
  • 写回答

2条回答 默认 最新

  • dswfyq6201 2017-02-14 07:47
    关注

    Since Yii 2.0.7, the shortcut for relational aliases is 'rtvCard.serial sn' and not 'rtvCard.serial AS sn' (From Bizley's comment AS works too)

    From the docs:

    If you need an alias for an intermediate table when joining over nested relations, e.g. $query->joinWith(['orders.product']), you need to nest the joinWith calls like in the following example:

    $query->joinWith(['orders o' => function($q) {
        $q->joinWith('product p');
    }])
    ->where('o.amount > 100');
    

    So in your case:

    RtvReqDet::find()
        ->joinWith(['rtvCard' => function($q) {
            $q->joinWith(['serial sn', 'serial.item si', 'item ri']);
        }])
        ->select([ ...
    
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?