Yii2不唯一的表/别名

时间:2017-04-11 03:56:08

标签: mysql yii2-advanced-app

我创建一个如下所示的查询,但总是错误不是唯一的表/别名,尽管我在每个关系模型中为askPo添加了别名。

$query = AskPolicy::find();
      $query-> JoinWith(['askPos'])
           ->where(['askPolicy.id' => 'askPos.id_policy'])
          ->innerJoinWith(['askPos.askGoods'])
        ->andWhere('ask_po.id = ask_goods.id_po')
        ->innerJoinWith(['askTransports'])
        ->andWhere('ask_policy.id = ask_transport.id_po')
        ->innerJoinWith(['client'])->andWhere(['id_client'=>Yii::$app->user->identity->id_client])->all();

下面我添加了别名

模型a:

public function getAskPos()
    {
        return $this->hasMany(\frontend\modules\ask_marine_cargo\models\AskPo::className(), ['id_policy' => 'id'])->from(AskPo::tableName() . ' askPo1');;
    }

模型b:

public function getPo()
{
    return $this->hasOne(\frontend\modules\ask_marine_cargo\models\AskPo::className(), ['id' => 'id_po'])->from(AskPo::tableName() . ' getPo');
}

SQL查询:

SELECT
    ask_goods.name,
    ask_po.po_num,
    ask_policy.policy_num,
    ask_transport.type_transport
FROM ask_policy
    INNER JOIN ask_po
     ON ask_po.id_policy = ask_policy.id
    INNER JOIN ask_goods
     ON ask_po.id = ask_goods.id_po
    INNER JOIN ask_transport
     ON ask_policy.id = ask_transport.id_po;

0 个答案:

没有答案