Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 两个模型在Yii2中显示到同一视图_Php_Model View Controller_View_Controller_Yii2 - Fatal编程技术网

Php 两个模型在Yii2中显示到同一视图

Php 两个模型在Yii2中显示到同一视图,php,model-view-controller,view,controller,yii2,Php,Model View Controller,View,Controller,Yii2,我试图从两个相关的模型中获取信息,显示在一个视图中 因此,我试图实现的是使用索引视图来显示人员列表,如果我进入特定人员的详细视图,我希望显示与该人员相关的属性列表 我有数据库设置,这样当我创建一个新的人物时,一个默认的行会插入到属性表中,在名为person\u id的列下插入人物id 看我的两个模型课 人民: class People extends \yii\db\ActiveRecord { /** * @inheritdoc */ public stat

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

因此,我试图实现的是使用索引视图来显示人员列表,如果我进入特定人员的详细视图,我希望显示与该人员相关的属性列表

我有数据库设置,这样当我创建一个新的人物时,一个默认的行会插入到属性表中,在名为person\u id的列下插入人物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相同

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

亲切问候。

这里有一个例子:

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,
        ]);
    }
}
更多信息:


要在视图中显示相关信息,您可以通过快速加载获得最佳性能。我将提供一个示例:

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

    return $this->render('view', [
            'model' => $model,
    ]);
}
现在我看到您的人际关系模型名为getId0,为了可读性,您可以将其更改为getAttribs,并使用“attribs”更改为->但这只是一个离题:

编辑:正如@soju所评论的,属性不能用作关系名称,这就是为什么gii给它命名为getId0。属性或其他更具信息性的内容有助于提高可读性

如果要在小部件(如GridView或ListView)中显示结果,可以按照以下指南操作:

编辑2:正如@soju所评论的,指南可能已经过时了。还要阅读官方文件。

如果要创建自己的视图,可以使用$model->id0->haircolor访问这些值,或者,如果重命名关系,$model->attribs->haircolor,就像其他属性一样

记住:使用GridView/ListView在显示时需要数据库中的表名,如“attributes.eyecolor”,但是$model->id0需要模型中的关系名,前面没有“get”,并且小写。

使用属性作为关系名是不可能的,而且链接教程已经过时,官方文档更好