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?

Controller:

$list = table::find();
foreach($list as $k=>$v)
{
    $list[$k]->perms_txt = '2222';
}
$this->view->setVar('list',$list);

view template:

{% for rs in list %}
    {{rs.perms_txt}}-------
{%/endfor%}

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.



2.9k
edited Oct '14

page error!

Controller

    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(
            array(
                "data" => $list,
                "limit"=> $this->page_limit,
                "page" => $currentPage
            )
        );
        $page = $paginator->getPaginate();
        $this->view->setVar('page',$page);
    }
Phalcon\Paginator\Exception: Invalid data for paginator File=/data/www/mvc.dibiao.com/apps/controllers/RoleController.php Line=26 #0 /data/www/mvc.dibiao.com/apps/controllers/RoleController.php(26): Phalcon\Paginator\Adapter\Model->getPaginate()
#1 [internal function]: RoleController->indexAction('0')
#2 [internal function]: Phalcon\Dispatcher->dispatch()
#3 /data/www/mvc.dibiao.com/index.php(8): Phalcon\Mvc\Application->handle()
#4 {main}

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



83.4k
Accepted
answer

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



2.9k

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