On Tue, Oct 19, 2010 at 8:30 AM, Joshua Muheim <[email protected]> wrote:
>
> Then I saw that every related model has its own key there, so it makes
> a bit sense. But why is the key in the array the model's name?
> Wouldn't it be better to have the name of the association in there?
What if you have several other models associated by belongsTo (or any
other assoc)?
> Because what when I have multiple associations to the same model? Or a
> HABTM with myself?
The key is the model alias, actually. This is usually the same as the
name. And, if you've got a model associated with itself, you'd do:
(say, for a User model)
$this->hasMany = array( // or other assoc
'Friend' => array(
'className' => 'User',
....
So, when you want to fetch a User's Friends, you're really fetching
other Users. But you refer to them by the alias to avoid confusion
(both for yourself and the DB query).
> Aside of this, I guess retrieving the "immediate" data of the model
> (not of it's related models') should have a special status and should
> be able to be retrieved in a faster way, like the one above...
Meh. Faster, how? It's a few extra keystrokes. And, besides, if all
your other data is going to come to you in an array, why not have it
all there? Besides, having it all together like that allows for
manipulating it with Set class. If some data was in an array, and
other inan object, that wouldn't be possible. Not without adding the
object's data to an array first. And that'd be a bunch of extra
keystrokes.
Personally, I've gotten into the habit of always fetching data into a
variable named $data. No $user = $this->User->find(...) or $post =
...Because then your constantly looking for data in
$user['User'][...], which seems a bit dumb. Or looks that way. But
$data['User'] makes perfect sense.
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 [email protected]
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en