我很抱歉我的英语。所以,我有3个表有很多关系。
此代码在视图文件中显示属性:
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'scientist_id',
'scientist_name',
'scientist_surname',
'scientist_patronymic',
'scientist_birthdate',
'scientist_email:email',
'scientist_phone',
'scientist_photo',
'scientist_status',
'scientist_job:ntext',
'scientist_additional_information:ntext',
'field_id', //display field but no data
],
]) ?>
所以我需要在&#34; field_id&#34; s&#34;哪个对应&#34;科学家_id&#34;来自SUMMARY_FIELD表。我怎么能这样做?
与科学家模型中的表格的关系:
public function getSummaryFields()
{
return $this->hasMany(SummaryField::className(), ['scientist_id' => 'scientist_id']);
}
public function getFields()
{
return $this->hasMany(Field::className(), ['field_id' => 'field_id'])->viaTable('summary_field', ['scientist_id' => 'scientist_id']);
}
SummaryField模型中的关系:
public function getField()
{
return $this->hasOne(Field::className(), ['field_id' => 'field_id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getScientist()
{
return $this->hasOne(Scientist::className(), ['scientist_id' => 'scientist_id']);
}
答案 0 :(得分:2)
在模型中创建函数:
function getFieldId($model)
{
$string = '';
foreach ($model->summaryFields as $cat) {
$string .= $cat->field_id . " ";
}
return $string;
}
使用$model->functionName()
:
<?= DetailView::widget([
'model' => $model,
'attributes' => [
[
'attribute'=>'field_id',
'value' => $model->getFieldId($data),
],
],
]); ?>