Quantcast

ZF2 DB Result position forwarded?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

ZF2 DB Result position forwarded?

henit
Hi.

I have a kind of strange situation. I have a Zend\Db\ResultSet object from a db query with 5 rows of data in it. This object is then sent to the view for display.

When i send it to the content template (the ViewModel returned by the controller action method), it works fine:

$model = new ViewModel();
$model->setVariable('categories', $categories);
return $model

and i can traverse the content in the view. But if i send it to a child model that I i have initiated in the same controller method like this:

$menu = new ViewModel();
$menu->setVariable('categories', $categories);
$model->layout()->addChild($menu, 'left');

I get an exception when I try to traverse the same way in that view (the exact same view code). The exception is this one in the rewind method of the Result class:

throw new Exception\RuntimeException('This result is a forward only result set, calling rewind() after moving forward is not supported');

This occurs because $this->position is set to 5, as if it has allready traversed the results before it gets to the view. I only use the result object one time, and the object has position set to -1 before it is sent to $menu->setVariable but when the object is available in the view, it is position=5.

It looks like the variable object is traversed when sent to a child model but not to the main view. Any idea what could cause this?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: ZF2 DB Result position forwarded?

ralphschindler

>
> throw new Exception\RuntimeException('This result is a forward only result
> set, calling rewind() after moving forward is not supported');
>
> This occurs because $this->position is set to 5, as if it has allready
> traversed the results before it gets to the view. I only use the result
> object one time, and the object has position set to -1 before it is sent to
> $menu->setVariable but when the object is available in the view, it is
> position=5.

Sound like someone is traversing it for some reason.

> It looks like the variable object is traversed when sent to a child model
> but not to the main view. Any idea what could cause this?

This sounds like an odd problem, in general, unless there is good reason
to do so, no one should be travsersing the dataset.  I'll have to ask
Matthew whats going on here.

This happens b/c you're using mysqli, and by default, Mysqli only does
forward-moving result sets.  If you plan on using Mysqli and know that
memory is not going to be an issue, you'll either need to

$resultSet->getDataSource()->buffer();

In the future, we'll have an option that you can opt to have fully
buffered result sets from mysqli.

-ralph

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Loading...