【问题标题】:When generating Entities from an Existing Database, how to modify / create manyToMany从现有数据库生成实体时,如何修改/创建 manyToMany
【发布时间】:2013-02-08 22:53:31
【问题描述】:

Here,有关于“如何从现有数据库生成实体”的解释。

我有一张桌子person。还有一张桌子address。一个人可以有许多不同的地址,一个地址可以与一个或多个人相关。所以这是一个“多对多”的关系。因此,我创建了一个名为 personaddress 的“中间”表,其中有一个 idPersonne 和 idAddress。

当启动一代时,一切都很好,但多对多关系(不止一个)。

PersonneAdresse:
    type: entity
    table: personne_adresse
    fields:
        id:
            id: true
            type: bigint
            nullable: false
            generator:
                strategy: IDENTITY
    manyToOne:
        idPersonne:
            targetEntity: Personne
            cascade: {  }
            mappedBy: null
            inversedBy: null
            joinColumns:
                id_personne:
                    referencedColumnName: id
            orphanRemoval: false
        idAdresse:
            targetEntity: Adresse
            cascade: {  }
            mappedBy: null
            inversedBy: null
            joinColumns:
                id_adresse:
                    referencedColumnName: id
            orphanRemoval: false
    lifecycleCallbacks: {  }

关于oneToMany的文档还不够(我需要一个例子来理解):

如果您的实体之间存在 oneToMany 关系,您将 需要编辑生成的 xml 或 yml 文件以在 oneToMany 的特定实体定义了 inversedBymappedBy 件。

而且我很确定修改不仅涉及 oneToMany,而且还涉及 manyToMany。 你能解释一下我应该做什么/修改才能正确生成相应的实体吗?

【问题讨论】:

    标签: symfony doctrine


    【解决方案1】:

    我是这样做的:我已经生成了整个文件,但我知道存在一些 ManyToMany 关系。所以我在相应的文件中“手动”声明了它们。然后我遇到了另一个问题。在文档中,代码示例并非 100% 正确。您必须添加“@ORM\”而不是简单的“@”。这是我的有效代码:

    /**
     * @ORM\ManyToMany(targetEntity="Adresse", inversedBy="personnes")
     * @ORM\JoinTable(name="personne_adresse")
     **/
    private $adresses;
    /**
     * @ORM\ManyToMany(targetEntity="Partenaire", inversedBy="personnes")
     * @ORM\JoinTable(name="personne_partenaire")
     **/
    private $partenaires;
    /**
     * @ORM\ManyToMany(targetEntity="Telephone", inversedBy="personnes")
     * @ORM\JoinTable(name="personne_telephone")
     **/
    private $telephones;
    
    public function __construct() {
        $this->adresses = new \Doctrine\Common\Collections\ArrayCollection();
        $this->partenaires = new \Doctrine\Common\Collections\ArrayCollection();
        $this->telephones = new \Doctrine\Common\Collections\ArrayCollection();
    }
    

    此外,您必须删除相应的 ManyToMany Php 文件,因为 Symfony 2(我猜)处理那些“中间”ManyToMany 表本身 => 无需声明。在我的示例中,我必须删除“PersonneAdresse.php”、“PersonnePartenaire.php”和“PersonneTelephone.php”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-05
      • 1970-01-01
      • 1970-01-01
      • 2016-01-02
      • 2011-08-15
      • 1970-01-01
      • 2015-08-04
      • 2015-03-28
      相关资源
      最近更新 更多