加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

yii2 – 如何为同一个表的多个连接指定别名?

发布时间:2020-12-13 17:49:38 所属栏目:PHP教程 来源:网络整理
导读:我的模型中有两个关系定义在同一个表中 public function getCountry(){ return $this-hasOne(Country::className(),['country_id' = 'country_id']);}public function getCurrency(){ return $this-hasOne(Country::className(),['country_id' = 'currency']
我的模型中有两个关系定义在同一个表中

public function getCountry(){
    return $this->hasOne(Country::className(),['country_id' => 'country_id']);
}

public function getCurrency(){
    return $this->hasOne(Country::className(),['country_id' => 'currency']);
}

我想在我的查询中加入这两个关系.下面的代码显示错误.

Country::find()->joinWith(['country','currency'])->....

也试过这个

Country::find()->joinWith(['country','currency as cur'])->....

如何为第二关系指定别名?

解决方法

您可以为特定关系提供别名,如下所示:

->joinWith([
    'country','currency' => function ($q) {
        $q->from(Country::tableName() . ' cur');
    }
])

请参阅此主题以获取更多详细信息 – https://github.com/yiisoft/yii2/issues/2377#issuecomment-34573765

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读