yii2获取相关记录

时间:2016-01-09 19:07:51

标签: php mysql database yii2

我想使用listview小部件显示用户喜欢的帖子,因此dataprovider必须包含正确的记录,因为我不希望在我的视图中进行任何查询。

我可以使用以下视图访问帖子数据:

<?php foreach ($likedposts as $post) { ?>
    <?php var_dump($post->likedpost['title']); ?>
<?php } ?>

我的控制器如下:

public function actionIndex()
{

    $user_id = Yii::$app->user->id; 

    $likedposts =  Likedposts::find()->where([ 'user_id' => $user_id])->all();

    return $this->render('index', [
        'likedposts' => $likedposts,
    ]);
}

我希望能够访问数据,例如:

<?php foreach ($likedposts as $model) { ?>
    <?php var_dump($model->title); ?>
<?php } ?>

模式 用户
-id
-name
-email
-password

博客文章
-id
-title
-content

喜欢的帖子
-id
-post_id
-user_id

更新

下面的第一个变量我需要获得所有喜欢的帖子但是我只需要从表中选择post_id,因为它将在dataprovider中使用,然后将获得所有id属于preferpost变量的帖子(需要创建一个变量数组吗?)

    $likedposts =  Likedposts::find()->where([ 'user_id' => $user_id])->all();
    $dataProvider = Posts::find()->where(['id'] => $likedposts->post_id])->all();

更新2(如果只有一条/单条记录,则有效)

    $user_id = Yii::$app->user->id; 

    //if i can change ->one() to all() it breaks
    $likedposts =  Likedposts::find()->where([ 'user_id' => $user_id])->one();

    $DataProvider = new ActiveDataProvider([
            'query' => Posts::find()->
                where(['id' => $likedposts->post_id])->
                orderBy('id DESC'),
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);


    return $this->render('index', [
        'DataProvider' => $DataProvider,
    ]);

如果我使用all()

,我会收到错误

尝试获取非对象的属性

1 个答案:

答案 0 :(得分:0)

您可以将相关模型和dataProvider传递给视图..

我不知道你的代码所以我只能以一种简单的方式做一些建议

public function actionIndex()
{

   $user_id = Yii::$app->user->id; 
   $modellUser = User::find()->where(['id'=> $_user])->one();

  $modelBlogPost = BlogPost::find()->where(['user_id' => $user_id])->one();

  $searchModel = new \common\models\LikedpostsSearch();
  $dataProviderLikedposts= $searchModel->search(Yii::$app->request->queryParams);
  $dataProviderLikedposts->query->andWhere('User  = ' . $User_id );

    return $this->render('index', [
      'modelUser' => $modelUser,
      'modelBlogPost' =$modelBlogPost,
      'dataProviderLikedposts' => $dataProviderLikedposts,
    ]);
}

在你看来

你可以

创建一个:

 detailView with  the $modelUser  for user information

 <?=  DetailView::widget([
    'model' => $modelUser,
    'mode' => 'view',
    'attributes' => [
       .....
    ],
 ]) ?>

detailView with the $modelBlogPost for the blog post information

<?=  DetailView::widget([
    'model' => $modelBlogPost,
    'mode' => 'view',
    'attributes' => [
       .....
    ],
  ]) ?>

a

listView with the $dataProvideLikedpost for the likedpost information ..


<?= ListView::widget( [
  'dataProvider' => $dataProviderLikedposts,
  .....
 ] ); ?>

我希望这个元素可以为您提供正确的解决方案 如果问题仅与获取您需要的dataProvider相关

对于包含查询的dataProvider,您应该创建查询

$likedposts =  Likedposts::find()->where([ 'user_id' => $user_id])->all();

// only the query whitout all otherwise you obtain the models not the dataProvider
$query = Posts::find()->where(['id'] => $likedposts->post_id]);

使用您的查询

创建新的dataProvider
 $provider = new ActiveDataProvider([
 'query' => $query,
'pagination' => [
    'pageSize' => 20,
],

]);

然后在渲染视图中传递此'dataProvider' => $provider

更新所有()

我认为最后一条评论(使用one()而不使用all()与$ preferposts中的结果是模型集合这一事实有关。在这种情况下,likeposts-&gt; post_id don&t; t包含一个post_id数组但是是一个非对象(该对象将是$ likesposts [&#39; 0&#39;] - &gt; post_id)..

在您的查询中,您需要一个包含用户喜欢的post_id集的矢量/数组。

foreach ($likedposts as $key => $value) { 
    myArray[] = $value;
}


$query = Posts::find()->where( 'id IN ' . $myArray);

or 

$query = Posts::find()->where(['id'] => $myArray]);