加入表格和显示

时间:2016-01-25 12:36:15

标签: mysql listview yii2

我正在使用Yii PHP框架,试图从两个都有一个名为comment_id的列的表中显示user_comment和user_reply。

这是评论表

CREATE TABLE `comment` (
  `comment_id` int(11) NOT NULL,
  `byy` varchar(30) NOT NULL,
  `user_comment` varchar(900) NOT NULL,
  `topic_id` varchar(30) NOT NULL,
  `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `up` bigint(11) NOT NULL,
  `down` bigint(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

这是回复表

CREATE TABLE `reply` (
  `reply_id` int(11) NOT NULL,
  `comment_id` int(19) NOT NULL,
  `byy` varchar(29) NOT NULL,
  `user_reply` varchar(29) NOT NULL,
  `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `up` varchar(29) NOT NULL,
  `down` varchar(29) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

我希望以列表视图格式显示user_comment和user_reply,并根据comment_id进行排序。我遇到了问题。

  <?php
    $querys = new Query;
    $querys->select([
        'comment.user_comment', 
        'reply.user_reply']
        )  
      ->from('comment')
      ->join('LEFT OUTER JOIN', 'reply',
            'reply.comment_id =comment.comment_id') ; 
     $command = $querys->createCommand();
    $data = $command->queryAll(); 

    $queryd = new Query;
    $queryd->select([
        'comment.user_comment', 
        'reply.user_reply']
        )  
      ->from('comment')
      ->join('LEFT OUTER JOIN', 'reply',
            'reply.comment_id =comment.comment_id')    
      ->LIMIT(5); 
     $command = $queryd->createCommand();
    $data = $command->queryAll();  


 foreach ($data as $detail) {
        $dataArr[] = array(
            'user_reply' => $detail['user_reply']
        );
      }
          $dataProvider = new ArrayDataProvider([
                'allModels' => $dataArr,
                'pagination' => [
                'pageSize' => 10,
                ],
                'sort' => [
                    'attributes' => ['comment_created_at'],
                ],
            ]);
          echo ListView::widget([
             'dataProvider' => $dataProvider,
             'itemOptions' => ['class' => 'comment-item'],
             'itemView' => 'adapter',

        ]);
    ?>

适配器类

<?php
use yii\helpers\Html;
use yii\helpers\HtmlPurifier;

//var_dump($model);exit;
?>

              <div class="col-lg-12 col-sm-12">
            <section>

            <?php echo $model['user_reply']; ?>


            </section>
          </div>

我收到此错误:

  

获取未知属性:未定义索引:user_reply

我做错了什么?

1 个答案:

答案 0 :(得分:0)

尝试

foreach ($data as $detail) {
        $dataArr[] = array(
            'user_reply' => $detail['reply.user_reply']
        );
      }

或使用别名

$queryd = new Query;
$queryd->select('comment.user_comment as user_comment, 
                reply.user_reply as user_reply')  
->from('comment')
->join('LEFT OUTER JOIN', 'reply',
    'reply.comment_id =comment.comment_id')    
 ->LIMIT(5); 
 $command = $queryd->createCommand();
 $data = $command->queryAll();  

 foreach ($data as $detail) {
        $dataArr[] = array(
            'user_reply' => $detail['user_reply']
        );
      }

对于dataProvider,您必须记住有一个模型集合,然后

<div class="col-lg-12 col-sm-12">
    <section>

      <?php  
          foreach($models as $model){
               //echo $model['user_reply']; 
                echo $model->user_reply; 
          }
        ?>

     </section>
 </div>