CakePHP HABTM JoinTable 带额外字段

发布于 2024-11-24 15:50:23 字数 2456 浏览 6 评论 0原文

因此,我有一个客户表、一个联系人表和一个contacts_customers 连接表,该表还具有映射到contact_type 表的contact_type_id 字段。我已使用 CakePHP 更新 HABTM 连接表上的额外字段链接到的信息 试图提出解决方案,但只部分有效。我更改了 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_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'
);

contact_customer.php

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执着的年纪 2024-12-01 15:50:23

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文