【发布时间】:2011-07-15 18:14:27
【问题描述】:
所以我有一个customers 表、一个contacts 表和一个contacts_customers 连接表,其中还有一个contact_type_id 字段映射到一个contact_type 表。我已经使用来自CakePHP update extra field on HABTM join table 的信息来尝试提出解决方案,但它只是部分起作用。我把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,紧接着是@ 987654328@)。我在这里的假设是这是由于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] 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'
);
contact_types.php
var $hasMany = array(
'ContactsCustomer'
);
customer.php
var $hasMany = array(
'ContactsCustomer'
);
contacts_customer.php
var $belongsTo = array(
'Contact', 'ContactType', 'Customer'
);
【问题讨论】:
标签: cakephp has-and-belongs-to-many