我在尝试一些更复杂的东西。我有一个储存所有一般物品的物品,我有一个产品,它是一个物品,我有一个商品,一个产品和一个物品。所以我有一个输入价值的表格,它应该保存到所有的模型相关的表格(物品,产品,货物)。之所以有这么多的表,是因为所有的表都应该有一个稍后使用的id,例如:产品应该有它的id,稍后用于销售。这里是控制器:
public function add() {
$this->load();
if ($this->request->is('post')) {
$this->Item->create();
$this->request->data['Item']['code'] = $finalCode;
$this->request->data['Item']['is_deleted'] = false;
$item = $this->Item->save($this->request->data);
if(!empty($item)){
$this->request->data['Product']['item_id'] = $this->Item->id;
$this->request->data['Good']['item_id'] = $this->Item->id;
debug($this->request->data['Product']['item_id']);
debug($item);
$this->Item->Product->save($this->request->data);
$this->request->data['Good']['pid'] = $this->Product->id;
$this->Item->Good->save($this->request->data);
}
if($this->Good->validationErrors || $this->Item->validationErrors || $this->Product->validationErrors){
//ERRORS
}
else{
//FAILS
}
}
}编辑:我已经更改了控制器,现在该项目从未保存过,但是产品和Good被保存,并且它们都映射得很好,in是可以的,但是项目甚至不在db中,Al身产品和Good有item_id设置为一个值,这个值应该是db中的下一个。
class Good extends AppModel {
public $belongsTo = array(
'Item' => array(
'className' => 'Item',
'foreignKey' => 'item_id',
),
'Product' => array(
'className' => 'Product',
'foreignKey' => 'pid',
)
);
}
class Product extends AppModel {
public $hasOne = array(
'Good' => array(
'className' => 'Good',
'foreignKey' => 'pid',
'dependent' => false,
),
);
}
class Item extends AppModel{
public $hasMany = array(
'Good' => array(
'className' => 'Good',
'foreignKey' => 'item_id',
'dependent' => false,
),
'Product' => array(
'className' => 'Product',
'foreignKey' => 'item_id',
'dependent' => false,
),
);
}即使是调试过的$item看起来也不错,但没有保存:
array(
'Item' => array(
'name' => 'Microcontrollers',
'description' => 'Wire Jumpers Female-to-Female 30 cm',
'weight' => '22',
'measurement_unit_id' => '7',
'item_type_id' => '29',
'code' => 'GOD-34',
'is_deleted' => false,
'modified' => '2019-10-22 12:37:53',
'created' => '2019-10-22 12:37:53',
'id' => '120'
),
'Good' => array(
'status' => 'development',
'hts_number' => '8473 30 20',
'tax_group' => '20%',
'eccn' => 'EAR99',
'release_date' => array(
'month' => '10',
'day' => '22',
'year' => '2019',
'hour' => '10',
'min' => '10',
'meridian' => 'am'
),
'is_for_distributors' => '1'
),
'Product' => array(
'project' => 'neqwww'
)
)发布于 2019-10-21 11:41:59
我认为问题在于您要保存数据的行中的代码。
$this->Item->create();
$this->Good->create();
$this->Product->create();$this是什么?如果您创建一个带有"$this“的项目,然后尝试创建一个”产品“,"$this”将不具有item_id。
尝试使用类似的方法来保存创建的项目。
$item = $this->Item->create();然后,使用创建的$item,您可以使用$item->id创建一个$product
更新:
来自cakephp文档。
// Create: id isn't set or is null
$this->Recipe->create();
$this->Recipe->save($this->request->data);
// Update: id is set to a numerical value
$this->Recipe->id = 2;
$this->Recipe->save($this->request->data);您必须使用$this->Item->$data将信息保存到数据库中。
https://book.cakephp.org/2.0/en/models/saving-your-data.html
也许create()方法有点不清楚。它用于重新启动模型状态。
所以,这将是
$item_saved = $this->Item->save($data['Item']);
$data['Product']['item_id'] = $this->Item->getLastInsertId();
$product_saved = $this->Product->save($data['Product']);编辑2:
可能是因为在保存项目之前没有使用create()。请试试这个:
$this->Item->create();
$item_saved = $this->Item->save($data['Item']);
$data['Product']['item_id'] = $this->Item->getLastInsertId();
$product_saved = $this->Product->save($data['Product']);https://stackoverflow.com/questions/58483433
复制相似问题