繁体   English   中英

Yii 模型关系不起作用

[英]Yii model relation not working

我有 2 个表,我必须将这两个表联系起来

-------------------           -----------------
|    preceptor    |           |       bio     |
|-----------------|           |---------------|
| Preceptor_id    |           | bio_id        |
| Preceptor_name  |           | Preceptor_ID  |
| wat_id          |           | Preceptor_ID1 |
-------------------           | Preceptor_ID2 |
                              -----------------

在“生物”模型中,我是这样写的

public function getPreceptor(){
    return $this->hasOne(Preceptor::className(),['Preceptor_id'=>'Preceptor_ID']);
}
public function getPreceptorName(){
    return $this->preceptor->Preceptor_name;
}

public function getPreceptorID1(){
    return $this->hasOne(Preceptor::className(),['Preceptor_id'=>'Preceptor_ID1']);
}
public function getPreceptorID1Name(){
    return $this->preceptor->Preceptor_name;
}

public function getPreceptorID2(){
    return $this->hasOne(Preceptor::className(),['Preceptor_id'=>'Preceptor_ID2']);
}
public function getPreceptorID2Name(){
    return $this->preceptor->Preceptor_name;
}

在“导师”模型中,我是这样写的

public function getBio(){
    return $this->hasOne(Bio::className(), ['Preceptor_ID' => 'Preceptor_id']);
}

public function getBios0(){
    return $this->hasOne(Bio::className(), ['Preceptor_ID1' => 'Preceptor_id']);
}

public function getBios1(){
    return $this->hasOne(Bio::className(), ['Preceptor_ID2' => 'Preceptor_id']);
}

但我的问题是,当我在生物视图中显示它时,它只在所有 3 个字段中显示“Preceptor_ID”值,所以我如何同时显示“Preceptor_ID1”和“Preceptor_ID2”

在您的生物模型中,您在请求不同导师的名称时指定相同的关系。 它应该如下所示:

public function getPreceptorID#Name() {
    return $this->preceptorID#->name;
}

这种请求数据的方式是非常多余的。 您应该只定义关系并在视图中请求名称,例如$bio->preceptorID#->name

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM