YII PHP嵌套的foreach错误

时间:2015-03-29 07:37:14

标签: php yii foreach frameworks nested

我在这里有一些错误 我想使用嵌套的foreach组合来自2个查询的数据并将数据保存在名为result的数组中,但这些部分代码中存在错误Trying to get property of non-object'id_product'=>$detail_result->ID, 有谁可以帮助我?

我仍然使用yii framework php

这是我的代码

$ result = array();

        $criteria = new CDbCriteria;
        $criteria->condition = 'date_time >= :start and date_time <= :end';
        $criteria->order = 'date_time';
        $criteria->params = array(':start' => $_POST['tanggal']['start'] ,':end' => $_POST['tanggal']['end']);

        $checkIn = CheckIn::model()->findAll($criteria);

        foreach($checkIn as $entry) {
            $sql = "select * from check_in_detail where ID_check_in = $entry->ID";
            $detail_results = Yii::app()->db->createCommand($sql)->queryAll();

            foreach($detail_results as $detail_result)
            {
                 $result [] = array(
                     'tanggal'=>$entry->date_time,
                    'id_product'=>$detail_result->ID,
                     'total'=>$detail_result->total,
                     'id_distributor'=>$entry->iDDistributor->name,
                     'other'=>$entry->description,
                 );
             }
         }

有人可以帮忙吗? 感谢

1 个答案:

答案 0 :(得分:2)

问题在于您访问$detail_result变量

函数queryAll()会带回一个数组数组,而不是一个对象数组!

将您的代码更改为以下内容:

             $result [] = array(
                 'tanggal'=>$entry['date_time'],
                 'id_product'=>$detail_result['ID'],
                 'total'=>$detail_result['total'],
                 ...  
             );

(顺便说一句,你可以通过var_dump你的变量来确切看到它包含的内容)