简体   繁体   English

CakePHP:连接表具有额外字段的habtm

[英]CakePHP: habtm with join table having an extra field

I am generating model tables with cake bake utility, and I am facing a strange situation. 我正在使用蛋糕烘焙实用程序生成模型表,但我面临着一个奇怪的情况。

I have these tables: 我有这些表:

  • product(id_product)
  • guest(id_guest)
  • product_guest(product, guest, amount)

where product_guest table keep trace of what product a guest bought, and the amount of product bought. 其中product_guest表可记录客人购买了哪些产品以及购买的产品数量。

Now, with HABTM relationship on cake bake, I can only specify relation between tables product and guest, but there's no trace of the amount field. 现在,有了蛋糕烘焙时的HABTM关系,我只能指定表产品和来宾之间的关系,但是没有amount字段的痕迹。

How can I map this situation correctly to retrieve and add amount too? 如何正确映射这种情况以检索和添加amount

In your case, HABTM is not the correct association to use. 就您而言,HABTM不是正确的关联。 You should be using the hasMany through association. 您应该通过关联使用hasMany

The fundamental change is that you need a third model based on your product_guest table. 根本的变化是您需要基于product_guest表的第三个模型。

Create these relationships: 创建这些关系:

  • Product hasMany ProductGuest 产品有很多产品客人
  • Guest hasMany ProductGuest 访客有很多产品访客
  • ProductGuest belongsTo Product 产品访客所属产品
  • ProductGuest belongsTo Guest 产品访客属于来宾

Wrong table structure 1st of all 表格结构错误第1个

You should have 你应该有

guests : id , user_name , password 

This is the guest who will purchase product . 这是要购买产品的客人。 This is master table. 这是主表。

products : id , product_name , description 

This is your product table with details regarding to products like description etc. This is master table. 这是您的产品表,其中包含有关产品的详细信息,例如说明等。这是主表。

orders -> id , amount , user_id

Here order is a table with details such as who bought product and price. 这里的订单是一张表格,其中包含谁购买了产品和价格等详细信息。 This is master table. 这是主表。

Then one order can have many Items so 那么一个订单可以有很多物品

orders_products ->  order_id , product_id 

Now this will contain order id and which product was included as foreign key. 现在,它将包含订单ID以及包含哪些产品作为外键。 This is Child table which contains Foreign keys of product and order. 这是子表,其中包含产品和订单的外键。

Here orders_products is HABTM with products And orders as 在这里,orders_products是具有产品和订单的HABTM,

One order can contain multiple products.And one product can be part of multiple orders. 一个订单可以包含多个产品,而一个产品可以是多个订单的一部分。 Does it make sense ? 是否有意义 ?

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

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