我正在尝试保存订单以及订单中的产品.
I am trying to save an order, and the products in the order.
订单正在保存,但产品没有保存.
The order is being saved, but the products are not.
我有一个 orders 表和一个 products 表和一个 orders_products 表.
I have an orders table and a products table and a orders_products table.
在 Order 模型中我设置了 $hasAndBelongsToMany = 'Product';
In the Order model I set $hasAndBelongsToMany = 'Product';
在 orders_products 表上,我有几个额外的字段:order_id、product_id 加上 price、quantity 以获取销售价格和销售数量.
on the orders_products table I have a couple extra fields: order_id, product_id plus price, quantity to capture the sale price and quantity sold.
我通过以下方式保存数据:
I am saving the data via:
$this->Order->saveAll($data);
$this->Order->saveAll($data);
这里是 $data 是:
Here is what $data is:
Array
(
[Order] => Array
(
[user_email] => st@kr.com
[billing_first] => Steve
... //more excluded
[total] => 5000
)
[Product] => Array
(
[0] => Array
(
[id] => 1
[price] => 5000.00
[quantity] => 1
)
)
)
订单已保存到订单表中,但没有任何内容保存到 orders_products 表中.我希望 orders_products 表保存 [new_order_id], 1, 5000.00, 1
The order gets saved to the order table but nothing is getting saved to the orders_products table. I am expected the orders_products table to save [new_order_id], 1, 5000.00, 1
我确实收到了这个通知:
I do get this notice:
Notice (8): Undefined index: id [CORE/cake/libs/model/model.php, line 1391]
Model::__saveMulti() - CORE/cake/libs/model/model.php, line 1391
Model::save() - CORE/cake/libs/model/model.php, line 1355
Model::__save() - CORE/cake/libs/model/model.php, line 1778
Model::saveAll() - CORE/cake/libs/model/model.php, line 1673
CartsController::saveOrder() - APP/controllers/carts_controller.php, line 128
CartsController::checkout() - APP/controllers/carts_controller.php, line 172
Dispatcher::_invoke() - CORE/cake/dispatcher.php, line 204
Dispatcher::dispatch() - CORE/cake/dispatcher.php, line 171
[main] - APP/webroot/index.php, line 83
有什么想法吗?
HABTM 超卖.很多时候它无法满足需求,例如当您有额外的数据要存储时.您最好在模型之间建立 hasMany/belongsTo 关系.
HABTM is over-sold. A lot of the times it fails to meet the needs, such as when you have additional data to store. You'll be better off to do a hasMany/belongsTo relationship between the models.
摘自 CakePHP 手册:
Taken from the CakePHP Book:
当 HABTM 变成复杂吗?
默认情况下,当保存一个HasAndBelongsToMany 关系,蛋糕将删除连接表上的所有行在保存新的之前.例如如果你有一个有 10 个孩子的俱乐部联系.然后你更新俱乐部有2个孩子.俱乐部只会有 2 个孩子,而不是 12 个.
By default when saving a HasAndBelongsToMany relationship, Cake will delete all rows on the join table before saving new ones. For example if you have a Club that has 10 Children associated. You then update the Club with 2 children. The Club will only have 2 Children, not 12.
另外请注意,如果您想添加更多加入的字段(当它是创建或元信息)这是可以使用 HABTM 连接表,但是重要的是要了解您有一个简单的选择.
Also note that if you want to add more fields to the join (when it was created or meta information) this is possible with HABTM join tables, but it is important to understand that you have an easy option.
两个模型之间的 HasAndBelongsToMany实际上是三个的简写通过两者关联的模型hasMany 和一个belongsTo 关联.
HasAndBelongsToMany between two models is in reality shorthand for three models associated through both a hasMany and a belongsTo association.
在您的情况下,我建议制作一个 LineItem 模型并以这种方式加入所有内容:
In your case I would suggest making a LineItem model and joining everything that way:
Order hasMany LineItemLineItem属于Order,ProductOrder hasMany LineItemLineItem belongsTo Order, Product这篇关于用额外的字段保存 HABTM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
不能使用 'Object 作为类名,因为它是保留的Cannot use #39;Object as class name as it is reserved Cake 2.2.x(不能使用 Object 作为类名,因为它是保留的 Cake 2.2.x)
OAuth 重定向后会话丢失Session is lost after an OAuth redirect(OAuth 重定向后会话丢失)
Cakephp 3.x 中的分页排序Pagination Sort in Cakephp 3.x(Cakephp 3.x 中的分页排序)
CakePHP 多个应用程序的共享核心CakePHP Shared core for multiple apps(CakePHP 多个应用程序的共享核心)
在 CakePHP 3 上登录 [ Auth->identify() ] 始终为 falLogin [ Auth-gt;identify() ] always false on CakePHP 3(在 CakePHP 3 上登录 [ Auth-identify() ] 始终为 false)
致命错误:允许的内存大小为 134217728 字节已用尽Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 87 bytes)(致命错误:允许的内存大小为 134217728 字节已用尽