Saturday, January 31, 2009

Re: How to load HABTM Joining model in a plugin?

You just saved me hours of work.

Note to anyone else who stumbles upon this, you can see this problem
as well even if you're not using "with" in either of your HABTM
models, at least if you've "modelized" your join tables. Once I added
"with" to both sides of all my HABTM model associations, all worked
like a charm.

On Jan 18, 8:10 am, Martin Westin <martin.westin...@gmail.com> wrote:
> In case someone else wants to avoid spending an afternoon inside the
> ClassRegistry.
> Here is the wisdom I was lacking:
>
> When specifying "with" for a HABTM... you MUST specify this on both
> ends of the association.
>
> You cannotuse the "less changes = less errors" and start by trying
> to get one side (A->B) correct and then fill in the other side when it
> works from the first side. This is because if Cake chooses to load
> Class B first (even though I am in ControllerA and A tops B
> alphabetically) the registry will have created an AppModel called AB
> (joiningmodel) and when A loads, that Class is reused (as it
> should).  In a word: GAAAAAARRRRGHGHHH!!!
>
> It may sound dumb as h*** but it took me hours trawling line for line
> throughModel, ClassRegistry and App before I figured this out. I
> don't know how my associatedmodelmanaged to load first but I think
> I'll leave it here and get on with something productive.
>
> Keep Baking!
> /Martin
>
> On Jan 15, 8:42 am, Martin Westin <martin.westin...@gmail.com> wrote:
>
> > Sure. This is from the NewsMessage side of things
>
> > var $hasAndBelongsToMany = array(
> >     'NewsSubscriber' => array(
> >         'className'             => 'News.NewsSubscriber',
> >         'joinTable'             => 'news_messages_news_subscribers',
> >         'with'                  => 'News.NewsMessagesNewsSubscriber',
> >         'foreignKey'            => 'news_message_id',
> >         'associationForeignKey' => 'news_subscriber_id',
> >         'conditions'            => '',
> >         'order'                 => '',
> >         'limit'                 => '',
> >         'unique'                => true,
> >         'finderQuery'           => '',
> >         'deleteQuery'           => '',
> >         'insertQuery'           => ''
> >         )
> >     );
>
> > I have tried removing the "News." prefix.
> > I have tried putting the joinmodelin the "root" application.
> > (just to make see if anything happened)
>
> > /Martin
>
> > On Jan 14, 10:57 pm, Webweave <webwe...@gmail.com> wrote:
>
> > > Can you post your HABTM from your models ?
>
> > > It looks like you have themodelfilein the right place, so my guess
> > > is that the problem is in there.
>
> > > On Jan 14, 4:59 am, Martin Westin <martin.westin...@gmail.com> wrote:
>
> > > > Hi everyone,
>
> > > > This is the first time I have tried to make a realmodelfor a joining
> > > > table and I got really stuck on this seemingly simple thing. I just
> > > > cant get it to work. Does anyone know how thisfileshould be named?
>
> > > > I have a HABTM association between two models in a plugin. I have now
> > > > created a joiningmodelto be able to capture some callbacks and write
> > > > custom "behaviours". Problem is Cake doesnotload themodelbut
> > > > resorts to creating its magic "empty"modelbased on AppModel. It does
> > > >noteven seem to find themodel'sfile.
>
> > > > The facts:
>
> > > > News.NewsMessage habtm News.NewsSubscriber
>
> > > > --- among other things in NewsMessage
> > > > 'with' => 'News.NewsMessagesNewsSubscriber',
> > > > ---
>
> > > > --- NewsMessagesNewsSubscriber
> > > > debug('NewsMessagesNewsSubscriberfilewas loaded');
> > > > class NewsMessagesNewsSubscriber extends NewsAppModel {
> > > >     var $name = 'NewsMessagesNewsSubscriber';
> > > >     function afterFind() {
> > > >         debug('NewsMessagesNewsSubscriber class was used in a find');
> > > >     }}
>
> > > > ---
>
> > > > I have but debug lines straight into themodelfileto see if it loads
> > > > at all and nothing. So I guess I have to name it something special I
> > > > can't figure out.
>
> > > > I have tried the following files placed in app/plugins/news/models/
> > > > news_messages_news_subscriber.php <-- should be this one, right?
> > > > news_message_news_subscriber.php
> > > > news_messages_news_subscribers.php
>
> > > > If you know what I should call thisfile, please reply while I have a
> > > > few hairs left on my head ;)
> > > > thanks
>
> > > > Martin Westin
--~--~---------~--~----~------------~-------~--~----~
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: