[英]CakePHP and HABTM insert/save doesn't work
我被卡在Cakephp插件中,这是我所拥有的
-订购HABTM地址
-地址模型
这是我在地址模型中的关联:
public $hasAndBelongsToMany = array(
'Address' => array(
'className' => 'Address',
'joinTable' => 'address_customers',
'foreignKey' => 'customer_id',
'associationForeignKey' => 'address_id'
)
);
我想用一个新地址保存/插入一个新客户,所以,我认为下面的数组可以工作:
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' => ''
)
)
)
唯一有效的是当我先保存客户然后使用下面的数组时:
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' => ''
)
)
)
但是,我仍然没有填写关联表 ! 它仅在地址表中添加一个条目。
由于您在HABTM关系中指定了可连接对象,因此我怀疑其命名约定问题。
您要保存“地址”或“客户”模型吗? 因为如果您从Address模型调用它,我想应该已经有一个客户,在这种情况下,您只需要设置:
$this->request->data['Customer']['Customer'] = $customer_id;
并保存地址数据,它应该在表中创建HABTM关联。
是的,您应该在两者中都指定它:
地址模型:
public $hasAndBelongsToMany = array(
'Customer' => array(
'className' => 'Customer',
'joinTable' => 'address_customers',
'foreignKey' => 'address_id',
'associationForeignKey' => 'customer_id'
));
客户模型:
public $hasAndBelongsToMany = array(
'Address' => array(
'className' => 'Address',
'joinTable' => 'address_customers',
'foreignKey' => 'customer_id',
'associationForeignKey' => 'address_id'
));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.