简体   繁体   English

CakePHP - 使用保存关联保存到两个以上模型

[英]CakePHP - saving to more than two models using save associated

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:

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

EDIT: I have changed the controller and now the Item is never saved but Product and Good is 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.编辑:我已经更改了 controller,现在项目从未保存,但 Product 和 Good 已保存,并且它们都映射得很好,ID 没问题但 Item 甚至不在数据库中,尽管 Product 和 Good 将 item_id 设置为一个值应该是数据库中的下一个。

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

}

Even the debugged $item looks ok but is not saved:甚至调试后的 $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'
    )
)

I think the problem is with your code at the lines where you are saving the data.我认为问题在于您保存数据的行中的代码。

$this->Item->create();
$this->Good->create();
$this->Product->create();

What is $this? $this 是什么? If you create a item with "$this" and later try to create a "product", "$this" will not have the item_id.如果您使用“$this”创建项目,然后尝试创建“产品”,则“$this”将没有 item_id。

Try using something like this to save the item created.尝试使用类似的东西来保存创建的项目。

$item = $this->Item->create();

Then, with that $item created, you could create a $product with the $item->id然后,创建了 $item,您可以使用 $item->id 创建一个 $product

Update:更新:

From cakephp documentation.来自 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);

You must use $this->Item->save($data) to save the information into database.您必须使用 $this->Item->save($data) 将信息保存到数据库中。

https://book.cakephp.org/2.0/en/models/saving-your-data.htmlhttps://book.cakephp.org/2.0/en/models/saving-your-data.html

Maybe the method create() is a bit unclear.也许 create() 方法有点不清楚。 It is used to restart the model state.用于重启 model state。

So, it would be所以,这将是

$item_saved = $this->Item->save($data['Item']);
$data['Product']['item_id'] = $this->Item->getLastInsertId();
$product_saved = $this->Product->save($data['Product']);

Edit 2:编辑2:

Maybe it is because you didn't use create() before save the Item.可能是因为您在保存项目之前没有使用 create()。 Try this please:请试试这个:

$this->Item->create();
$item_saved = $this->Item->save($data['Item']);
$data['Product']['item_id'] = $this->Item->getLastInsertId();
$product_saved = $this->Product->save($data['Product']);

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

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