0

如果有可能通过教义制造某些东西,我正在徘徊......我会尝试解释:你有一个像人类一样的物体。他/她有它的属性,如身高、体重等。现在我们有 2 个表“humans”和“human_attributes”。对于多对多关系,我们必须创建第三个表“human2human_attributes”,该表由学说自行生成,其中包含所列表中的 id-s。我想要的是在两个 id 字段旁边放一个值字段,例如

id_human  id_human_attribute value
   2              12          180cm

转换为人类 id-2 的属性 id-12(height) 值为 180cm。

是否有可能在原则上做这样的事情,如果有人知道如何在 Symfony2 中实现它?

谢谢。

4

1 回答 1

1

如果要添加不能使用 ManyToMany 关系但不能使用中间实体的第三个字段,则无法选择使用 dictionary2:

/**
 * @ORM\Entity()
 * @ORM\Table(name="human_has_attribute",
 *     uniqueConstraints = {
 *         @ORM\UniqueConstraint(name="unique_human_human_attribute", columns={"human_id", "human_attribute_id"})
 *     }
 * )
 */
class HumanHasAttritube
{
    // ...

    /**
     * @var Human
     * 
     * @ORM\ManyToOne(targetEntity="Human", inversedBy="humanHasAttributes")
     * @ORM\JoinColumn(name="human_id", referencedColumnName="id")
     */
    protected $human;

    /**
     * @var HumanAttribute
     * 
     * @ORM\ManyToOne(targetEntity="HumanAttribute", inversedBy="humanHasAttributes")
     * @ORM\JoinColumn(name="human_attribute_id", referencedColumnName="id")
     */
    protected $humanAttribute;

    /**
     * @var string
     *
     * @ORM\Column(type="string", length=255)
     */
    protected $value;
}

请注意,由于@ORM\Table 的 uniqueConstraints 参数,我添加了一个唯一的 SQL 约束,即 human 和 humanAttribute 都是唯一的(如果它尊重您的系统的逻辑,否则删除有关唯一性的行,或者还添加 value 字段,或者你想要什么^^)!

验证时不要忘记添加唯一验证器(如果它是唯一的):

 * @UniqueEntity(
 *      fields={"human", "humanAttribute"},
 *      message="A human can't have the same attribute 2 times."
 * )
class HumanHasAttribute
于 2013-06-03T11:44:28.007 回答