Doctrine2 挑战,我有 OneToMany,但没有 ManyToOne,

问题描述 投票:0回答:1

拥有一个消息系统,其中的消息可以属于咨询,但并非必须如此。不过,所有咨询确实都有 1 条或多条消息...

我的实体已由 Doctrine 自动生成,但

vendor/bin/doctrine-module orm:validate-schema
惨败......

vendor/bin/doctrine-module orm:validate-schema
[Mapping]  FAIL - The entity-class 'Legetimen\Entity\Konsultasjon' mapping is invalid:
* The association Legetimen\Entity\Konsultasjon#meldinger refers to the owning side field Legetimen\Entity\Meldinger#konsulatsjon which is not defined as association, but as field.
* The association Legetimen\Entity\Konsultasjon#meldinger refers to the owning side field Legetimen\Entity\Meldinger#konsulatsjon which does not exist.

[Database] FAIL - The database schema is not in sync with the current mapping file.

相关协会的定义:

来自消息/ Meldinger:

/**
 * @var integer
 *
 * @ORM\Column(name="konsulatsjon", type="integer", precision=0, scale=0, nullable=true, unique=false)
 * @ORM\ManyToOne(targetEntity="Konsultasjon", inversedBy="konsultasjonid")
 */
 private $konsulatsjon;

来自我的“家长”咨询咨询:

 /**
 * @var \Doctrine\Common\Collections\Collection
 *
 * @ORM\OneToMany(targetEntity="Legetimen\Entity\Meldinger", mappedBy="konsulatsjon", cascade={"persist"}, fetch="EAGER")
 */
 private $meldinger;

我的 konsultasjon 映射了外部触发器,但不是我的消息(因为许多 konsultasjon 为 NULL)。

此外,我对拼写无能为力,因为这是一个遗留且相当混乱的应用程序......

如果您需要任何其他信息,请告诉我,我们将不胜感激。

[编辑]

按照评论中的建议删除了消息(Meldinger)中的@var和@column,最终得到以下结果:

  vendor/bin/doctrine-module orm:validate-schema
[Mapping]  FAIL - The entity-class 'Legetimen\Entity\Konsultasjon' mapping is invalid:
* The mappings Legetimen\Entity\Konsultasjon#meldinger and Legetimen\Entity\Meldinger#konsulatsjon are inconsistent with each other.

[Mapping]  FAIL - The entity-class 'Legetimen\Entity\Meldinger' mapping is invalid:
* The association Legetimen\Entity\Meldinger#konsulatsjon refers to the inverse side field Legetimen\Entity\Konsultasjon#konsultasjonid which is not defined as association.
* The association Legetimen\Entity\Meldinger#konsulatsjon refers to the inverse side field Legetimen\Entity\Konsultasjon#konsultasjonid which does not exist.
* The referenced column name 'id' has to be a primary key column on the target entity class 'Legetimen\Entity\Konsultasjon'.

  [Doctrine\ORM\ORMException]
  Column name `id` referenced for relation from Legetimen\Entity\Meldinger towards Legetimen\Entity\Konsultasjon does not exist.

orm:validate-schema

咨询 ID:

 /**
 * @var integer
 *
 * @ORM\Column(name="konsultasjonid", type="integer", precision=0, scale=0, nullable=false,          unique=false)
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
 private $konsultasjonid;

消息 ID / Meldinger

/**
 * @var integer
 *
 * @ORM\Column(name="meldingerid", type="integer", precision=0, scale=0, nullable=false, unique=false)
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
 private $meldingerid;
php symfony doctrine-orm zend-framework2
1个回答
2
投票

当您不使用

$id
作为实体的主键时,您必须使用
JoinColumn
注解来配置实体之间的关系。 加入专栏.

如果要更改关系属性的列名称,您需要使用

JoinColumn
而不是
Column
注释。

class Meldinger { 
    /**
     * @var integer
     *
     * @ORM\Column(name="meldingerid", type="integer", precision=0, scale=0, nullable=false, unique=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
     private $meldingerid;

     /**
      * @var integer
      *
      * @ORM\ManyToOne(targetEntity="Konsultasjon", inversedBy="meldinger")
      * @JoinColumn(name="konsulatsjon", referencedColumnName="konsulatsjonid")
      */
     private $konsulatsjon;
 }

 class Konsultasjon { 
    /**
     * @var integer
     *
     * @ORM\Column(name="konsultasjonid", type="integer", precision=0, scale=0, nullable=false, unique=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $konsultasjonid;

    /**
     * @var \Doctrine\Common\Collections\Collection
     *
     * @ORM\OneToMany(targetEntity="Legetimen\Entity\Meldinger", mappedBy="konsulatsjon", cascade={"persist"}, fetch="EAGER") 
     * @JoinColumn(name="meldinger", referencedColumnName="meldingerid")
     */
    private $meldinger;
 }

PS.
我还更正了

@ManyToOne
Meldinger
参数。

© www.soinside.com 2019 - 2024. All rights reserved.