[英]Matrix type mapping with Doctrine entities
I have a bit of an issue. 我有一个问题。 i am modeling a DB structure using Doctrine 2 entities and I got stuck with one relation.
我正在使用Doctrine 2实体对数据库结构进行建模,但我陷入了一种关系。
What I need to represent in the database is a matrix like table: 我需要在数据库中表示的是一个像表这样的矩阵:
| |A|B|C|D|
|A| x
|B|x x
|C| x x
|D| x
What the table shows is what offers are available with other offers (x=true, empty = false). 该表显示的是与其他要约(x = true,empty = false)可以使用的要约。 What I tried to do is a Many to Many relationship with doctrine, hoping that this will generate a mapping table, but Doctrine does not do that.
我试图做的是与教义之间的多对多关系,希望这会生成一个映射表,但是教义没有这样做。 Here is my entity:
这是我的实体:
/**
* @ORM\Entity @ORM\HasLifecycleCallbacks
* @ORM\Entity(repositoryClass="Core\Repository\Main")
* @ORM\Table(name="offer_availability")
*/
class OfferAvailability implements JsonSerializable
{
/**
* @var int
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
protected $id;
/**
* @var String
* @ORM\Column(type="string")
*/
protected $name;
/**
* @var \Doctrine\Common\Collections\Collection
* @ORM\ManyToMany(targetEntity="Core\Entity\Offers\OfferAvailability", mappedBy="availableWith", inversedBy="availableWith", fetch="EXTRA_LAZY")
*/
protected $availableWith;
I know I can create the mapping table entity manually and Use One to Many relations. 我知道我可以手动创建映射表实体并使用一对多关系。 But what I am wondering if there is a more clever way of approaching this.
但是我想知道是否有更聪明的方法来解决这个问题。
Never mind, i have found the issue. 没关系,我已经找到了问题。 Creating mappings in this case only confuses Doctrine.
在这种情况下,创建映射只会使Doctrine困惑。 Removing mapping and inversion compleely solved the problem:
删除映射和反转完全可以解决问题:
/**
* @var \Doctrine\Common\Collections\Collection
* @ORM\ManyToMany(targetEntity="Core\Entity\Offers\OfferAvailability", fetch="EXTRA_LAZY")
*/
protected $availableWith;
This generated a mapping table: 这生成了一个映射表:
------------------------------------
|offeravailablity_offeravailability|
------------------------------------
|offeravailability_source |
|offeravailability_target |
------------------------------------
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.