用额外的字段保存 HABTM?

时间:2023-01-19
本文介绍了用额外的字段保存 HABTM?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试保存订单以及订单中的产品.

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_idproduct_id 加上 pricequantity 以获取销售价格和销售数量.

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 LineItem
  • LineItem属于OrderProduct
  • Order hasMany LineItem
  • LineItem belongsTo Order, Product

这篇关于用额外的字段保存 HABTM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:Cron 调度器 CakePHP 2.0 下一篇:CakePHP 中的 base_url

相关文章

最新文章