Engine_Api::_()->getTable 返回什么

what is returned by 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') returns object(Module_Model_DbTable_Tbl)。这是你的数据库的模型 table.

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

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