在Yii中找到它后打印一行

时间:2014-04-22 09:10:27

标签: php yii

我在表中有一个名为Object的模型,其中包含一列object_type。我想在表中显示object_type = journal的所有记录,但是我收到错误“Undefined index:object_name”。我怎么称呼记录?

<tbody>
        <?php
            $type='journal';
            $criteria = new CDbCriteria();
            $criteria->condition = "object_type =:object_type";
            $criteria->params = array(':object_type' => $type);
            $results = Object::model()->findAll($criteria);

            foreach ($results as $value);
            {
            echo '
                <tr>
                    <td>'.$results['object_name'].'</td>
                    <td>'.$results['object_publisher'].'</td>
                </tr>';
            }
        ?> 

以下工作代码

<?php
        $type='journal';
        $criteria = new CDbCriteria();
        $criteria->condition = "object_type =:object_type";
        $criteria->params = array(':object_type' => $type);
        $results = Object::model()->findAll($criteria);

        foreach ($results as $value)
        {
            echo '
                <tr>
                    <td>'.$value->object_name.'</td>
                    <td>'.$value->object_publisher_name.'</td>
                </tr>';
        }
    ?> 

2 个答案:

答案 0 :(得分:1)

方法findAll返回CActiveRecord数组link

像这样使用:

$value->object_name
$value->object_publisher

答案 1 :(得分:-1)

您希望$value是数组,但它是对象。使用$results->object_name