Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting the index with 'id' in CakePHP's find('all') method

I have an array with data, all with their own unique ID. I'm using the ORM method find('all') and the resulting array looks somewhat like this:

Array
(
    [0] => Array
        (
            [Wijken] => Array
                (
                    [id] => 1
                    [name] => Naam
                    [lat] => 13.37
                    [lon] => 13.37
                    [zoom] => 14
                )

        )

)

From my Routing I'm receiving an unique ID.. What I want, is re-use my array and get the data from, let's say, ID 1.

So what I need is that the indexes of my associative array (returned by find('')) are being set with the id of the "Wijken"-object itself.

I explained everything, just in case people have a different approach. Querying the database again with the param ID is not acceptable though.

like image 576
Gerben Jacobs Avatar asked Apr 19 '11 08:04

Gerben Jacobs


1 Answers

try Set::combine

To maintain find('all') structure (from icc97 comment):

$idsAsIndexes = Set::combine($wijkens, '{n}.Wijken.id', '{n}');

Alternatively you can also extract a single model:

$idsAsIndexes = Set::combine($wijkens, '{n}.Wijken.id', '{n}.Wijken');

hope that's what you are looking for :)

like image 116
talelcool Avatar answered Sep 19 '22 02:09

talelcool