Yii 模型关系不起作用

Yii model relation not working

我有 2 table 我必须在这两者之间建立关系

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

在"bio"模型中我这样写

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;
}

在 "preceptor" 模型中我这样写

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.