You need to set the relationship loading to eager
, this will prevent lazy loading of objects with just an id
and nothing else.
You can find more information on eager loading and it's annotation here:
As for my code, this is how it looks like when defining a link to a User
now:
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="answers", fetch="EAGER")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $user;
In this case, the User
entity can have multiple answers
. When loading a User
from the answer
perspective, this will work:
foreach($answers as $answer) {
$user = $answer->getUser();
if (!$user) {
continue;
}
}