First, your table name is wrong, should be contacts_phones instead of
contacts_phone.   Second, you're not forming your data correctly in
your view for a hasAndBelongsToMany relationship.  CakePHP expects a
HABTM relationship to come in as [Model][Model][id]s.  What you have
there is a hasMany relationship setup, which actually makes more
sense.

It looks to me from your view file that what you really want is a
Contact->hasMany->Phone instead of Contact->hasAndBelongsTo->Phone.
Setting up a hasMany relationship is easy, just remove your
contacts_phones table and add contact_id field to your phones table.

A hasAndBelongsToMany relationship between Contact and Phone means
multiple contacts can the same phone number and visa versa, that's
probably not what you want, but if it is you'll need rework your view
so your contact can select form a list of phone numbers to associate
with.  Review http://book.cakephp.org/view/189/Automagic-Form-Elements
for tips on making your HABTM relationship easier.

Hope that helps,
Nick
On Mar 9, 7:05 am, Mark <daoustm...@gmail.com> wrote:
> I am working on a project that involves a lot of many to many
> relationships, and for some reason cakephp isn't playing nice with
> them.  Take the following tables:
>
> Table: contacts
> id
> name
> created
> updated
>
> Table: phones
> id
> number
>
> Table: contacts_phone
> contact_id
> phone_id
>
> Here are my two models:
>
> class Contact extends AppModel{
>     var $name = 'Contact';
>     var $hasAndBelongsToMany = 'Phone';
>
> }
>
> class Phone extends AppModel{
>     var $name = 'Phone';
>     var $hasAndBelongsToMany = 'Contact';
>
> }
>
> The controller:
>
>     function add(){
>         if(!empty($this->data)){
>             if($this->Contact->saveAll($this->data)){
>                 $this->Session->setFlash('Contact has been saved');
>             }
>         }
>     }
>
> And the view:
>
> <h1>Add Contact</h1>
> <?php
> echo $form->create('Contact');
> echo $form->input('Contact.name');
> echo $form->input('Phone.0.number');
> echo $form->end('Save');
> ?>
>
> The result?
>
> 1       START TRANSACTION               0               0
> 2       INSERT INTO `contacts` (`name`, `updated`, `created`) VALUES
> ('Mark', '1268143497', '1268143497')            1               0
> 3       SELECT LAST_INSERT_ID() AS insertID             1       1       0
> 4       SELECT `ContactsPhone`.`phone_id` FROM `contacts_phones` AS
> `ContactsPhone` WHERE `ContactsPhone`.`contact_id` = 3          0       0     
>   0
> 5       COMMIT          0               0
>
> What am I missing?

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 cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en

Reply via email to