CakePHP-保存到更多模型,但main失败
我正在尝试更复杂的事情。我有一个储存所有普通物品的物品,我有一个产品是一个物品,我有一个商品是一个产品和一个物品。因此,我有一个表格用于输入货物的价值,它将保存到所有与模型相关的表格(项目、产品、货物)。之所以有这么多表格,是因为所有表格都应有一个稍后使用的id,例如:产品应有一个稍后用于销售的id。这是控制器:CakePHP-保存到更多模型,但main失败,php,cakephp,cakephp-model,Php,Cakephp,Cakephp Model,我正在尝试更复杂的事情。我有一个储存所有普通物品的物品,我有一个产品是一个物品,我有一个商品是一个产品和一个物品。因此,我有一个表格用于输入货物的价值,它将保存到所有与模型相关的表格(项目、产品、货物)。之所以有这么多表格,是因为所有表格都应有一个稍后使用的id,例如:产品应有一个稍后用于销售的id。这是控制器: class GoodsController extends AppController { public function add(){ $this->Item->cre
class GoodsController extends AppController {
public function add(){
$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;
$this->Item->Product->save($this->request->data);
$this->request->data['Good']['pid'] = $this->Product->id;
$this->Item->Good->save($this->request->data);
}
}
项目从未保存,但产品和货物已保存,并且它们都映射良好,id正常,但项目不在数据库中,尽管通过产品和货物将项目id设置为数据库中的下一个值。即使请求的数据看起来也不错:
array(
'Item' => array(
'name' => 'Microcontrollers',
'modified' => '2019-10-22 12:37:53',
'created' => '2019-10-22 12:37:53',
'id' => '120'
),
'Good' => array(
'status' => 'development',
'is_for_distributors' => '1'
),
'Product' => array(
'project' => 'neqwww'
)
)
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 $hasOne= array(
'Good' => array(
'className' => 'Good',
'foreignKey' => 'item_id',
'dependent' => false,
),
'Product' => array(
'className' => 'Product',
'foreignKey' => 'item_id',
'dependent' => false,
),
);
}
关系看起来不错:
array(
'Item' => array(
'name' => 'Microcontrollers',
'modified' => '2019-10-22 12:37:53',
'created' => '2019-10-22 12:37:53',
'id' => '120'
),
'Good' => array(
'status' => 'development',
'is_for_distributors' => '1'
),
'Product' => array(
'project' => 'neqwww'
)
)
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 $hasOne= array(
'Good' => array(
'className' => 'Good',
'foreignKey' => 'item_id',
'dependent' => false,
),
'Product' => array(
'className' => 'Product',
'foreignKey' => 'item_id',
'dependent' => false,
),
);
}
与此同时,我明白这只是一个不必要的复杂问题,我将重新设计我的数据库。检查查询日志以查看所有查询。这会有帮助的。同时,我明白这只是一个不必要的复杂问题,我将重新设计我的数据库。