开发者

CakePHP HABTM JoinTable w/Extra Field

So I have a customers table, a contacts table, and a contacts_customers join table, which also has contact_type_id field which maps to a contact_type table. I have used the information linked to from CakePHP update extra field on HABTM join table in an attempt to come up with a solution, but it's only partly working. I changed the add() method in the ContactCustomersController to

$this->ContactsCustomer->Contact->save($this->data);
$this->ContactsCustomer->create();
if ($this->ContactsCustomer->save($this->data,
   array('validate' => 'first'))) {
       $this->Session->setFlash(__('The contacts customer has been saved', true));
   $this->redirect(array('controller' => 'contacts_customers', 'action' => 'index'));
} else {
   $this->Session->setFlash(__('The contacts customer could not be saved. Please, try again.', true));
}

Note the two-part save; this is because using the saveAll() method of the ContactsCustomer model was just out and out failing with no indication of the problem (all that showed up in the SQL log on the resulting page was a BEGIN immediately followed by a ROLLBACK). My as开发者_开发百科sumption here was that this was due to the foreign key constraint between the contacts table and the contacts_customers table, so I opted for the two-part save.

In any event, there are no errors reported in the current code, but the end result is that the appropriate information is saved in the contacts table, but nothing is saved in the contacts_customers join table. The data posted appears to be appropriate according to the FireBug log data:

_method POST
data[Contact][address_1]    asdsad
data[Contact][address_2]    
data[Contact][city] asdasd 
data[Contact][email]    jb@sc.net
data[Contact][fax]  
data[Contact][first_name]   Joe
data[Contact][last_name]    Blow
data[Contact][mobile_phon...    
data[Contact][office_phon...    sdfsdfdf
data[Contact][postal_code...    121212
data[Contact][state]    asdas
data[Contact][title]    
data[ContactsCustomer][ContactType] 
data[ContactsCustomer][ContactType][]   1
data[ContactsCustomer][ContactType][]   2
data[ContactsCustomer][ContactType][]   3
data[ContactsCustomer][Customer]    1

What have I done incorrectly here? Many thanks to any assistance offered!

Relevant model data:

contact.php

var $hasMany = array(
    'ContactsCustomer'
);

contact_types.php

var $hasMany = array(
    'ContactsCustomer'
);

customer.php

var $hasMany = array(
    'ContactsCustomer'
);

contacts_customer.php

var $belongsTo = array(
    'Contact', 'ContactType', 'Customer'
);


haven't tested that yet, but I believe $this->data might have been modified by the first save. Just use an extra variable to hold that data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜