Saturday, June 30, 2012

Re: find() not respecting belongsTo association

'foreignKey' => 'user_id'

The broadcasters table should have a user_id column that is the
foreign key pointing to users.id. Don't point broadcasters.id to
users.id. (This isn't just a Cake rule but SQL in general.)

Having said that, I wonder if belongsTo is the association to use
here. Wouldn't HABTM be more appropriate?

On Sat, Jun 30, 2012 at 8:20 AM, kleelof <kleelof@yahoo.com> wrote:
> I am having a problem getting a model->find() call to respect a belongsTo
> association I built.
>
> Here is the association:
>
> var $belongsTo = array('User' => array('className'  =>'User',
>                                        'foreignKey' => 'id',
>                                    'fields'     => array('username')
>                                        )
>                       );
>
> And here is the find call:
>
> $this->set('broadcasters_list', $this->Broadcaster->find('all',
>
>  array('fields'=>array('id')
>                                                          )));
>
> Now for a very odd thing; I have a pagination call on the Broadcaster model
> that, when called, respects the belongsTo association and returns the
> necessary information from the User model.
>
> take care, lee
>
> Edit:
>
> I took a look at the query being built, here is what I have:
>
> For the find command:
>
> SELECT `Broadcaster`.`id` FROM `broadcasters` AS `Broadcaster` LEFT JOIN
> `users` AS `User` ON (`Broadcaster`.`id` = `User`.`id`) LEFT JOIN `images`
> AS `profile_image` ON (`profile_image`.`user_id` = `Broadcaster`.`id` AND
> image_type = 3) WHERE 1 = 1
>
> For the pagination call:
>
> SELECT `Broadcaster`.`id`, `Broadcaster`.`key_id`,
> `Broadcaster`.`session_id`, `Broadcaster`.`broadcast_mode`,
> `Broadcaster`.`next_guest_id`, `Broadcaster`.`one_to_one_credits`,
> `Broadcaster`.`one_to_many_credits`, `Broadcaster`.`avilable_for_private`,
> `Broadcaster`.`connect_type`, `Broadcaster`.`commission`,
> `Broadcaster`.`credits`, `Broadcaster`.`enabled`, `User`.`username`,
> `profile_image`.`file_id` FROM `broadcasters` AS `Broadcaster` LEFT JOIN
> `users` AS `User` ON (`Broadcaster`.`id` = `User`.`id`) LEFT JOIN `images`
> AS `profile_image` ON (`profile_image`.`user_id` = `Broadcaster`.`id` AND
> image_type = 3) WHERE `enabled` = 1 LIMIT 10
>
> With the exception of the fields being retrieved, it all looks the same. Am
> I missing something in there?
>
> --
> Our newest site for the community: CakePHP Video Tutorials
> http://tv.cakephp.org
> Check out the new CakePHP Questions site http://ask.cakephp.org and help
> others with their CakePHP related questions.
>
>
> 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

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


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

No comments: