我试图弄清楚如何使用JSON通过Ajax显示一些元素。我遵循此Using ajax in zend framework 2,everting正常运行,但现在想通过JSON传递例如10个对象
public function indexAction()
{
$result = new JsonModel (array(
'fototable' => $this->FotoTable->getFotos()
)
);
return $result;
}
然后在js中循环并显示数据。问题是如果我执行上面的代码。我没有得到任何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);
}
}
最佳答案
在
public function getFotos()
{
$select = new Select();
$select->from(self::$tableName);
$resultSet = $this->selectWith($select);
$resultSet->buffer();
return $resultSet;
}
您返回的结果集不是数组。你可以做
return $resultSet->toArray();
有时无法使用数组,因此可以将resultSet迭代到数组
$array = array();
foreach ($resultSet as $row) {
$array[] = $row
}
return $array;