Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

How to modify table::find() result data?


$list = table::find();
foreach($list as $k=>$v)
    $list[$k]->perms_txt = '2222';

view template:

{% for rs in list %}

echo perms_txt not value!

After each iteration in your code (foreach($list as $k=>$v)...) model will be cleared, because Resultset not stores all model objects. Resultset is storing only one current model for current iteration.

You can use something like that

$list = table::find();
$stored = array();
foreach($list as $v) {
    $v->perms_txt = '2222';

    $stored[] = $v;

$this->view->setVar('list', $stored);

but this is bad way, because you will store all records in memory.

edited Oct '14

page error!


    public function indexAction()
        $currentPage = $this->request->getQuery('page','int',0);
        $list = array();
        $result = Roles::find();
        foreach($result as $k=>$v)
            $v->perms_txt = '2222';
            $list[$k] = $v;
        $paginator = new \Phalcon\Paginator\Adapter\Model(
                "data" => $list,
                "limit"=> $this->page_limit,
                "page" => $currentPage
        $page = $paginator->getPaginate();
Phalcon\Paginator\Exception: Invalid data for paginator File=/data/www/ Line=26 #0 /data/www/ Phalcon\Paginator\Adapter\Model->getPaginate()
#1 [internal function]: RoleController->indexAction('0')
#2 [internal function]: Phalcon\Dispatcher->dispatch()
#3 /data/www/ Phalcon\Mvc\Application->handle()
#4 {main}

line 26: $page = $paginator->getPaginate();


$list is an array and not a resulset, you need to use Phalcon\Paginator\Adapter\NativeArray instead of Phalcon\Paginator\Adapter\Model


oh.thanks! use Phalcon\Paginator\Adapter\NativeArray; but table::find() result not modify?