Thursday, September 25, 2008

What is the "CakePHP way" to use data returned from the model?

Hi all,

I am new to CakePHP. I've used both PHP (with no frameworks) and Ruby
on Rails in the past, and decided that learning a PHP framework might
make PHP projects a lot more pleasant. I chose CakePHP because I'm
familiar with Rails - so far it's great, but I do have to wrap my head
around the differences.

I'm a bit unsure on what is the "CakePHP way" to use data returned
from the model. To keep things simple, let's say I have a model
Picture, with only one field, id.

What's throwing me off is that the data returned doesn't always seem
to follow a consistent format. For example,

$this->Picture->findById("1")

will return

Array
(
[Picture] => Array
(
[id] => 1
)
)

but

$this->Picture->query("SELECT * FROM pictures WHERE id=1")

will return

Array
(
[0] => Array
(
[pictures] => Array
(
[id] => 1
)
)
)

It feels a bit odd to have to write, for example, $pic["Picture"]
["id"] everywhere, rather than $pic["id], but I can live with that.

What's really throwing me off is $pic["Picture"]["id"] versus
$pic["pictures"]["id"]. Having to account for that every time I
either read or use data seems... well... it really doesn't feel right.

So, am I missing anything? Am I supposed to massage the data returned
from Find and Query methods in the controller? How do you guys use it
in your views and helpers, to keep things as simple and consistent as
possible?

I'm using 1.2.0.7296-RC2.

Thanks for any help!

Alex

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

No comments: