Engine_Api::_()->getTable 返回的内容



我想知道它是通过调用getTable('tbl','module'(返回的数组还是对象。当我尝试将返回的值插入另一个数组然后将其编码为 JSON 时,我遇到了问题。将其本身传递给$this->_helper->json()会成功生成 JSON 输出,但将其用作另一个数组的一部分不起作用,并且是一个空数组。有关控制器代码,请参见下文:

//put your code here
public function indexAction () {
}
public function browseAction () {
$resultArray = $this->prepareArray();
$this->sendResponse($resultArray);
}
/**
* HTTP Response Codes
*/
const HTTP_OK = 200;
public function sendResponse($data) {
ob_clean();
$this->_helper->json($data);
}
public function prepareArray() {
//Here we will prepare the array to be later encoded as JSON
$responseArray = [
'status_code' => '',
'body'       => [
'itemsCount' => '',
'foods' =>  [
'food_id' => '',
'food_title' => '', 
'image' => '',
],
],
];
$foodTable = Engine_Api::_()->getDbTable('foods', 'restapi');
$foods = $foodTable->getFoods($this->view);
$foodArray = [];
print_r($foods);
while ($row = mysqli_fetch_row($foods)) {
$foodArray['food_id'] = $row['food_id'];
$foodArray['food_title'] = $row['title'];
$foodArray['image'] = $row['image'];  
}
error_log( $row ['food_id'] . ',' . $row['title']);            
$responseArray['status_code'] = '200';
$responseArray['body']['itemsCount'] = count($foods);        
$responseArray['body']['foods']= $foodsArray;
return $responseArray;
}

如果一切设置正确,getTable('tbl', 'module')返回object(Module_Model_DbTable_Tbl)。这是数据库表的模型。

您的特定错误位于您分配foodsArray未在任何地方定义的变量的行上。您应该在此处分配foodArray

$responseArray['body']['foods']= $foodsArray; // should be foodArray

最新更新