我这里有一些错误我想使用嵌套foreach
合并来自 2 个查询的数据,并将数据保存在名为 result 的数组中,但这些代码部分Trying to get property of non-object
错误:'id_product'=>$detail_result->ID
,谁能帮我?
我还是新手使用Yii框架PHP
这是我的代码
$result = 数组((;
$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,
);
}
}
有人可以帮忙吗?谢谢
问题在于您访问$detail_result
变量,
函数 queryAll()
将带回数组数组,而不是对象数组!
将您的代码更改为以下内容:
$result [] = array(
'tanggal'=>$entry['date_time'],
'id_product'=>$detail_result['ID'],
'total'=>$detail_result['total'],
...
);
(顺便说一句,你可以var_dump你的变量来准确查看它包含的内容(