2011-07-15 21 views
0

所以我有一个customers表,contact表和contacts_customers连接表,它还有contact_type_id字段映射到contact_type表。我已经使用从CakePHP update extra field on HABTM join table链接到的信息试图提出解决方案,但它只是部分工作。我在ContactCustomersControllerCakePHP HABTM JoinTable w /额外字段

$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)); 
} 

注意两部分保存改变add()方法;这是因为使用ContactsCustomer模型的saveAll()方法刚刚失败并且没有指出问题(所得结果页面上的SQL日志中出现的所有内容均为BEGIN,紧接着是ROLLBACK)。我的假设是,这是由于contacts表和contacts_customers表之间的外键约束所致,所以我选择了两部分保存。

在任何情况下,当前代码中都没有报告错误,但最终结果是相应的信息保存在contacts表中,但没有任何内容保存在contacts_customers连接表中。根据FireBug日志数据,发布的数据看起来是合适的:

_method POST 
data[Contact][address_1] asdsad 
data[Contact][address_2]  
data[Contact][city] asdasd 
data[Contact][email] [email protected] 
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 

我在这里做了什么不正确?非常感谢您提供的任何援助!

相关的模型数据:

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' 
); 

回答

0

还没有测试过,但我相信$ this-> data可能已经被第一次保存修改。只需使用一个额外的变量来保存这些数据。

+0

感谢您的尝试,但是'$ this-> data'的内容(包括其子数组)没有出现任何注释变化。 我仍在调查多条途径。 – user846764