Yii模型关系不起作用


Yii model relation not working

我有两个表,我必须在其中建立这两个之间的关系

-------------------           -----------------
|    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"值,所以我如何同时显示"Preciptor_ID1"&"Preceptor_ID2"

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

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

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