Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

doctrine2 mapping overwrite inherited inversedBy field from MappedSuperclass

another question. I have a abstract BaseLog Entity which keeps the association to my user. In addition I have 2 Entities (FooLog & BarLog) which extend BaseLog. In addition I have my User Entity which are suppose to hold two associations to Log. One for FooLog and one for BarLog. Here is my issue. I get error messages because I don't know how to overwrite BaseLog's inversedBy field in extending Entity. Could you please help me.

Because I think my explanation is not really good, here the Set up 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 have to overwrite BaseLog's inversedBy in FooLog & BarLog.

I get several Mapping error on this set up: BaseLog

  • BaseLog: The association BaseLog#user refers to the inverse side field User#logs which does not exist.
  • FooLog: The association FooLog#user refers to the inverse side field User#logs which does not exist.
  • BarLog: The association BarLog#user refers to the inverse side field User#logs which does not exist.
  • User: The mappings User#fooLogs and FooLog#user are incosistent with each other.
  • User: The mappings User#barLogs and BarLog#user are incosistent with each other.

Please help me to get my mapping sorted.

like image 615
dj_thossi Avatar asked Dec 12 '12 03:12

dj_thossi


1 Answers

I had similar issue with single inheritance. I resolved this by defining same association in both entity classes (parent and inherited) but with different name. In your case you can try this:

    /** @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 class BarLog:

/** @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;
    }

Notice different name ($bar_user). You also have to define foologs and barlogs properties in user entity. This removes doctrine validation errors.

like image 166
Vladatos Avatar answered Sep 28 '22 07:09

Vladatos