2014-11-22 67 views
1

我想弄清楚如何使用JSON顯示Ajax的幾個元素。我跟着此Using ajax in zend framework 2和外翻工作正常,但現在希望通過JSONZend 2 Ajax JSON多個數據

通過例如10目的在JS
public function indexAction() 
{ 
    $result = new JsonModel (array(
     'fototable' => $this->FotoTable->getFotos() 
     ) 
    ); 
    return $result; 
} 

,然後循環和顯示數據。問題是如果我做了上面的代碼。我沒有得到任何Json輸出。我試圖序列化數據,但是我得到的錯誤是無法序列化PDO陳述。

這裏是我Fototable類:

<?php 

namespace Media\Model; 

use Zend\Db\TableGateway\TableGateway; 
use Zend\Db\Sql; 
use Zend\Db\Sql\Select; 
use Zend\Db\Sql\Where; 
use Zend\Db\Adapter\Adapter; 

class FotoTable extends TableGateway{ 

    public static $tableName = 'media'; 

    public function addFoto(array $data) 
    { 
     return $this->insert($data); 
    } 

    public function updateFoto(array $insertData, $id) 
    { 
     return $this->update($insertData, array('id' => $id)); 
    } 

    public function getFotos() 
    { 
     $select = new Select(); 
     $select->from(self::$tableName); 
     $resultSet = $this->selectWith($select); 
     $resultSet->buffer(); 

     return $resultSet;  
    } 

    public function getFotoById($id) 
    { 
     $select = new Select(); 
     $select->from(self::$tableName); 
     $select->where(array('id' => $id)); 
     return $this->selectWith($select)->current(); 
    } 

    public function getFotosByObject($project) 
    { 
     $select = new Select(); 
     $select->from(self::$tableName); 
     $select->where(array('object_id' => $project)); 

     return $this->selectWith($select); 
    } 
} 
+0

你能還與getFotos()方法分享Footable類簽名? – edigu 2014-11-22 10:46:46

+0

剛剛編輯帖子 – user3544190 2014-11-22 12:58:56

回答

3

public function getFotos() 
{ 
    $select = new Select(); 
    $select->from(self::$tableName); 
    $resultSet = $this->selectWith($select); 
    $resultSet->buffer(); 

    return $resultSet;  
} 

您返回結果集是不是一個數組。您可以做

return $resultSet->toArray(); 

有時陣列不可用,所以你可以遍歷結果集到一個數組

$array = array(); 
foreach ($resultSet as $row) { 
    $array[] = $row 
} 
return $array;