phpsymfonydoctrine-ormzend-framework2

Doctrine2 challenge, I Have OneToMany, but NOT ManyToOne,


Have a messaging system, where messages can belong to a consultation, but it doesn't have to. All consultations do have 1 or more messages though...

My entities have been generated by Doctrine automatically, but vendor/bin/doctrine-module orm:validate-schema fails miserably...

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.

Definition of the associations in question:

From Messages / 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;

From my "Parent" Consultation Konsultasjon:

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

My konsultasjon is mapped with foreign trigger, but not my messages (because many of the konsultasjon is NULL).

Also there is wery little I can do about spelling, as this is a legacy and rather messy app...

Please let me know if you need anything else, and any help would be greatly appreciated.

[EDIT]

Removed @var and @column in Messages (Meldinger) as suggested in the comments, ended up with this result:

  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

Konsultasjon 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;

Messages 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;

Solution

  • When you don't use $id as Primary Key of your entity, you have to use JoinColumn annotation to configure relation between entities. JoinColumn.

    If you want to change column name of relation property you need to use JoinColumn instead of Column annotation.

    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.
    I also corrected @ManyToOneparameters for Meldinger.