从活动数据提供程序中的2个表进行连接


Make join from 2 tables in Active Data Provider

我有2个表:User和Posts。用户可以有多个帖子,帖子不能有多个用户。如何在模型中建立关系以及如何在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']);
    }
}

在User模型中已经有了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',
          ....