php 带有额外字段的 Doctrine 2 和多对多链接表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15616157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Doctrine 2 and Many-to-many link table with an extra field
提问by Henry van Megen
(Sorry for my incoherent question: I tried to answer some questions as I was writing this post, but here it is:)
(抱歉我的问题语无伦次:我在写这篇文章时试图回答一些问题,但在这里:)
I'm trying to create a database model with a many-to-many relationship inside a link table, but which also has a value per link, in this case a stock-keeping table. (this is a basic example for more problems I'm having, but I thought I'd just test it with this before I would continue).
我正在尝试在链接表中创建一个具有多对多关系的数据库模型,但每个链接也有一个值,在这种情况下是一个库存表。(这是我遇到的更多问题的基本示例,但我想我会在继续之前用它进行测试)。
I've used exportmwbto generate the two Entities Store and Product for this simple example, both are displayed below.
我已经使用exportmwb为这个简单的例子生成了两个实体商店和产品,两者都显示在下面。
However, the problem now is that I can't figure out how to access the stock.amount value (signed int, as it can be negative) using Doctrine. Also, when I try to create the tables using doctrine's orm:schema-tool:create function
但是,现在的问题是我无法弄清楚如何使用 Doctrine 访问 stock.amount 值(有符号整数,因为它可以是负数)。此外,当我尝试使用学说的 orm:schema-tool:create 函数创建表时
This yielded only two Entities and three tables, one as a link table without values and two data tables, as many-to-many relationships aren't entities themselves so I can only have Product and Store as an entity.
这仅产生了两个实体和三个表,一个作为没有值的链接表和两个数据表,因为多对多关系本身不是实体,所以我只能将 Product 和 Store 作为实体。
So, logically, I tried changing my database model to have stock as a separate table with relationships to store and product. I also rewrote the fieldnames just to be able to exclude that as a source of the problem:
因此,从逻辑上讲,我尝试更改我的数据库模型,将库存作为一个单独的表,并与商店和产品建立关系。我还重写了字段名,以便能够将其排除为问题的根源:
Then what I found was that I still didn't get a Stock entity... and the database itself didn't have an 'amount'-field.
然后我发现我仍然没有得到 Stock 实体……而且数据库本身没有“金额”字段。
I really needed to be able to bind these stores and products together in a stock table (among other things)... so just adding the stock on the product itself isn't an option.
我真的需要能够将这些商店和产品绑定到一个库存表中(除其他外)......所以只在产品本身上添加库存不是一种选择。
root@hdev:/var/www/test/library# php doctrine.php orm:info
Found 2 mapped entities:
[OK] Entity\Product
[OK] Entity\Store
And when I create the database, it still doesn't give me the right fields in the stock table:
当我创建数据库时,它仍然没有给我股票表中的正确字段:
So, looking up some things here, I found out that many-to-many connections aren't entities and thus cannot have values. So I tried changing it to a separate table with relationships to the others, but it still didn't work.
所以,在这里查找一些东西,我发现多对多连接不是实体,因此不能有值。因此,我尝试将其更改为与其他表有关系的单独表,但仍然无效。
What am I doing wrong here?
我在这里做错了什么?
回答by Ocramius
A Many-To-Many association with additional values is not a Many-To-Many, but is indeed a new entity, since it now has an identifier (the two relations to the connected entities) and values.
具有附加值的多对多关联不是多对多,但确实是一个新实体,因为它现在具有标识符(与连接实体的两个关系)和值。
That's also the reason why Many-To-Many associations are so rare: you tend to store additional properties in them, such as sorting
, amount
, etc.
这也是为什么许多-to-many关联是如此罕见:你会存储在他们的附加属性,如sorting
,amount
等。
What you probably need is something like following (I made both relations bidirectional, consider making at least one of them uni-directional):
您可能需要的是以下内容(我将两个关系设为双向,考虑至少将其中一个设为单向):
Product:
产品:
namespace Entity;
use Doctrine\ORM\Mapping as ORM;
/** @ORM\Table(name="product") @ORM\Entity() */
class Product
{
/** @ORM\Id() @ORM\Column(type="integer") */
protected $id;
/** ORM\Column(name="product_name", type="string", length=50, nullable=false) */
protected $name;
/** @ORM\OneToMany(targetEntity="Entity\Stock", mappedBy="product") */
protected $stockProducts;
}
Store:
店铺:
namespace Entity;
use Doctrine\ORM\Mapping as ORM;
/** @ORM\Table(name="store") @ORM\Entity() */
class Store
{
/** @ORM\Id() @ORM\Column(type="integer") */
protected $id;
/** ORM\Column(name="store_name", type="string", length=50, nullable=false) */
protected $name;
/** @ORM\OneToMany(targetEntity="Entity\Stock", mappedBy="store") */
protected $stockProducts;
}
Stock:
库存:
namespace Entity;
use Doctrine\ORM\Mapping as ORM;
/** @ORM\Table(name="stock") @ORM\Entity() */
class Stock
{
/** ORM\Column(type="integer") */
protected $amount;
/**
* @ORM\Id()
* @ORM\ManyToOne(targetEntity="Entity\Store", inversedBy="stockProducts")
* @ORM\JoinColumn(name="store_id", referencedColumnName="id", nullable=false)
*/
protected $store;
/**
* @ORM\Id()
* @ORM\ManyToOne(targetEntity="Entity\Product", inversedBy="stockProducts")
* @ORM\JoinColumn(name="product_id", referencedColumnName="id", nullable=false)
*/
protected $product;
}
回答by timdev
Doctrine handles many-to-many relationships just fine.
Doctrine 可以很好地处理多对多关系。
The problem that you're having is that you don't need a simple ManyToMany association, because associations can't have "extra" data.
您遇到的问题是您不需要简单的多对多关联,因为关联不能有“额外”数据。
Your middle (stock) table, since it contains more than product_id and store_id, needs its own entity to model that extra data.
您的中间(库存)表,因为它包含的不仅仅是 product_id 和 store_id,需要它自己的实体来建模额外的数据。
So you really want three classes of entity:
所以你真的想要三类实体:
- Product
- StockLevel
- Store
- 产品
- 库存水平
- 店铺
and two associations:
和两个协会:
- Product oneToMany StockLevel
- Store oneToMany StockLevel
- 产品一对多库存水平
- 存储 oneToMany StockLevel