I got a pretty simple setup here, but can't seem to get the data to
save automatically (like its supposed to).  Any guidance would be
appreciated...

I have two models: contacts and phones.  Contacts HABTM phones and
vice versa...

contacts_controller.php

class ContactsController extends AppController {
    var $scaffold;
    var $helpers = array('Form');

    function add(){
        if(!empty($this->data)){
            $this->Contact->saveAll($this->data);
        }
    }
}

class Contact extends AppModel {
    var $name = 'Contact';
    var $hasAndBelongsToMany = array('Phone');
}

class Phone extends AppModel {
    var $name = 'Phone';
    var $hasAndBelongsToMany = array('Contacts');
}

Add.ctp
<?=$form->create('Contact'); ?>
<?=$form->input('name');?>
<?=$form->input('address')?>
<?=$form->input('Phone.phone')?>
<?=$form->submit('Add');?>
<?=$form->end();?>

The result? See below...

1       START TRANSACTION               0               0
2       INSERT INTO `contacts` (`name`, `address`, `updated`, `created`)
VALUES ('Mark2', 'some address', '1270574965', '1270574965')            1       
        1
3       SELECT LAST_INSERT_ID() AS insertID             1       1       0
4       SELECT `ContactsPhone`.`phone_id` FROM `contacts_phones` AS
`ContactsPhone` WHERE `ContactsPhone`.`contact_id` = 4          0       0       0
5       INSERT INTO `contacts_phones` (`contact_id`,`phone_id`) VALUES
(4,'6515874018')                1               0
6       COMMIT          0               0

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 [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

To unsubscribe, reply using "remove me" as the subject.

Reply via email to