CakePHP - 保存到更多模型但主要失败

CakePHP - saving to more models but main fails

我正在尝试更复杂的方法。我有一个存储所有一般项目的项目,我有一个产品,它是一个项目,我有一个商品,它是一个产品和一个项目。所以我有一个用于输入商品价值的表格,它将保存到所有与模型相关的表格(项目、产品、商品)中。这么多表的原因是因为所有表都应该有一个稍后使用的 id,例如:产品应该有它的 id,稍后用于销售。这是控制器:

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);
            }
}

Item 永远不会保存,但 Product 和 Good 会保存,它们都映射得很好,id 没问题,但 Item 甚至不在数据库中,尽管 Product 和 Good 已 item_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,
    ),
    );

}

同时我明白这只是一个不必要的复杂化,我将重新设计我的数据库。