[英]Two Models Displaying to Same View in Yii2
我正在嘗試從一個視圖中顯示的兩個相關模型中獲取信息。
因此,我要完成的工作是使索引視圖顯示人員列表,如果接下來進入該特定人員的詳細視圖,我希望顯示與該人員相關的屬性列表。
我有數據庫設置,以便在創建新人員時將默認行插入到屬性表中,其中該人員的ID在名為person_id的列下。
看我的兩個模型課
人:
class People extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'people';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['dob', 'CURDATE'], 'safe'],
[['age'], 'integer'],
[['firstname', 'surname'], 'string', 'max' => 50]
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'firstname' => 'Firstname',
'surname' => 'Surname',
'dob' => 'Dob',
'age' => 'Age',
'CURDATE' => 'Curdate',
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getId0()
{
return $this->hasOne(Attributes::className(), ['person_id' => 'id']);
}
}
屬性:
class Attributes extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'attributes';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['haircolor', 'eyecolor', 'weight', 'height', 'person_id'], 'required'],
[['weight', 'height', 'person_id'], 'integer'],
[['haircolor', 'eyecolor'], 'string', 'max' => 50]
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'haircolor' => 'Haircolor',
'eyecolor' => 'Eyecolor',
'weight' => 'Weight',
'height' => 'Height',
'person_id' => 'Person ID',
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getPeople()
{
return $this->hasOne(People::className(), ['id' => 'person_id']);
}
}
我已經通過Gii為這兩種模型生成了CRUD。
我想知道的是如何設置我的人員控制器和人員視圖,以便它可以正常工作。
回顧一下,我的index.php視圖將僅顯示人員列表,如果存在記錄,則可以查看該特定記錄,如果您查看該記錄-這將是view.php文件,我想顯示屬性該特定人員的ID(這些值為默認值),該人員的ID與屬性表中的person_id相同
然后,用戶將能夠更新與該人有關的屬性。
親切的問候。
這是一個例子:
public function actionCreate()
{
$user = new User;
$profile = new Profile;
if ($user->load(Yii::$app->request->post()) && $profile->load(Yii::$app->request->post()) && Model::validateMultiple([$user, $profile])) {
$user->save(false); // skip validation as model is already validated
$profile->user_id = $user->id; // no need for validation rule on user_id as you set it yourself
$profile-save(false);
return $this->redirect(['view', 'id' => $user->id]);
} else {
return $this->render('create', [
'user' => $user,
'profile' => $profile,
]);
}
}
更多信息:
http://www.yiiframework.com/forum/index.php/topic/53935-solved-subforms/page__p__248184
http://www.yiiframework.com/doc-2.0/guide-input-tabular-input.html
要在視圖中顯示相關信息,急切加載將獲得最佳性能。 我將提供一個示例:
public function actionView($id)
{
$model = Person::find()
->where(['id' => $id])
->with('id0')
->one();
return $this->render('view', [
'model' => $model,
]);
}
現在,我看到您在Person Model中的關系稱為getId0,您可以將可讀性更改為getAttribs(),然后更改為->with('attribs')
但這只是題外話:)
編輯: 正如@soju所評論的那樣,屬性不能用作關系名稱,這就是為什么gii給它指定了名稱getId0。 屬性或更多有用的信息可能有助於提高可讀性。
如果要在GridView或ListView之類的小部件中顯示結果,則可以按照以下指南進行操作:
EDIT2: 正如@soju所說,指南可能已過時。 還要閱讀官方文件。 http://www.yiiframework.com/doc-2.0/guide-output-data-widgets.html#working-with-model-relations
如果要創建自己的視圖,則可以使用$model->id0->haircolor
來訪問值,或者,如果重命名關系, $model->attribs->haircolor
可以像其他任何屬性一樣$model->attribs->haircolor
。
記住:使用GridView / ListView在顯示時需要數據庫中的表名,例如'attributes.eyecolor'
,但是$model->id0
需要$model->id0
的關系名,前面沒有'get'並且小寫。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.