Cakephp 3.x将数据插入两个表中

Cakephp 3.x将数据插入两个表中,cakephp,orm,save,associations,cakephp-3.0,Cakephp,Orm,Save,Associations,Cakephp 3.0,我有两张桌子:- 销售 CREATE TABLE `sales` ( `id` int(10) NOT NULL AUTO_INCREMENT, `title` varchar(255) DEFAULT NULL, `description` text, `quantity` int(10) DEFAULT NULL, `price` decimal(18,2) DEFAULT NULL, `payment_method_id` int(10) D

我有两张桌子:-

销售

CREATE TABLE `sales` (
    `id` int(10) NOT NULL AUTO_INCREMENT,
    `title` varchar(255) DEFAULT NULL,
    `description` text,
    `quantity` int(10) DEFAULT NULL,
    `price` decimal(18,2) DEFAULT NULL,
    `payment_method_id` int(10) DEFAULT NULL,
    `user_id` int(10) DEFAULT NULL,
    `created` datetime DEFAULT NULL,
    `modified` datetime DEFAULT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
CREATE TABLE `sale_details` (
    `id` int(10) NOT NULL AUTO_INCREMENT,
    `sale_id` int(10) DEFAULT NULL,
    `product_id` int(10) DEFAULT NULL,
    `quantity` int(10) DEFAULT NULL,
    `price` decimal(18,2) DEFAULT NULL,
    `total_price` decimal(18,2) DEFAULT NULL,
    `created` datetime DEFAULT NULL,
    `modified` datetime DEFAULT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
    id  title   description  quantity  price   payment_method_id  user_id  created              modified             
------  ------  -----------  --------  ------  -----------------  -------  -------------------  ---------------------
     1  (NULL)  (NULL)              3  63.00                   1        1  2017-03-03 11:37:11  2017-03-03 11:37:11 
    id  sale_id  product_id  quantity  price   total_price  created              modified             
------  -------  ----------  --------  ------  -----------  -------------------  ---------------------
     1        1           1         2  24.00   48.00        2017-03-03 11:37:11  2017-03-03 11:37:11  
     2        1          49         1  15.00   15.00        2017-03-03 11:37:11  2017-03-03 11:37:11
销售详情

CREATE TABLE `sales` (
    `id` int(10) NOT NULL AUTO_INCREMENT,
    `title` varchar(255) DEFAULT NULL,
    `description` text,
    `quantity` int(10) DEFAULT NULL,
    `price` decimal(18,2) DEFAULT NULL,
    `payment_method_id` int(10) DEFAULT NULL,
    `user_id` int(10) DEFAULT NULL,
    `created` datetime DEFAULT NULL,
    `modified` datetime DEFAULT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
CREATE TABLE `sale_details` (
    `id` int(10) NOT NULL AUTO_INCREMENT,
    `sale_id` int(10) DEFAULT NULL,
    `product_id` int(10) DEFAULT NULL,
    `quantity` int(10) DEFAULT NULL,
    `price` decimal(18,2) DEFAULT NULL,
    `total_price` decimal(18,2) DEFAULT NULL,
    `created` datetime DEFAULT NULL,
    `modified` datetime DEFAULT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
    id  title   description  quantity  price   payment_method_id  user_id  created              modified             
------  ------  -----------  --------  ------  -----------------  -------  -------------------  ---------------------
     1  (NULL)  (NULL)              3  63.00                   1        1  2017-03-03 11:37:11  2017-03-03 11:37:11 
    id  sale_id  product_id  quantity  price   total_price  created              modified             
------  -------  ----------  --------  ------  -----------  -------------------  ---------------------
     1        1           1         2  24.00   48.00        2017-03-03 11:37:11  2017-03-03 11:37:11  
     2        1          49         1  15.00   15.00        2017-03-03 11:37:11  2017-03-03 11:37:11
我需要使用ajax将数据相应地插入到这些表中。首先,我将数组数据结构化为以下内容

这是我的数据

[
    'quantity' => 3,
    'price' => 63,
    'payment_method_id' => 1,
    'user_id' => 1,
    'sale_details' => [
        0 => [
            'product_id' => 1,
            'quantity' => 2,
            'price' => 24,
            'total_price' => 48
        ],
        1 => [
            'product_id' => 49,
            'quantity' => 1,
            'price' => 15,
            'total_price' => 15
        ]
    ]
]
SalesController.php

if ($this->request->is('ajax')) {

    $sales = $this->Sales->newEntity(
        $this->request->data(), 
        [
            'validate' => 'create',
            'associated' => [
                'SaleDetails' => ['validate' => 'create']
            ]
        ]
    );

    if ($this->Sales->save($sales)) {
        //code 
    }
}
我设法将数据插入到这些表中,但这两个表的主键随着2的增加而不断增加。我插入了3次数据。以下是数据的记录方式:-

销售

CREATE TABLE `sales` (
    `id` int(10) NOT NULL AUTO_INCREMENT,
    `title` varchar(255) DEFAULT NULL,
    `description` text,
    `quantity` int(10) DEFAULT NULL,
    `price` decimal(18,2) DEFAULT NULL,
    `payment_method_id` int(10) DEFAULT NULL,
    `user_id` int(10) DEFAULT NULL,
    `created` datetime DEFAULT NULL,
    `modified` datetime DEFAULT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
CREATE TABLE `sale_details` (
    `id` int(10) NOT NULL AUTO_INCREMENT,
    `sale_id` int(10) DEFAULT NULL,
    `product_id` int(10) DEFAULT NULL,
    `quantity` int(10) DEFAULT NULL,
    `price` decimal(18,2) DEFAULT NULL,
    `total_price` decimal(18,2) DEFAULT NULL,
    `created` datetime DEFAULT NULL,
    `modified` datetime DEFAULT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
    id  title   description  quantity  price   payment_method_id  user_id  created              modified             
------  ------  -----------  --------  ------  -----------------  -------  -------------------  ---------------------
     1  (NULL)  (NULL)              3  63.00                   1        1  2017-03-03 11:37:11  2017-03-03 11:37:11 
    id  sale_id  product_id  quantity  price   total_price  created              modified             
------  -------  ----------  --------  ------  -----------  -------------------  ---------------------
     1        1           1         2  24.00   48.00        2017-03-03 11:37:11  2017-03-03 11:37:11  
     2        1          49         1  15.00   15.00        2017-03-03 11:37:11  2017-03-03 11:37:11
销售详情

CREATE TABLE `sales` (
    `id` int(10) NOT NULL AUTO_INCREMENT,
    `title` varchar(255) DEFAULT NULL,
    `description` text,
    `quantity` int(10) DEFAULT NULL,
    `price` decimal(18,2) DEFAULT NULL,
    `payment_method_id` int(10) DEFAULT NULL,
    `user_id` int(10) DEFAULT NULL,
    `created` datetime DEFAULT NULL,
    `modified` datetime DEFAULT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
CREATE TABLE `sale_details` (
    `id` int(10) NOT NULL AUTO_INCREMENT,
    `sale_id` int(10) DEFAULT NULL,
    `product_id` int(10) DEFAULT NULL,
    `quantity` int(10) DEFAULT NULL,
    `price` decimal(18,2) DEFAULT NULL,
    `total_price` decimal(18,2) DEFAULT NULL,
    `created` datetime DEFAULT NULL,
    `modified` datetime DEFAULT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
    id  title   description  quantity  price   payment_method_id  user_id  created              modified             
------  ------  -----------  --------  ------  -----------------  -------  -------------------  ---------------------
     1  (NULL)  (NULL)              3  63.00                   1        1  2017-03-03 11:37:11  2017-03-03 11:37:11 
    id  sale_id  product_id  quantity  price   total_price  created              modified             
------  -------  ----------  --------  ------  -----------  -------------------  ---------------------
     1        1           1         2  24.00   48.00        2017-03-03 11:37:11  2017-03-03 11:37:11  
     2        1          49         1  15.00   15.00        2017-03-03 11:37:11  2017-03-03 11:37:11
正如您从表中注意到的,sales表id随着2的增加而增加,sale\u details表也是如此

我的问题如下:-

1) 我是CakePHP3的新手,所以这是将数据保存到多个表中的正确方法吗?我删除了以下几行,仍然能够将数据保存到这些表中。如何处理此处的“关联”

SalesController.php

if ($this->request->is('ajax')) {

    $sales = $this->Sales->newEntity(
        $this->request->data(), 
        [
            'validate' => 'create',
            /*'associated' => [
                'SaleDetails' => ['validate' => 'create']
            ]*/ // removed
        ]
    );

    if ($this->Sales->save($sales)) {
        //code 
    }
}
2) 我不明白为什么那些id会随着2的增加而增加。我很确定我已经为这两个表设置了auto_increment=1

系统变量
auto\u increment\u increment
已设置为2。但我不知道这是怎么发生的


提前感谢。

是的,这是保存关联的正确方法

即使未通过
associated
选项指定关联,您的关联仍在转换(并因此保存),因为默认情况下允许一级关联,即在删除该选项时,可以转换所有一级关联,并且在指定该选项时,如示例所示,只能转换
SaleDetails
关联

引用文件:

默认情况下,此表上的所有关联都将水合。您可以使用options参数限制生成的关联,或包括更深的关联

保存实体时,还可以选择保存部分或所有关联实体。默认情况下,将保存所有第一级实体

另见