本文介绍了使用额外字段保存HABTM?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在尝试保存订单以及订单中的产品。 订单正在保存,但产品不保存。 我有一个订单表和产品表和 orders_products 表。 在订单模型中,我设置 $ hasAndBelongsToMany ='Product'; orders_products 表上,我有一个额外的字段: order_id。 , product_id plus price , $ b $ b $ this-> Order-> saveAll($ data); p> 数组( [Order] => Array [user_email ] => [email protected] [billing_first] => Steve ... //更多已排除 [total] => 5000 ) [Product] => Array ( [0] => Array ( [id] => 1 [price] => 5000.00 [quantity ] => 1 ) ) ) 订单保存到订单表,但没有任何内容保存到orders_products表中。我期望orders_products表保存 [new_order_id],1,5000.00,1 : 注意(8):未定义的索引: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,第128行 CartsController :: checkout() - APP / controllers / carts_controller.php,第172行 Dispatcher :: _ invoke() - CORE / cake / dispatcher.php,行204 Dispatcher :: dispatch() - CORE / cake / dispatcher.php,行171 [main] - APP / webroot / index.php,第83行 任何想法?解决方案 HABTM已过售。很多时候它不能满足需要,比如当你有额外的数据要存储。你最好在模型之间做一个hasMany / belongsTo关系。 取自CakePHP Book: HABTM变成时该怎么办? b $ b HasAndBelongsToMany关系,Cake 将删除连接表上的所有行,然后保存新的行。例如,如果你有一个俱乐部有10个孩子相关联。然后您更新俱乐部与2个孩子。俱乐部将只有有2个孩子,而不是12个。 还要注意,如果你想添加更多字段到加入是创建的或元信息)这是可能与HABTM连接表,但重要的是要了解你有一个简单的选项。 HasAndBelongsTo两个模型之间的多个实际上是通过 hasMany和belongsTo关联关联的三个模型的简写。 在你的情况下,我建议创建一个 LineItem 模型,并以这种方式加入一切: 订单 hasMany LineItem LineItem belongsTo 订单,产品 / li> I am trying to save an order, and the products in the order.The order is being saved, but the products are not.I have an orders table and a products table and a orders_products table.In the Order model I set $hasAndBelongsToMany = 'Product';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);Here is what $data is:Array( [Order] => Array ( [user_email] => [email protected] [billing_first] => Steve ... //more excluded [total] => 5000 ) [Product] => Array ( [0] => Array ( [id] => 1 [price] => 5000.00 [quantity] => 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, 1I 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 1391Model::save() - CORE/cake/libs/model/model.php, line 1355Model::__save() - CORE/cake/libs/model/model.php, line 1778Model::saveAll() - CORE/cake/libs/model/model.php, line 1673CartsController::saveOrder() - APP/controllers/carts_controller.php, line 128CartsController::checkout() - APP/controllers/carts_controller.php, line 172Dispatcher::_invoke() - CORE/cake/dispatcher.php, line 204Dispatcher::dispatch() - CORE/cake/dispatcher.php, line 171[main] - APP/webroot/index.php, line 83Any ideas? 解决方案 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.Taken from the CakePHP Book: What to do when HABTM becomes complicated? 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. 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 between two models is in reality shorthand for three models associated through both a hasMany and a belongsTo association.In your case I would suggest making a LineItem model and joining everything that way:Order hasMany LineItemLineItem belongsTo Order, Product 这篇关于使用额外字段保存HABTM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-05 14:05