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
Please help me to get my mapping sorted.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With