Convert doctrine2 overwrites inversedBy inherited field from MappedSuperclass

another question. I have an abstract BaseLog Entity object that communicates with my user. In addition, I have 2 objects (FooLog and BarLog) that extend BaseLog. In addition, I have a User Entity, which supposedly should contain two associations in Log. One for FooLog and one for BarLog. Here is my problem. I get error messages because I don’t know how to overwrite the BaseLog inversedBy field in the Entity extension. Could you help me.

Because I think my explanation is not very good, here is the setup of my entities.

Baselog

/** @ORM\MappedSuperclass */ abstract class BaseLog { /** * @ORM\ManyToOne(targetEntity="User", inversedBy="logs") * @ORM\JoinColumns({ * @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=true, onDelete="SET NULL") * }) */ private $user; } 

Foolog

 /** @ORM\Entity */ class FooLog extends BaseLog { // Some additional fields } 

Barlog

 /** @ORM\Entity */ class BarLog extends BaseLog { // Some additional fields } 

User

 /** @ORM\Entity */ class User { /** * @ORM\OneToMany(targetEntity="FooLog", mappedBy="user", cascade={"persist"}) */ private $fooLogs; /** * @ORM\OneToMany(targetEntity="BarLog", mappedBy="user", cascade={"persist"}) */ private $barLogs; } 

How do I rewrite BaseLog inversedBy in FooLog and BarLog.

I get a few matching errors in this setting: BaseLog

  • BaseLog:. The user of the BaseLog # association refers to reverse side-field custom logs that do not exist.
  • FooLog:. The user association FooLog # refers to backward field logs User # that do not exist.
  • BarLog: The BarLog # association user refers to back side field logs User # that do not exist.
  • User: Mappings User # fooLogs and user FooLog # are not compatible with each other.
  • User: The mappings of User # barLogs and user BarLog # are not compatible with each other.

Please help me sort the sort.

+7
source share
3 answers

I had a similar problem with single inheritance. I solved this by defining the same association in both entity classes (parent and inherited), but with a different name. In your case, you can try the following:

  /** @ORM\Entity */ class FooLog extends BaseLog { /** * @ORM\ManyToOne(targetEntity="User", inversedBy="foologs") * @ORM\JoinColumns({ * @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=true, onDelete="SET NULL") * }) */ private $user; } 

and in the BarLog class:

 /** @ORM\Entity */ class BarLog extends BaseLog { /** * @ORM\ManyToOne(targetEntity="User", inversedBy="barlogs") * @ORM\JoinColumns({ * @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=true, onDelete="SET NULL") * }) */ private $bar_user; } 

Pay attention to another name ($ bar_user). You must also define the foologs and barlogs properties in the user entity. This eliminates doctrine validation errors.

+4
source

I ran into the same problem using Inheritance of individual tables . As far as I can tell, there is no solution. Even if inversedBy is considered mandatory, it seems you can get away from it by ignoring it. However, since Doctrine documents show that productivity is deteriorating rapidly.

@AssociationOverride does not help, because you cannot change association definitions, only database column properties.

I have tried several options, none of which are satisfactory.

Update: I still could not find a solution to such errors when running app/console doctrine:schema:validate .

 [Mapping] FAIL - The entity-class 'Acme\AcmeBundle\Entity\CourseLocation' mapping is invalid: * The field Acme\AcmeBundle\Entity\CourseLocation#facilitators is on the inverse side of a bi-directional relationship, but the specified mappedBy association on the target-entity Acme\AcmeBundle\Entity\CourseFacilitatorResponsibility#courseLocation does not contain the required 'inversedBy' attribute. 

In this case, CourseFacilitatorResponsibility is a subclass (with one table inheritance) of CourseResponsibility . CourseLocation refers to several CourseResponsibility entities. I think that unidirectional associations are the only way around it.

+3
source

IIRC was not a good way to override mappings in older versions of Doctrine. Doctrine> = 2.2 has something called association redefinition , so maybe you can use it.

BTW Why don't you want to move associations from the database to specific classes and define valid inversedBy?

+1
source

All Articles