如何使用 yii 数组

how to use yii array

伙计们,我正在使用 yii2 advance 我在其中遇到了一些小问题我有 2 table 我必须在这两者之间建立关系

       ---------------------               -----------------
       |      roleedu      |               |     bio       |
       |-------------------|               |---------------|
       | Roleedu_id        |               | bio_id        |
       | Roleedu_position  |               | Roleedu_id    |
       ---------------------               -----------------
               |                                    |
               |                                    |
               |                                    |
           roleedu                                 bio
-----------------------------------    -------------|---------------
|  Roleedu_id  | Roleedu_position |    |   bio_id   | Roleedu_id   |
-----------------------------------    -------------|---------------
|       1      |      cat         |    |      1     |    2,3       |
|       2      |      bat         |    |      2     |    1,2,3     |
|       3      |      ox          |    |      3     |    4,3       |
|       4      |      crow        |    |      4     |    4,3,2     |
-----------------------------------    -----------------------------

我的 "bio" 型号是

public function getRoleedu(){
    return $this->hasOne(Roleedu::className(), ['Roleedu_id' => 'Roleedu_id']);
}
public function getRole(){
    return $this->roleedu->Roleedu_position;
}

我的 "roleedu" 型号是

public function getBios(){
    return $this->hasMany(Bio::className(), ['Roleedu_id' => 'Roleedu_id']);
}

我的观点

<?= DetailView::widget([
    'model' => $model,
    'attributes' => [
        'bio_id',
        'Role',
    ],
]) ?>

我看的时候是这样的

bio_id = 4
Role = crow

但我想像这样展示 ex

bio_id = 4
Role  = crow,ox,bat

这怎么可能??有什么建议吗? 提前谢谢大家^^

简介 table 中的专栏 Roleedu_id 不得不惨死。这是设计数据库的糟糕方法,这就是您遇到问题的原因。实际上,table 和

之间完全没有已知关系

如果你真的坚持使用如此糟糕的设计,你的角色功能应该是

public function getRole(){
     return implode(',', ArrayHelper::map(Roleedu::find()->where('Roleedu_id IN ('.$this->Roleedu_id.')')->asArray()->all(), 'Roleedu_position', 'Roleedu_position'));
}

该代码未经测试,但它应该与您想要的相当接近。 删除您设计的任何其他关系,而您没有任何关系。