CakePHP HABTM连接表w/额外字段
所以我有一个customers表,一个contacts表和一个contacts\u customers join表,它还有一个contact\u type\u id字段,映射到contact\u type表。我使用了从中链接到的信息,试图找到一个解决方案,但它只起到了部分作用。我将CakePHP HABTM连接表w/额外字段,cakephp,has-and-belongs-to-many,Cakephp,Has And Belongs To Many,所以我有一个customers表,一个contacts表和一个contacts\u customers join表,它还有一个contact\u type\u id字段,映射到contact\u type表。我使用了从中链接到的信息,试图找到一个解决方案,但它只起到了部分作用。我将ContactCustomersController中的add()方法更改为 $this->ContactsCustomer->Contact->save($this->data); $this
ContactCustomersController
中的add()
方法更改为
$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));
}
注意两部分保存;这是因为使用ContactsCustomer
模型的saveAll()
方法失败了,没有任何问题的迹象(结果页面上的SQL日志中显示的只是一个BEGIN
,紧接着是一个ROLLBACK
)。我在这里的假设是,这是由于contacts
表和contacts\u customers
表之间的外键约束造成的,因此我选择了两部分保存
在任何情况下,当前代码中都没有报告错误,但最终结果是相应的信息保存在contacts
表中,而contacts\u customers
join表中没有保存任何信息。根据FireBug日志数据,发布的数据似乎是适当的:
_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
我在这里做错了什么?非常感谢您提供的任何帮助
相关模型数据:
contact.php
var $hasMany = array(
'ContactsCustomer'
);
联系_types.php
var $hasMany = array(
'ContactsCustomer'
);
customer.php
var $hasMany = array(
'ContactsCustomer'
);
var $belongsTo = array(
'Contact', 'ContactType', 'Customer'
);
contacts_customer.php
var $hasMany = array(
'ContactsCustomer'
);
var $belongsTo = array(
'Contact', 'ContactType', 'Customer'
);
我还没有测试过,但我相信$this->数据可能在第一次保存时被修改了。只需使用一个额外的变量来保存该数据。感谢您的尝试,但是
$this->data
的内容(包括其子数组)似乎没有任何变化。我仍在调查多个途径。