0

I am new in Zend Framework. I am trying to display data from database using JSON. And I encoded the data and passed it to JQuery. But cannot retrieve value from database. Data displayed as "undefined". My controller function is as follows:

 public function displayAction() 
 {
    $data1 = array();
    $request = $this->getRequest();
    $response = $this->getResponse();
    if ($request->isPost()) {
       $response->setContent(\Zend\Json\Json::encode(array('data' => $this-> getStickyNotesTable() -> fetchAll())));
}
    return $response; 
}

My FetchAll() is:

 public function fetchAll() {
    $resultSet = $this->select(function (Select $select) {
                $select->order('created ASC');
            });
    $entities = array();
    foreach ($resultSet as $row) {
        $entity = new Entity\StickyNote();
        $entity->setId($row->id)
                ->setNote($row->note)
                ->setCreated($row->created);
        $entities[] = $entity;
    }
    return $entities;

}

JQuery function :

function getUserList(element) {

$('#indicator').show();

$.post('stickynotes/display',
    function(data, textStatus) {
        renderUserList(data);
        $('#indicator').hide();
    }, 
    "json"      
);

}

function renderUserList(jsonData) {
var table = '<table width="600" cellpadding="5" class="table table-hover table-bordered"><thead><tr><th scope="col">Note</th></tr></thead><tbody>';

$.each(jsonData, function(index, data){    
    table += '<tr>';
    table += '<td class="edit" field="note" user_id="'+data.id+'">'+data.note+'</td>';
    table += '<td><a href="javascript:void(0);" user_id="'+data.id+'" class="delete_confirm btn btn-danger"><i class="icon-remove icon-white"></i></a></td>';
    table += '</tr>';
    });

table += '</tbody></table>';

$('div#content').html(table);
}

I tested it using Firebug. It shows

{"data":[{},{},{},{},{},{},{},{},{},{},{},{},{}]} 

as Response. Anyone please help me. Thanks.

2 Answers 2

2

The issue is with your fetchAll method. Try with this updated version:

public function fetchAll() {
    $resultSet = $this->select(function (Select $select) {
        $select->order('created ASC');
    });
    $entities = array();
    foreach ($resultSet as $row) {
        $entity = array(
            "id"      => $row->id,
            "note"    => $row->note,
            "created" => $row->created
        );
        $entities[] = $entity;
    }
    return $entities; 
}
Sign up to request clarification or add additional context in comments.

1 Comment

Many many thanks. This displays the data correctly in Response in firebug. But cannot display data in the table. It always shows "undefined". Any mistake in my JQuery function. Please help me.
2

You'll need to configure your module.config.php and add a strategy within your template_map add.

'strategies' => array(
        'ViewJsonStrategy',
    ),

to return a jsonModel.

If you want to work with a jsonModel within your controller you'll need to call it like so:

    $json = new JsonModel(array(
    'param'   => 'foobar',
    'success' => true,
    ));

    return $json;

3 Comments

Thanks for the suggestion. I added this, but no improvement.
I directly add some data to an array and assign that array to Json:encode function. Then it displays the data. But it cannot display data from database. Any suggestions?
You should look into your fetch all like mihaidoru suggested. I totally missed that part and tought it was the strategy. You should look into that! Are you by chance doing a tutorial? bigemployee.com/… this one by chance?

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.