-- Ralf Eggert <[email protected]> wrote
(on Tuesday, 11 September 2012, 05:37 PM +0200):
> when I use the Zend\Paginator with the Zend\Paginator\Adapter\DbSelect
> Adapter I can set a prototype for the result set. If I want to get my
> own user entity object back from the paginator I would need to implement
> the Zend\Db\ResultSet\ResultSetInterface in my user entity class which
> also extends Traversable and Countable interfaces.
> 
> Another way would be to use a hydrator class which takes the data from
> the result set and passes it to an instance of my user entity class. But
> since the reading of the data with the help of the Paginator only starts
> when the view script is processed, I would need to implement this in my
> view script or a view helper. This seems like a bad design.
> 
> So, I wonder if I could configure the DbSelect Adapter to use my own
> user entity class instead of an ArrayObject which is created from the
> ResultSet.

I actually did this recently in a demo app, and the combination is quite
cool!

Basically, you need to use the HydratingResultSet with the DbSelect
paginator, and pass it the hydrator and a "prototype" object that's of
the type you want to use.

To use this with the DbSelect Paginator adapter, you provide a result
set _prototype_, which looks something like the following:

    $resultSetPrototype = new HydratingResultSet(
        new ArraySerializableHydrator(),
        new User,
    );
    $adapter = new DbSelect($select, $adapter, $resultSetPrototype);
    $paginator = new Paginator($adapter);

It "just works" -- as you iterate through the paginator instance, you'll
get instances of "User" (or whatever class you seed it with). Obviously,
you'll want to specify a hydrator suited to the domain objects you're
trying to use.

One note: I found I needed to enable buffering on the resultset -- your
mileage may vary based on what DB adapter you use. To enable it, simply
do this:

    $resultSetPrototype->buffer();

and you're good to go.

To see an example of this:

    
https://github.com/weierophinney/PhlyPeep/blob/master/src/PhlyPeep/Model/PeepTable.php

That TableGateway extension defines the result set prototype in the
constructor, and then uses it to seed a DbSelect adapter and Paginator
in various "fetch" methods I define. This is probably the sort of thing
you're looking for, as it's then done in the domain logic, and not in
your view. In your view, you'd simply specify the page and number of
results per page.

-- 
Matthew Weier O'Phinney
Project Lead            | [email protected]
Zend Framework          | http://framework.zend.com/
PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc

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


Reply via email to