[英]CakePHP and HABTM insert/save doesn't work
I'm stuck in Cakephp insert, here is what I have 我被卡在Cakephp插件中,这是我所拥有的
-Order HABTM Address -订购HABTM地址
-Address model -地址模型
Here is my association in Address model : 这是我在地址模型中的关联:
public $hasAndBelongsToMany = array(
'Address' => array(
'className' => 'Address',
'joinTable' => 'address_customers',
'foreignKey' => 'customer_id',
'associationForeignKey' => 'address_id'
)
);
I want to save/insert a new customer with one new address So, I thought the array below would work : 我想用一个新地址保存/插入一个新客户,所以,我认为下面的数组可以工作:
array(
'Customer' => array(
'nom' => 'Khiami',
'prenom' => 'TEST',
'tel' => '0945454545',
'ptel' => '',
'commentaire' => '',
'email' => '',
'anniversaire' => array(
'day' => '08',
'month' => '12',
'year' => '2012'
),
'createdFrom' => 's'
),
'Address' => array(
(int) 0 => array(
'adresse' => 'ADDR TEST',
'cp_id' => '1',
'etage' => '',
'residence' => '',
'batiment' => '',
'appartement' => '',
'type' => 'l',
'code_sonette' => '',
'code_portail' => '',
'code_batiment' => '',
'code_asc' => ''
)
)
)
The only thing that is working is when I save the Customer first and then use the array below : 唯一有效的是当我先保存客户然后使用下面的数组时:
array(
(int) 0 => array(
'Customer' => array(
'customer_id' => '394'
),
'Address' => array(
'adresse' => 'ADDR TEST',
'cp_id' => '1',
'etage' => '',
'residence' => '',
'batiment' => '',
'appartement' => '',
'type' => 'l',
'code_sonette' => '',
'code_portail' => '',
'code_batiment' => '',
'code_asc' => ''
)
)
)
But, I still don't have the association table filled ! 但是,我仍然没有填写关联表 ! It only adds an entry in the Address table.
它仅在地址表中添加一个条目。
Since you specify the jointable in your HABTM relationship I doubt its a naming convention problem. 由于您在HABTM关系中指定了可连接对象,因此我怀疑其命名约定问题。
Are you saving in the Address or the Customer model ? 您要保存“地址”或“客户”模型吗? Because if you are calling it from the Address model I suppose there should already be a customer, in that case you only have to set:
因为如果您从Address模型调用它,我想应该已经有一个客户,在这种情况下,您只需要设置:
$this->request->data['Customer']['Customer'] = $customer_id;
And save the address data, it should create the HABTM association in the table. 并保存地址数据,它应该在表中创建HABTM关联。
Yes you should specify it in both: 是的,您应该在两者中都指定它:
Address model: 地址模型:
public $hasAndBelongsToMany = array(
'Customer' => array(
'className' => 'Customer',
'joinTable' => 'address_customers',
'foreignKey' => 'address_id',
'associationForeignKey' => 'customer_id'
));
Customer Model: 客户模型:
public $hasAndBelongsToMany = array(
'Address' => array(
'className' => 'Address',
'joinTable' => 'address_customers',
'foreignKey' => 'customer_id',
'associationForeignKey' => 'address_id'
));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.