从Active Data Provider中的2个表进行连接

时间:2016-11-16 18:31:32

标签: php yii yii2

我有2个表:用户和帖子。用户可以有很多帖子,帖子可以有很多用户。如何在该模型中建立关系以及如何在ActiveDataProvider中进行加入我在Posts表中有user_id并希望在我的gridview中显示数据,如Posts(id,title,text)和User(name)我该怎么做?需要在我的模型中建立关系,我该如何使用它? 帖子模型:

<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "posts".
 *
 * @property integer $id
 * @property integer $user_id
 * @property string $post_title
 * @property string $post_text
 */
class Posts extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'posts';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['user_id'], 'integer'],
            [['post_title'], 'string', 'max' => 50],
            [['post_text'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'user_id' => 'User ID',
            'post_title' => 'Post Title',
            'post_text' => 'Post Text',
        ];
    }
    public function insertPost()
    {
        $userId = \Yii::$app->user->identity->id;
        $posts = new Posts();
        $posts->user_id = $userId;
        $posts->post_title = $this->post_title;
        $posts->post_text = $this->post_text;
        return $posts->save();
    }
    public function getUser()
    {
        return $this->hasOne(User::classname(),['user_id'=>'id']);
    }
}

用户模型:

* @property integer $id
 * @property string $email
 * @property string $password
 * @property string $name
 */
class User extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'user';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['email'], 'string', 'max' => 100],
            [['password'], 'string', 'max' => 255],
            [['name'], 'string', 'max' => 25],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'email' => 'Email',
            'password' => 'Password',
            'name' => 'Name',
        ];
    }
    public function setPassword($password)
    {
        $this->password = sha1($password);
    }
    public function validatePassword($password)
    {
        return $this->password === sha1($password);
    }
    public static function findIdentity($id)
    {
        return self::findOne($id);
    }
    public static function findIdentityByAccessToken($token, $type = null)
    {

    }
    public function getId()
    {
        return $this->id;
    }
    public function getAuthKey()
    {

    }
    public function validateAuthKey($authKey)
    {

    }
    public function getPost()
    {
        return $this->hasMany(Posts::classname(),['id'=>'user_id']);
    }
}

1 个答案:

答案 0 :(得分:1)

您已在User和Post

之间的用户模型中拥有关系(您的函数getPost)

您可以访问Post的值,例如:

$userModel = User::find()->where([ 'id' => $id])->one();

$myUserPost  = $userModel->post;

$myUserPostAttribute = $userModel->post->attribute;

对于ActiveDataProvider,您可以使用

$dataProvider = User::find()->where([ 'id' => $id]);

并最终在用户模型中为单个属性添加getter,例如:

getMyPostAttribute1()
{
    return $this->post->attribute1
}

所以你可以在gridview中轻松使用这个getter

    <?= GridView::widget([
          'dataProvider' => $dataProvider,
          ......
          'columns' => [
          ['class' => 'yii\grid\SerialColumn'],
          'myPostAttribute1',
          ....