Rosti Rosti - 19 days ago 4
PHP Question

Make join from 2 tables in Active Data Provider

I have 2 tables:User and Posts. User can have many posts,post can't have many users. How to build relations in that models and how to make Join in ActiveDataProvider I have user_id in my Posts table and want to show data in my gridview like Posts(id,title,text) and User(name) how can I do that?I need to make relations in my model and how can I use it?;
Posts model:

<?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']);
}
}


User model:

* @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']);
}
}

Answer

You already have a relation (your function getPost ) in User model between User and Post

you can access the the value of Post eg:

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

$myUserPost  = $userModel->post;

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

for ActiveDataProvider you can use

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

and eventually add getter for single attribute in User Model eg:

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

so you can easly use this getter in a gridview

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