使用连接查询时非法字符串偏移 'id'

Illegal string offset 'id' while using join query

我在 Yii 中遇到了一些问题 framework.I 需要将连接查询结果显示到我的视图页面 pdtview。但是我在显示结果 array.Here 是我的 controller page code.

public function actionPdtview()
   {
        $model=new Products;
        $models=Products::model()->findAll();

        $rows = Yii::app()->db->createCommand()
        ->select('category.ctg,category.cid, products.*')
        ->from('category')
        ->join('products','category.cid = products.cid')
        ->queryRow();
//    print_r($rows);die;
            $this->render('pdtview',array('model'=>$rows));

       }

并查看下面给出的页面代码

<table>
<tr>
    <th>sl.no</th>
    <th>Category</th>
    <th>Product</th>
</tr>


 <?php 
//    print_r($model);die;
      foreach($model as $row)
      {
    ?>
    <tr>
        <td><?php echo $row['id']; ?></td>
        <td><?php echo $row['ctg']; ?></td>
        <td><?php echo $row['pdt']; ?></td>
    </tr>
    <?php
      }
      ?>
</table>

我正在尝试使用 print_r() 显示查询结果,然后以 table 格式更正输出 obtained.But,我没有得到相同的 result.Only错误:Illegal string offsets 'id'。请提前帮我解决这个问题problem.Thanks。

print_r($model) 输出(来自评论):

Array ( 
    [ctg] => Flowers 
    [cid] => 1 
    [id] => 1 
    [pdt] => lilly 
)  

我想你想使用 queryAll 因为 queryRow 只获取第一行。

public function actionPdtview()
   {
        $model=new Products;
        $models=Products::model()->findAll();

        $rows = Yii::app()->db->createCommand()
        ->select('category.ctg,category.cid, products.*')
        ->from('category')
        ->join('products','category.cid = products.cid')
        ->queryAll();
        //  print_r($rows);die;
            $this->render('pdtview',array('model'=>$rows));

       }

这可能 return 一个对象数组,您可以这样访问:

    <tr>
        <td><?php echo $row->id; ?></td>
        <td><?php echo $row->ctg; ?></td>
        <td><?php echo $row->pdt; ?></td>
    </tr>