2015-03-19 66 views
1

我试图从两个相关的模型中获取信息,显示在一个视图中。在Yii2中显示相同视图的两个模型

因此,我试图完成的是有索引视图来显示人员列表,如果我然后详细查看该特定人员,我想要一个与该人员相关的属性列表出现。

我有数据库设置,所以当我创建一个新人时,一个默认行被插入属性表中,该人员的名字叫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']); 
    } 
} 

我已经为这两个模型的生成CRUD通过GII。

我想知道的是如何设置我的人员控制器和人员查看,以便这可以正常工作。

回想一下,我的index.php视图将只显示人员列表,如果存在记录,您可以查看该特定记录,如果您查看记录 - 这将是view.php文件,我想以显示该特定人的属性(这些将是默认值),其中该人的ID与属性表中的person_id相同

然后用户将能够更新与该人有关的属性。

亲切的问候。

回答

0

下面的例子:

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

0

要在视图中显示的相关信息,你得到预先加载的最佳性能。我会提供一个例子:

public function actionView($id) 
{ 
    $model = Person::find() 
     ->where(['id' => $id]) 
     ->with('id0') 
     ->one(); 

    return $this->render('view', [ 
      'model' => $model, 
    ]); 
} 

现在我明白了,你在角色模型关系称为getId0,你可以为可读性的变化,要getAttribs(),并更改为->with('attribs')但是,这只是题外话:)

编辑:为@soju评论,属性是不可能的,因为关系名称来使用,这就是为什么GII给了它的名字getId0。属性或更多信息可能会有助于提高可读性。

如果你想显示在插件的结果,如GridView控件或ListView,你可以在这里遵循的指导:

http://www.ramirezcobos.com/2014/04/16/displaying-sorting-and-filtering-model-relations-on-a-gridview-yii2/

EDIT2:为@soju评论,指南是可能过时。请阅读官方文件。 http://www.yiiframework.com/doc-2.0/guide-output-data-widgets.html#working-with-model-relations

如果你想创建自己的观点,你可以用$model->id0->haircolor访问值,或者,如果重命名的关系,$model->attribs->haircolor就像你会任何其他属性。

记住:使用的GridView/ListView控件从数据库显示的时候,像'attributes.eyecolor',但$model->id0需要从模型中关系的名字,而在前面的“得到”,以更低的情况下,需要表名。

+0

使用'属性'作为关系名是不可能的,并且链接教程已过时,官方文档更好http://www.yiiframework.com/doc-2.0/guide-output-data-widgets.html#working -with-model-relations – soju 2015-03-19 14:31:24

+0

谢谢。我会更新我的答案 – 2015-03-19 14:33:38

相关问题