1

In CakePHP 3 a find returns an object instead of an array. I know that you can convert the object into an array using toArray(). I'm trying to decide which is "better" to use. I know this can be seen as a subjective question, but I don't mind, I'm interested in your answers.

I read about object vs array, but can't really get a good answer out of what I googled and read. Person X says that using objects is better or faster, Person Y says using arrays is. So I thought I would ask here.

So while keeping CakePHP in mind, does it matter if you use the returned object or convert it into an array performance wise?

2
  • 3
    actually find() returns a query object until you call all() or toArray(). So what object are you talking about? query objects? Entity oblects? Recordset objects? Commented Aug 31, 2017 at 13:51
  • I don't think that matters a lot..It's all about practices you love to try. Commented Aug 31, 2017 at 17:11

1 Answer 1

3

Performance wise, it depends. If you disable hydration as in https://book.cakephp.org/3.0/en/orm/query-builder.html#getting-arrays-instead-of-entities

Then any performance hit is negligible. However, if you perform the query then call toArray on the entities, that's a bit slower performance wise.

As far as which is better, that's largely dependent on your particular use-case. There are some cases where it's better to have an array, and some where the object may be preferred. Without context, which is better is unanswerable.

Sign up to request clarification or add additional context in comments.

4 Comments

I missed the hydration part in de book, thanks for linking that! Do you know if you can set hydration app wide, so that it is used for all queries? The context mostly is for the find and processing the results in the view, with sometimes some processing before sending it to the view (using hash::combine or other functions which require an array). (edit: i hit enter too soon)
As far as I can tell, there's no way to turn it off app wide, however if you want, you could create a behavior with a beforeFind (see: book.cakephp.org/3.0/en/orm/table-objects.html#beforefind ) that disables hydration on any find and then simply add that behavior to your tables.
Oke, thank you! One last question (if you don't mind me asking), could you maybe give an example of a case while using CakePHP where you would want to use objects? I find the difference between the two difficult to understand.
@Femke Objects are useful when you want to use accessors and mutators (see: link) or type hinting. In addition, they make saving changes to database rows very easy as you can just do something like $entity->field = "new value"; table->save($entity). With the exception of strict type hinting, all of that can also be done with arrrays, just with a little more work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.