@blogica.  Everyone who has made HABTM work is a guru as far as I'm
concerned :)

To test what is being returned,  I'm simply putting this in my
controller view:

$this->User->recursive = 1;
debug($this->User->findAll());

the Group array is always empty.

Do I need to put more stuff in the model?
Can anyone share a working example?

On Feb 23, 6:37 pm, "b logica" <[EMAIL PROTECTED]> wrote:
> Oh, I'm by no stretch a Cake guru. I'm still working on my first app
> (porting over an existing site) in my spare time. But I'm trying to
> answer what questions I can so the gurus don't have to. Of course,
> corrections to any erroneous info I might give are more than welcome.
>
> I think that part of the problem with the
> $associationForeignKey/$foreignKey is that, under some circumstances,
> it doesn't matter if they're reversed. I say that because I just did
> just that in one of my models and it showed no obvious effect. But (if
> I'm correct in that assumption) problems definitely will show up if
> there are other inconsistencies.
>
> What I mean is, once the models, controllers, etc. are set up
> correctly, it may be that *sometimes* those values may be switched
> without showing any errors. But this will only be the case once
> everything's set up. Until then, if the values are switched, things
> will go wrong immediately.
>
> I'm just talking out of my arse right now; none of this has been
> tested. But I can see for myself that switching the values doesn't
> cause any obvious error.
>
> Actually, upon further thought, I think that it's not crucial for
> SELECT but that a save would fail miserably (or, more likely, have
> very inconsistent results).
>
> As for your initial problem, you probably need to set the correct
> recursion. You can also look into using the Bindable behavior:
>
> http://bakery.cakephp.org/articles/view/bindable-behavior-control-you...
>
> On Sat, Feb 23, 2008 at 12:38 PM, villas <[EMAIL PROTECTED]> wrote:
>
> >  @blogica.  Thanks for your reply and although you may be right about
> >  the keys being reversed in my inital post,  I had already tried them
> >  both ways before posting.
>
> >  You are also right about other examples being confused.  The CookBook
> >  example seems to be wrong in a couple of ways and when I eventually
> >  discover how this works,  I'll be delighted to propose amendments.
>
> >  Until then,  perhaps the best way for me to figure this out is by
> >  copying someone's working code.  Perhaps some kind soul would provide
> >  the barest working example for me to copy.  Otherwise,  I hope blogica
> >  or some other guru may provide an insight.
>
> >  On Feb 23, 4:56 pm, "b logica" <[EMAIL PROTECTED]> wrote:
> >  > You have your keys reversed.
>
> >  > 'foreignKey' => 'user_id',
> >  > 'associationForeignKey' => 'group_id',
>
> >  > This same thing has caught me up, over and over again, due to looking
> >  > at the array and its keys from the wrong perspective. Rather than the
> >  > keys & values be about Group they are more about User, as pertains to
> >  > Group. I hope that makes sense. Just remember that the key with
> >  > 'association' is for the 'other' model (ie. not the model for the
> >  > present class but the one that is the key in the $hasAndBelongsToMany
> >  > array.
>
> >  > A surprising number of examples out there have it reversed. I think
> >  > there's an example in the docs somewhere that has it reversed, also.
>
> > > On Sat, Feb 23, 2008 at 10:22 AM, villas <[EMAIL PROTECTED]> wrote:
>
> >  > >  Cake 1.2 Beta / Php 5.xx / Firebird 2
>
> >  > >  I have never succeeded in making even the simplest HABTM association
> >  > >  work. I tried scaffold and reading everything I could and
> >  > >  experimenting different ways.  Here is the simple setup:
>
> >  > >  Users:  id, name
> >  > >  Groups: id, name
> >  > >  Groups_Users: user_id, group_id
>
> >  > >  Model:  User.php
> >  > >  class User extends AppModel {
>
> >  > >         var $name = 'User';
> >  > >         var $useTable = 'users';
>
> >  > >         var $hasAndBelongsToMany = array(
> >  > >                         'Group' => array('className' => 'Group',
> >  > >                                                 'joinTable' => 
> > 'groups_users',
> >  > >                                                 'foreignKey' => 
> > 'group_id',
> >  > >                                         'associationForeignKey' => 
> > 'user_id',
> >  > >                                         'unique' => true,
> >  > >                                                 'conditions' => '',
> >  > >                                                 'fields' => '',
> >  > >                                                 'order' => '',
> >  > >                                                 'limit' => '',
> >  > >                                                 'offset' => '',
> >  > >                                                 'finderQuery' => '',
> >  > >                                                 'deleteQuery' => '',
> >  > >                                                 'insertQuery' => ''
> >  > >                         )
> >  > >         );
> >  > >  }
>
> >  > >  When I use 'find' in my user controller and then dump the array,  I
> >  > >  get the [user] array data,  but [group] array is always empty.
>
> >  > >  The debug message shows the SQL from two queries.  When I run the
> >  > >  queries manually they each return data (from users and groups
> >  > >  respectively) as expected.
>
> >  > >  I know it should be simple,  but why can't I get the [group] array
> >  > >  populated from the queries.  It has been driving me mad!
>
> >  > >  Regards, villas
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" 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
-~----------~----~----~----~------~----~------~--~---

Reply via email to