我的控制器功能有以下代码:
$this->loadModel('Cardetail');
$car_info_oneway= $this->Cardetail->query("Select * from cardetails as c INNER JOIN sellers as s ON c.seller_id=s.id where c.id='$car_id_oneway'");
我想要做的是在我的视图中读取结果集的一列的值。但是,每次我收到错误" Undefined offset 0。"这是我的代码中的一部分。 :
$count = $car_info_oneway[0]['c']['total_number_of_seats'];
这可能是什么问题?
答案 0 :(得分:2)
您为什么使用query()
?您应该使用Cake的find()
方法使用contain
检索您的数据以检索卖家数据(您需要确保您的Cardetail
和Seller
模型正确关联) 。
我假设你只是想要检索单个Cardetail
记录,所以应该使用find('first')
: -
$this->loadModel('Cardetail');
$car_info_oneway = $this->Cardetail->find('first', [
'contain' => ['Seller'],
'conditions' => [
'Cardetail.id' => $car_id_oneway
]
]);
使用find('first')
时,返回的数组不会被数字索引,因此$count
将是: -
$count = $car_info_oneway['Cardetail']['total_number_of_seats'];
如果您遇到问题,可以使用debug($car_info_oneway)
检查返回的数组。