Doctrine 2 - Same Entity Multiple Join
I'm newbie in doctrine. I've got two tables messages and users. User entity have two joins, fromMessages and toMessages. Message entity have two joins, fromUser and toUser. My annotation is as follows:
User Entity:
/**
* @param \Doctrine\Common\Collections\Collection $property
* @OneToMany(targetEntity="Message", mappedBy="fromUser", cascade={"persist", "remove"})
*/
protected $fromMessages;
/**
* @param \Doctrine\Common\Collections\Collection $property
* @OneToMany(targetEntity="Message", mappedBy="toUser", cascade={"persist", "remove"})
*/
protected $toMessages;
Message Entity:
/**
* @var User
* @ManyToOne(targetEntity="User", inversedBy="fromMessages")
* @JoinColumns({
* @JoinColumn(name="fromUserId", referencedColumnName="id")
* })
*/
protected $fromUser;
/**
* @var User
* @ManyToOne(targetEntity="User", inversedBy="toMessages")
* @JoinColumns({
* @JoinColumn(name="toUserId", referencedColumnName="id")
* })
*/
protected $toUser;
I'm persist a message and after read messages with DQL("select u FROM App\Entities\User u") fromMessages is work but toMe开发者_Go百科ssages always return null. How can fix this problem?
Sorry my bad english. Thanks.
Are you setting the value of toMessages on the 'owning' side of the relationship? (That is, the many side of the @OneToMany ? As per the Doctrine 2 docs, only the owning side is persisted - the inverse side is discarded/ignored.
精彩评论