简体   繁体   English

Yii 模型关系不起作用

[英]Yii model relation not working

i have got 2 table where i have to relation between this two我有 2 个表,我必须将这两个表联系起来

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

in "bio" model i write like this在“生物”模型中,我是这样写的

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

and in "preceptor" model i write like this在“导师”模型中,我是这样写的

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']);
}

But my problem is when i show it in bio view it only show "Preceptor_ID" value in all 3 field so how can i show both "Preceptor_ID1" & "Preceptor_ID2"但我的问题是,当我在生物视图中显示它时,它只在所有 3 个字段中显示“Preceptor_ID”值,所以我如何同时显示“Preceptor_ID1”和“Preceptor_ID2”

In your bio model you specify the same relations when requesting the name for differing preceptors.在您的生物模型中,您在请求不同导师的名称时指定相同的关系。 It should be like below:它应该如下所示:

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

This way of requesting data is very redundant.这种请求数据的方式是非常多余的。 You should just define the relation and request the name in the view like $bio->preceptorID#->name .您应该只定义关系并在视图中请求名称,例如$bio->preceptorID#->name

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

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