2016-11-16 7 views
1

私は2つのテーブルを持っています:ユーザと投稿。ユーザーは多くの投稿を持つことができ、投稿は多くのユーザーを持つことができません。そのモデルで関係を構築する方法とActiveDataProviderで結合する方法Postsテーブルにuser_idがあり、Post(id、title、text)やUser(name)のようにgridviewにデータを表示するにはどうしたらいいですか?私のモデルで関係を作る必要があり、どうすればそれを使うことができますか? 投稿モデル:アクティブデータプロバイダの2つのテーブルからの結合を作成する

<?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モデル:

* @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

あなたは既にユーザーとポスト

間のUserモデルにおける関係(あなたの関数でgetPost)を持っているあなたはポストの値にアクセスすることができます例:

$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の私はそれとActiveDataProviderを使用する方法を

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

でこのゲッターを使用することができますか? – Rosti

+0

回答を更新しました – scaisEdge

関連する問題