在cakephp中保存HABTM相关数据
Saving HABTM associated data in cakephp
我有个小问题。我的商品模型有 Has 并且属于许多与用户模型的关联。在数据库中,它们通过 commodities_users
table 相关。因此,我希望 Cakephp 在创建新商品时在数据库中的 commodities_users
table 中创建新记录。但它不起作用。
我的 $this->request->data
数组(当我想保存一种新商品时)看起来像这样
array(
'Commodity' => array(
'commodity_type' => '0',
'commoditygroup_id' => '',
'name' => 'asdfad',
'code' => '',
'ean' => '',
'costprice' => '',
'saleprice' => '12512,123',
'default_vatrate' => '',
'saleprice_gross' => '',
'sync_cashconnector' => '1',
'commoditysetting_id' => '',
'default_amount_enabled' => '0',
'default_amount' => '',
'stock_min' => '',
'stock' => '',
'comment' => ''
),
'User' => array(
(int) 0 => array(
'id' => '23'
),
(int) 1 => array(
'id' => '24'
),
(int) 2 => array(
'id' => '30'
),
(int) 3 => array(
'id' => '31'
)
));
我正在以这种方式保存商品模型$this->Commodity->saveAll($this->request->data);
我的 cakePhp 版本是 2.4。
关系是
var $hasAndBelongsToMany = array(
'User' => array(
'className' => 'User',
'joinTable' => 'commodities_users',
'foreignKey' => 'commodity_id',
'associationForeignKey' => 'user_id',
),
);
我做错了什么?
您没有将正确的数据输入 saveAll()
。
根据 CakePHP 文档,构建数据的正确方法如下:
array(
array(
'Commodity' => array(
'commodity_type' => '0',
'commoditygroup_id' => '',
'name' => 'asdfad',
'code' => '',
'ean' => '',
'costprice' => '',
'saleprice' => '12512,123',
'default_vatrate' => '',
'saleprice_gross' => '',
'sync_cashconnector' => '1',
'commoditysetting_id' => '',
'default_amount_enabled' => '0',
'default_amount' => '',
'stock_min' => '',
'stock' => '',
'comment' => ''
),
'User' => array(
'User' => array('23','24','30','31')
)
)
);
然后您保存数据:
$this->Commodity->saveAll($this->request->data, array('deep' => true))
如需进一步参考,请参阅
好的,我自己解决了。我太笨了,只在商品模型中写了 HABTM 协会。为用户模型添加相同的内容解决了这个问题。 $this->reuqest->data
数组看起来如此
array(
'Commodity' => array(
'commodity_type' => '0',
'commoditygroup_id' => '',
'name' => 'asdfad',
'code' => '',
'ean' => '',
'costprice' => '',
'saleprice' => '12512,123',
'default_vatrate' => '',
'saleprice_gross' => '',
'sync_cashconnector' => '1',
'commoditysetting_id' => '',
'default_amount_enabled' => '0',
'default_amount' => '',
'stock_min' => '',
'stock' => '',
'comment' => ''
),
'User' => array(
'id' => array(
'0' => '23',
'1' => '24',
'2' => '25'
));
感谢您的宝贵时间!
我有个小问题。我的商品模型有 Has 并且属于许多与用户模型的关联。在数据库中,它们通过 commodities_users
table 相关。因此,我希望 Cakephp 在创建新商品时在数据库中的 commodities_users
table 中创建新记录。但它不起作用。
我的 $this->request->data
数组(当我想保存一种新商品时)看起来像这样
array(
'Commodity' => array(
'commodity_type' => '0',
'commoditygroup_id' => '',
'name' => 'asdfad',
'code' => '',
'ean' => '',
'costprice' => '',
'saleprice' => '12512,123',
'default_vatrate' => '',
'saleprice_gross' => '',
'sync_cashconnector' => '1',
'commoditysetting_id' => '',
'default_amount_enabled' => '0',
'default_amount' => '',
'stock_min' => '',
'stock' => '',
'comment' => ''
),
'User' => array(
(int) 0 => array(
'id' => '23'
),
(int) 1 => array(
'id' => '24'
),
(int) 2 => array(
'id' => '30'
),
(int) 3 => array(
'id' => '31'
)
));
我正在以这种方式保存商品模型$this->Commodity->saveAll($this->request->data);
我的 cakePhp 版本是 2.4。 关系是
var $hasAndBelongsToMany = array(
'User' => array(
'className' => 'User',
'joinTable' => 'commodities_users',
'foreignKey' => 'commodity_id',
'associationForeignKey' => 'user_id',
),
);
我做错了什么?
您没有将正确的数据输入 saveAll()
。
根据 CakePHP 文档,构建数据的正确方法如下:
array(
array(
'Commodity' => array(
'commodity_type' => '0',
'commoditygroup_id' => '',
'name' => 'asdfad',
'code' => '',
'ean' => '',
'costprice' => '',
'saleprice' => '12512,123',
'default_vatrate' => '',
'saleprice_gross' => '',
'sync_cashconnector' => '1',
'commoditysetting_id' => '',
'default_amount_enabled' => '0',
'default_amount' => '',
'stock_min' => '',
'stock' => '',
'comment' => ''
),
'User' => array(
'User' => array('23','24','30','31')
)
)
);
然后您保存数据:
$this->Commodity->saveAll($this->request->data, array('deep' => true))
如需进一步参考,请参阅
好的,我自己解决了。我太笨了,只在商品模型中写了 HABTM 协会。为用户模型添加相同的内容解决了这个问题。 $this->reuqest->data
数组看起来如此
array(
'Commodity' => array(
'commodity_type' => '0',
'commoditygroup_id' => '',
'name' => 'asdfad',
'code' => '',
'ean' => '',
'costprice' => '',
'saleprice' => '12512,123',
'default_vatrate' => '',
'saleprice_gross' => '',
'sync_cashconnector' => '1',
'commoditysetting_id' => '',
'default_amount_enabled' => '0',
'default_amount' => '',
'stock_min' => '',
'stock' => '',
'comment' => ''
),
'User' => array(
'id' => array(
'0' => '23',
'1' => '24',
'2' => '25'
));
感谢您的宝贵时间!