【问题标题】:Two Models Displaying to Same View in Yii2在 Yii2 中显示相同视图的两个模型
【发布时间】:2015-03-19 12:24:20
【问题描述】:

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

所以我想要完成的是让索引视图显示人员列表,如果我随后进入该特定人员的详细视图,我希望显示与该人员相关的属性列表。

我有数据库设置,这样当我创建一个新人时,一个默认行会插入到属性表中,该行的人的 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 与属性表中的 person_id 相同

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

亲切的问候。

【问题讨论】:

    标签: php model-view-controller view controller yii2


    【解决方案1】:

    这里是一个例子:

    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

    【讨论】:

      【解决方案2】:

      要在视图中显示相关信息,您可以通过预先加载获得最佳性能。我举个例子:

      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,您可以按照此处的指南进行操作:

      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 在显示时需要来自 db 的表名,例如 'attributes.eyecolor',但 $model->id0 需要来自模型的关系名称,没有“get”前面,小写。

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-22
      • 1970-01-01
      • 2015-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多