phpyii2yii2-advanced-appyii-relations

Yii model relation not working


i have got 2 table where i have to relation between this two

-------------------           -----------------
|    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"


Solution

  • 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.