Tuesday, March 30, 2010

Re: Retrieving objects from the DB instead of arrays

I'm afraid that there's no satisfying answer for you. Cake doesn't map
DB records to objects; arrays are all you're gonna get. If you want
your data in objects instead of arrays, then you'll have to rework a
substantial part of the Cake core. You're stuck with arrays unless you
want to do some serious, serious work. ;)

As Andy mentioned, you can use Model callbacks - especially afterFind
- to format your data. You can also write helpers to accomplish a lot
of the dirty work. So, instead of $person->getAge(), you might have
$formatter->getAge($person['Person']['birthday']) where "$formatter"
is FormatterHelper and getAge() is a function that takes a date and
converts it to an age. For helpers:

http://book.cakephp.org/view/1095/Helpers

Hope that helps.

- Jamie

On Mar 30, 8:31 pm, paws_galuten <jason.galu...@gmail.com> wrote:
> I'm new to cakePHP and it seems like the only way to retrieve data
> from the data source is as an array. I'm finding that I am using a lot
> of code in my controller to prepare the arrays for display in the
> view, when I would rather be able to send an object to the view and
> get data from that object.
>
> For example, if I had a person object, I could have an attribute that
> was their birthdate and in the view I could easily get their age with
> something like $person->getAge(). If I only have the person as an
> array, then I have to do the calculating of the age before I can
> display it.
>
> Does this make sense? It is a simplistic example, but I can see myself
> having to write a lot of code to manipulate arrays before the raw
> database data is usable.
>
> Thanks for your insights,
> Jason

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

To unsubscribe, reply using "remove me" as the subject.

No comments: