我试图弄清楚如何使用 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();
有时 to array 不可用,因此您可以将 resultSet 迭代到数组
$array = array();
foreach ($resultSet as $row) {
$array[] = $row
}
return $array;
关于php - Zend 2 Ajax JSON 多数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27076251/