我遇到CakePHP 1.3和HABTM关系的问题。我有2个型号,Offer和Device。优惠可以在多个设备上,设备有多个优惠。我有一个devices_offer.php模型和连接表。发生的事情是,在编辑商品时,它会创建第二个未完成的条目,其中包含商品表和联接表中的编辑。原始优惠保持不变。另一件事是,要约也与业务(模型)有关系。 A Business拥有报价。我这样说的原因是,编辑商品时创建的重复商品条目不包含条目中的商家名称。所以我想知道这种关系是否搞砸了......另外,当编辑设备时,它会复制与设备相关的所有商品,并保留旧商品。代码实际上只不过是烘焙控制器,我按照CakePHP文档创建了HABTM:
offers_controller.php(编辑功能):
if ($this->Offer->save($this->data)) {
$this->Session->setFlash(__('The offer has been saved',true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The offer could not be saved. Please, try again.',true));
}
devicesOffer包含:
var $belongsTo = array('Device' => array(
'className' => 'Device',
'foreignKey' => 'device_id',
'conditions' => '',
'fields' => '',
'order' => ''),
'Offer' => array(
'className' => 'Offer',
'foreignKey' => 'offer_id',
'conditions' => '',
'fields' => '',
'order' => ''
));
优惠模式:
var $hasAndBelongsToMany = array(
'Device' => array(
'className' => 'Device',
'joinTable' => 'devices_offers',
'foreignKey' => 'offer_id',
'associationForeignKey' => 'device_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => '',
'with' => 'DevicesOffer'
)
);
设备型号:
var $hasAndBelongsToMany = array(
'Offer' => array(
'className' => 'Offer',
'joinTable' => 'devices_offers',
'foreignKey' => 'device_id',
'associationForeignKey' => 'offer_id',
'unique' => true,
'conditions' => 'Offer.active = 1 AND Offer.expires > now()',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
);
我现在正在编辑/正确保存。但是,当我更新/编辑设备时。与该设备相关的所有商品仅在devices_offer中重复。我假设这是因为ID不存在。但是,如何在保存??
时设置与设备相关的商品的ID