ZF2: is there no fetchPairs() any more

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

ZF2: is there no fetchPairs() any more

Ralf Eggert
Hi,

I am just curious since I did not find any fetchPairs() method in
Zend\Db anymore. Maybe that was renamed or is it gone?

I know that I can provide the same stuff with taking the result set and
build my own array.

Regards,

Ralf

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


luk
Reply | Threaded
Open this post in threaded view
|

Re: ZF2: is there no fetchPairs() any more

luk
Ralf Eggert wrote
I am just curious since I did not find any fetchPairs() method in
Zend\Db anymore. Maybe that was renamed or is it gone?

I know that I can provide the same stuff with taking the result set and
build my own array.
Hi Ralf,
I cannot find it either. I'm guessing that nice and clean solution would be to apply something called Feature to the TableGateway. In the AbstractTableGateway you can find something like:
        // build result set
        $resultSet = clone $this->resultSetPrototype;
        $resultSet->initialize($result);

        // apply postSelect features
        $this->featureSet->apply('postSelect', array($statement, $result, $resultSet));

        return $resultSet;

I'm not sure though if fetchPairs can be achieved this way.
Cheers, -- Luke Mierzwa
Reply | Threaded
Open this post in threaded view
|

Re: ZF2: is there no fetchPairs() any more

Ralf Eggert
Hi Luke,

interesting idea with the features. An example would be handy.

Currently, I implemented this in my mapper:

------------------------------------------------------------------
    public function fetchParentOptions()
    {
        $options = array(
            '' => '-',
        );

        // Initialize select
        $select = $this->getDbTable()->getSql()->select();
        $select->columns(array('id', 'name'));
        $select->where->isNull('parent');
        $select->order('name');

        $result = $this->getDbTable()->fetchAll($select);

        foreach ($result as $data) {
            $options[$data->id] = $data->name;
        }

        return $options;
    }
------------------------------------------------------------------

Regards,

Ralf

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


luk
Reply | Threaded
Open this post in threaded view
|

Re: ZF2: is there no fetchPairs() any more

luk
Ralf Eggert wrote
Hi Luke,

interesting idea with the features. An example would be handy.

Currently, I implemented this in my mapper:

------------------------------------------------------------------
    public function fetchParentOptions()
    {
        $options = array(
            '' => '-',
        );

        // Initialize select
        $select = $this->getDbTable()->getSql()->select();
        $select->columns(array('id', 'name'));
        $select->where->isNull('parent');
        $select->order('name');

        $result = $this->getDbTable()->fetchAll($select);

        foreach ($result as $data) {
            $options[$data->id] = $data->name;
        }

        return $options;
    }
------------------------------------------------------------------
I don't have a working example unfortunately but I quickly investigated further that TableGateway takes as 3rd parameter $features which can be your custom Feature that extends Db\TableGateway\Feature\AbstractFeature. (for ie call it FetchPairsFeature)
Your custom class will allow you to create methods like preSelect or postSelect (suit better in your case) to manipulate ResultSet object. As some good example I would suggest you to check already implemented Features in Zend\Db\TableGateway\Feature. Hope it helps.
Cheers, -- Luke Mierzwa
luk
Reply | Threaded
Open this post in threaded view
|

Re: ZF2: is there no fetchPairs() any more

luk
luk wrote
Ralf Eggert wrote
Hi Luke,

interesting idea with the features. An example would be handy.

Currently, I implemented this in my mapper:

------------------------------------------------------------------
    public function fetchParentOptions()
    {
        $options = array(
            '' => '-',
        );

        // Initialize select
        $select = $this->getDbTable()->getSql()->select();
        $select->columns(array('id', 'name'));
        $select->where->isNull('parent');
        $select->order('name');

        $result = $this->getDbTable()->fetchAll($select);

        foreach ($result as $data) {
            $options[$data->id] = $data->name;
        }

        return $options;
    }
------------------------------------------------------------------
I don't have a working example unfortunately but I quickly investigated further that TableGateway takes as 3rd parameter $features which can be your custom Feature that extends Db\TableGateway\Feature\AbstractFeature. (for ie call it FetchPairsFeature)
Your custom class will allow you to create methods like preSelect or postSelect (suit better in your case) to manipulate ResultSet object. As some good example I would suggest you to check already implemented Features in Zend\Db\TableGateway\Feature. Hope it helps.
And forgot to mention that AbstractFeature has got already setter defined for TableGateway object which can be used to control DI.
Cheers, -- Luke Mierzwa
Reply | Threaded
Open this post in threaded view
|

Re: ZF2: is there no fetchPairs() any more

Ralf Eggert
Hi Luke,

I played around with the Feature stuff now and I think it is not the
right way to solve this.

First, when I add the feature it is used in all selects that fetch data
for my TableGateway class. I implemented the postSelect() method and did
a little check if $resultSet->getFieldCount() is equal to 2. Then it
could make sense to pass back an option list rather then the resultSet.
I could live with that.

The bigger problem is that a ResultSet object is passed to the
postSelect() method. I would need to manipulate this object to have the
keys of the results to be the primary key and the values just the other
column. I don't know how to do this and I guess it won't work.

So, I think this cannot be implemented on the TableGateway level. So
until the ZF2 offers a fetchPairs implementation I will stick to my
solution on the Mapper level.

Regards,

Ralf

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


luk
Reply | Threaded
Open this post in threaded view
|

Re: ZF2: is there no fetchPairs() any more

luk
Ralf Eggert wrote
Hi Luke,

I played around with the Feature stuff now and I think it is not the
right way to solve this.

First, when I add the feature it is used in all selects that fetch data
for my TableGateway class. I implemented the postSelect() method and did
a little check if $resultSet->getFieldCount() is equal to 2. Then it
could make sense to pass back an option list rather then the resultSet.
I could live with that.

The bigger problem is that a ResultSet object is passed to the
postSelect() method. I would need to manipulate this object to have the
keys of the results to be the primary key and the values just the other
column. I don't know how to do this and I guess it won't work.

So, I think this cannot be implemented on the TableGateway level. So
until the ZF2 offers a fetchPairs implementation I will stick to my
solution on the Mapper level.

Regards,

Ralf

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]
Hi Ralf, good job with researching Features further. Btw I was thinking that another option would be creating your own Hydrator class and set it on the ResultSet object. Let me know if you want to investigate this option. Otherwise we would need to ask Ralph Schindler or Matthew. Cheers
Cheers, -- Luke Mierzwa
Reply | Threaded
Open this post in threaded view
|

Re: ZF2: is there no fetchPairs() any more

fballiano
In reply to this post by Ralf Eggert
Ralf Eggert wrote
I am just curious since I did not find any fetchPairs() method in
Zend\Db anymore. Maybe that was renamed or is it gone?
I was wandering about the same thing so i'm writing a little bridge class to use like it was the old (zf2 pre beta3) zf2 or zf1. probably not the right approach but not finding any documentation or anyone talking about this i just went that way, also cause i want to continue calling one single method to query the db.

hope you find it useful:
https://github.com/fballiano/zfbridge

fabrizio