简体   繁体   English

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

[英]CakePHP - saving to more models but main fails

I am trying something more complicated.我正在尝试更复杂的东西。 I have an Item which stores all general items, I have a Product which is an item and I have a Good which is a product and a item.我有一个存储所有一般物品的项目,我有一个产品,它是一个项目,我有一个商品,它是一个产品和一个项目。 So I have a form for entering value for the good and it shall save to all model related tables (items, products, goods).所以我有一个输入商品价值的表格,它应该保存到所有 model 相关表(项目、产品、货物)中。 The reason for so many tables is because all tables shall have an id which is used later, example: product shall have its id which is used later for selling.有这么多表的原因是因为所有表都应该有一个稍后使用的 id,例如:产品应该有它的 id,稍后用于销售。 Here is the controller:这是 controller:

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

The Item is never saved but Product and Good are saved and they are all mapped well, ids are ok but Item is not even in the db, althrough Product and Good have item_id set to a value which should be next in the db.项目永远不会被保存,但 Product 和 Good 被保存并且它们都映射得很好,ID 没问题但 Item 甚至不在数据库中,尽管 Product 和 Good 将 item_id 设置为应该在 db 中的下一个值。 Even requested data looks good:甚至请求的数据看起来也不错:

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'
    )
)

Relations looks good:关系看起来不错:

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

}

In the meantime i understood that this is just a needless complicating and I shall redesign my db.同时我明白这只是一个不必要的复杂化,我将重新设计我的数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM