带有额外字段的 Doctrine 2 和多对多链接表 [英] Doctrine 2 and Many-to-many link table with an extra field
问题描述
(抱歉我的问题语无伦次:我在写这篇文章时试图回答一些问题,但在这里:)
(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).
我使用 exportmwb 生成两个实体商店和产品这个简单的例子,都显示在下面.
I've used exportmwb to generate the two Entities Store and Product for this simple example, both are displayed below.
然而,现在的问题是我无法弄清楚如何使用 Doctrine 访问 stock.amount 值(有符号整数,因为它可以是负数).此外,当我尝试使用学说的 orm:schema-tool:create 函数创建表时
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
这仅产生了两个实体和三个表,一个作为没有值的链接表和两个数据表,因为多对多关系本身不是实体,所以我只能将 Product 和 Store 作为实体.
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.
因此,从逻辑上讲,我尝试更改我的数据库模型,将库存作为一个单独的表,并与商店和产品建立关系.我还重写了字段名,以便能够将其排除为问题的根源:
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:
然后我发现我仍然没有得到 Stock 实体......而且数据库本身没有amount"字段.
Then what I found was that I still didn't get a Stock entity... and the database itself didn't have an 'amount'-field.
我真的需要能够将这些商店和产品绑定到一个库存表中(除其他外)......所以只在产品本身上添加库存不是一种选择.
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.
我在这里做错了什么?
推荐答案
具有附加值的多对多关联不是多对多,但确实是一个新实体,因为它现在有一个标识符 (连接实体的两个关系)和值.
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.
这也是多对多关联如此罕见的原因:您倾向于在其中存储额外的属性,例如sorting
、amount
等.
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.
您可能需要的是以下内容(我将两个关系设为双向,考虑至少将其中一个设为单向):
What you probably need is something like following (I made both relations bidirectional, consider making at least one of them uni-directional):
产品:
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;
}
商店:
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;
}
库存:
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;
}
这篇关于带有额外字段的 Doctrine 2 和多对多链接表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!