(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 exportmwb to 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的orm:schema-tool:create function创建表时
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:
因此,逻辑上,我尝试将我的数据库模型更改为一个单独的表,其中包含要存储和产品的关系。我还重写了字段名,以便能够排除这个问题的来源:
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:
当我创建数据库时,它仍然没有给我在stock表中的正确字段:
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?
我在这里做错了什么?
2 个解决方案
#1
125
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.
这也是为什么多对多关联如此罕见的原因:您倾向于在它们中存储其他属性,例如排序、数量等等。
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;
}
#2
16
Doctrine handles many-to-many relationships just fine.
学说可以处理多对多的关系。
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
- StockLevel
- Store
- 商店
and two associations:
和两个协会:
- Product oneToMany StockLevel
- 产品对StockLevel
- Store oneToMany StockLevel
- 商店oneToMany StockLevel
#1
125
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.
这也是为什么多对多关联如此罕见的原因:您倾向于在它们中存储其他属性,例如排序、数量等等。
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;
}
#2
16
Doctrine handles many-to-many relationships just fine.
学说可以处理多对多的关系。
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
- StockLevel
- Store
- 商店
and two associations:
和两个协会:
- Product oneToMany StockLevel
- 产品对StockLevel
- Store oneToMany StockLevel
- 商店oneToMany StockLevel