On Fri, Apr 18, 2008 at 5:54 PM, James K <[EMAIL PROTECTED]> wrote:
>
>  I've been bashing my skull against the wall all day trying to get a
>  HABTM relationship to save correctly...

Welcome to CakePHP. I think this is the single most frustrating thing
to get done right. However, it works great once you have it set up
correctly. And the source is some very clever PHP.

Having said that, I still can't write 2 controller/model sets in a row
without spending a good long time gnashing my teeth in despair.

>  Here's the setup:
>
>  Member hasOne Account
>  Member hasAndBelongsToMany Address
>
>  This is set up so a member may specify both a permanent address and a
>  mailing address (if "different than above"). Therefore I have an
>  address_type in my join table.

My $.02: save yourself some grief and put those addresses in the same
row in the accounts table. I doubt you'll ever have a need for the
addresses to be separate. Your application can then simply look to see
if the mailing address IS NULL and, if so, shoot the package or
whatever to the billing address.

Yes, you'll often have empty columns but a decent (from this decade)
database engine can optimise that.

>  ...ok... that's odd. Same thing, but a little different. It also finds
>  two rows as it should. Next comes the kicker
>
>  DELETE FROM [addresses_members] WHERE [member_id] IN (NULL, NULL)
>
>  ...wait, WHAT? Why in the world are you trying to delete my
>  relationships? Even worse is why are you trying to delete
>  relationships with NULL member ids?
>
>  INSERT INTO [addresses_prospects] ([prospect_id],[address_id]) VALUES
>  (25,'2')
>  INSERT INTO [addresses_prospects] ([prospect_id],[address_id]) VALUES
>  (25,'3')
>
>  ...ok, you wanted to re-insert them for some reason. That's all well
>  and good, but these queries fail because it's missing my address_type.
>  It seems it doesn't pick up the address_type I passed in, but I don't
>  know how else to populate the field since the HABTM relationship is
>  automagic'd (it knows there's an extra field on the read, but not on
>  the save).
>
>  I've been combing the net for days now, updated to the latest
>  nightlys, dug through trac and this group and have come up totally
>  empty handed. It seems no one has actually figured out how to use this
>  properly outside of really simple applications, or maybe I'm just
>  running into bugs?

Actually, it's a feature, apparently.

>  I COULD model the link table myself and do it all by hand, but the
>  problem with that is then Cake does a separate Select statement for
>  each model on the other side of the link table which might not be a
>  problem on a page that only has 2 related records, but what happens
>  when I'm editing a record with dozens or hundreds? There must be
>  someone out there who has gotten HABTM working with a more complex
>  link table.
>
>  I'm torn between whether this is something I should be filing in Trac,
>  or whether I'm just not understanding how it works. Help?
>

There are some posts about this (with fix) and I've just tried to
locate them for you. Sorry, I can't remember quite what the terms to
search on would be.

Have you had a go through these links?
(How to handle tricky HasAndBelongsToMany situations?)
http://groups.google.com/group/cake-php/web/faq

Also, look at this:
http://bakery.cakephp.org/articles/view/add-delete-habtm-behavior

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
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