Setting an index using "id" in CakePHP search method ("all")

I have an array with data, all with their own unique identifier. I use the ORM find ('all') method, and the resulting array looks something like this:

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

From my routing, I get a unique identifier. I want to reuse my array and get data, for example, ID 1.

So I need the indices of my associative array (returned by find ('')) to be set with the identifier of the Wijken object itself.

I explained everything, just in case people have a different approach. However, querying the database again with the ID parameter is not appropriate.

+6
source share
2 answers

try Install :: merge

To preserve the find ('all') structure (from icc97 comment):

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

Alternatively, you can also extract one model:

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

hope what you are looking for :)

+15
source

I don’t know how to make my id a key in an array, and I don’t even think that this is possible with Cake without doing something β€œfunny”.

But if you find everything, I have to assume that you are going to process the data and loop at some point, and at this time you might have something like:

 foreach ($wijkens as $wijken) { [...]do the general things here[...] if ($wijken['Wijken']['id'] == $url_id) { [...]do the thing you want to specifically do to id = 1 here[...] } } 

On the other hand, I understand that you do not need any additional request, although it seems to me that this is a relative insignificant transaction cost, and still I would prefer.

+1
source

Source: https://habr.com/ru/post/886221/


All Articles