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

时间:2018-07-07 07:29:11

标签: socialengine

我想知道它是否是通过调用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;
}

1 个答案:

答案 0 :(得分:1)

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

您要分配给foodsArray未在任何地方定义的变量的行上是您的特定错误。您应该在这里分配foodArray

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