簡體   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