1

I am trying to figure out how to display a few elements by Ajax using JSON. I followed this Using ajax in zend framework 2 and everting is working fine, but now want to pass for example 10 object by JSON

public function indexAction()
{   
    $result = new JsonModel (array(
        'fototable' => $this->FotoTable->getFotos()
        )
    );
    return $result;
}

and then loop in js and display the data. The problem is if I do above code. I do not get any Json output. I tried to serialize the data but i get the error that PDO statment can not be serialized.

Here is my Fototable class:

<?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);
    }
}
2
  • Can you also share Footable class signature with getFotos() method? Commented Nov 22, 2014 at 10:46
  • just edited the post Commented Nov 22, 2014 at 12:58

1 Answer 1

3

in

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

    return $resultSet;      
}

You return the Resultset which is not an array. You can either do

return $resultSet->toArray();

sometimes to array is not available so you can iterate over the resultSet to an array

$array = array();
foreach ($resultSet as $row) {
    $array[] = $row
}
return $array;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.