I want objects, not arrays!

A few times I’ve heard people complaining about the fact that CakePHP’s find methods return data as arrays and not as objects. “It’s not OO!”, “I want access to Model methods!”, etc. etc. if you’ve followed cake for a while, you’ve probably heard the same.

It’s a bit of a debate as to why you’d want to have objects vs arrays, but I can tell you that most arguments I’ve read call for some breaking of MVC and often arise due to not really understanding the way CakePHP does certain things. Personally, I have nothing against working with arrays especially if my ultimate goal is to just display some data in the view. You can do a quick search at the google group to get views from both sides of the story, so I won’t repeat them here, but please keep in mind that it’s always a good idea to figure out how to use a framework (any framework) to accomplish what’s needed, rather than how to make it work the way you prefer.

So can you return objects from find methods, instead of arrays? Actually it’s very simple, here’s a little gem of code that was posted at the google group, by gwoo:

[sourcecode language=’php’]
function afterFind($results, $primary = false) {
if($primary == true && !is_object($results)) {
return Set::map($results);
}
}
[/cc]

Just to clarify, this is supposed to be added to your app model, so all of the results returned by your find methods, will be automatically “objectified”.

Obviously you’d probably want to expand on that a little, but even with this simple addition you can easily get access to the much desired objects. Just remember to keep in mind that if you do use this approach, it should not be an excuse to break MVC and other conventions.

m4s0n501
  • Pingback: CakePHP1.2 モデルのデータをオブジェクトで扱う | Sun Limited Mt.()

  • jacknirons

    funny thing. my team and i has build a couble of different versions of implementations (in different projects) over the last 4 weeks for using real objects as data-records, instead of arrays. the pro of using a objects is the simpliest one: you know what your are using. you get an api, which you can use. with a array, you has nothing but a ton of data. and you can simply implement code for objects (-properties, which really need them. so, we don’t simply but all the properties of a result-array into properties of a array, what Set::map surely does. instead, we build a wrapper amoung the record-set and work with them.

  • teknoid

    @jacknirons

    Makes sense, but the reality is that the data will be simply displayed in the view in a vast majority of cases, therefore a necessity to work with an object is kind of low, unless of course your project has some specific requirements.

  • http://gedankenjournal.de André

    This article is now 2 years old. But, hey, YOU SAVED MY DAY :)

    Thanks a lot.

  • Pingback: CakePHP 2.0 Object not Array | Gravity Layouts()

  • Pingback: CakePHP 2.0 Object not Array()

  • Cameron

    Nice this is exactly what I was looking for regardless of how old it is!

    • teknoid

      cool