Search code examples
socialengine

what is returned by Engine_Api::_()->getTable


I wonder if it is an array that is returned by calling getTable('tbl', 'module') or an object. I am facing problems when I try to insert the returned value into another array and then encode it as JSON. Passing it per se to $this->_helper->json() generates JSON output successfully, but using it as part of another array just does not work and is a null array. See below for the controller code:

//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;
}

Solution

  • If everything is set properly, getTable('tbl', 'module') returns object(Module_Model_DbTable_Tbl). This is the model of your database table.

    Your particular error is on the line where you're assigning foodsArray variable that isn't defined anywhere. You should have assign foodArray here instead.

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