Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a flat array from a cakephp query

I am using cakephp to run a query, this query consists of custom derived fields as well as some that are in the table.

Cake returns the ones in the table in its own array and the others in an array key of 0 - essentially splitting my results which afterwards I then have to merge together.

I know I can use virtual fields to put the derived fields in to the main array however they are very custom and can change a lot, so it is not suitable to put these fields as virtual fields.

What I'd like is an option of some sort so that cakephp just returns everything in to the 0 key of the array. Something like $this->Model->find('all',$options,RTN_FLAT_ARRAY)

Is something like this already possible?

like image 343
user2486089 Avatar asked Oct 20 '25 12:10

user2486089


1 Answers

This does not make any sense as it would break your result. You're using the "all" find. So it is expected that it will return 0, 1, 2... records in the array not just one record. If you flatten this you get just one record and lose all others.

Use "first" instead and put your custom fields inside the record it will return. You can use the afterFind() callback of the model to modify the result sets.

Also I would not flatten the array, the structure is there for a certain reason. Everything in CakePHP follows conventions. So for example the result will match your form fields when you set them. If you change that structure you'll have to do a lot more manual work in other places. Follow the conventions, they're there for a reason.

like image 99
floriank Avatar answered Oct 23 '25 02:10

floriank



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!