I have two entities User\User
and Misc\Notification
and I want to have the possibility to fetch the user's notifications inside the User entity, by doing $user->getNotifications()
.
Usually I have no problem with this type of relations.
See my code below (I just added the TicketApp relation as an example, since this relation works and is exactly the same):
User\User : properties
/**
* This is working
* @ORM\OneToMany(targetEntity="[...]\Entity\Ticket\TicketApp", mappedBy="author")
*/
private $tickets;
/**
* This is not
* @ORM\OneToMany(targetEntity="[...]\Entity\Misc\Notification", mappedBy="receiver", fetch="EXTRA_LAZY")
*/
private $notifications;
User\User : __construct()
$this->tickets = new \Doctrine\Common\Collections\ArrayCollection();
$this->notifications = new \Doctrine\Common\Collections\ArrayCollection();
Misc\Notification : properties
/**
* @ORM\ManyToOne(targetEntity="[...]\Entity\User\User", inversedBy="notifications")
* @ORM\JoinColumn(nullable=false)
*/
private $receiver;
This seems to be Ok (or maybe this evening I'm completely blind...).
The problem is : $user->getNotifications()
returns null
instead of a Doctrine Collection
object.
I have datas in my table. This type of code works and returns two notifications:
$em->getRepository('[...]:Misc\Notification')->findByReceiver($user);
Also, I noticed in the Symfony debug toolbar that no query is fired when using $user->getNotifications()
.