Answer the question
In order to leave comments, you need to log in
Doctrine: one-to-one relationship in annotations: why does lazy loading work immediately?
I have two entities with a one-to-one relationship described in annotations
/**
* @ORM\OneToOne(targetEntity="Subdomain", mappedBy="company")
*/
protected $subdomain;
...
/**
* @ORM\OneToOne(targetEntity="Company", inversedBy="subdomain")
* @ORM\JoinColumn(name="target_id", referencedColumnName="id")
*/
private $company;
$product = $this->getDoctrine()
->getRepository('AppBundle:Product')
->find($id);
$categoryName = $product->getCategory()->getName();
When you call $product->getCategory()->getName(), Doctrine silently makes a second query to find the Category that's related to this Product.
What's important is the fact that you have easy access to the product's related category, but the category data isn't actually retrieved until you ask for the category (ie it's "lazily loaded").
$repository = $this->get('doctrine')->getRepository('TestBundle:Company');
$qb = $repository->createQueryBuilder('c');
$qb
->where('(c.id = :id)')
->setParameter('id', 1);
$query = $qb->getQuery();
$val = $query->getOneOrNullResult();
Answer the question
In order to leave comments, you need to log in
This is a long known behavior, the answer is at the link .
Solution: Leave the relationship only on the owner side - it's the entity that has the relationship. In your case Subdomain
. Or change the owner.
Hi guys!
the question is not really clear. at least for me personally
, when selecting objects through find
, lazy loading is used: for AppBundle:Product
objects, all its fields are selected, and for all relations, proxy objects are formed, through which, through additional queries to the database, you can get the relationship objects themselves.
There is a nuance when using DQL:
maybe you missed something? in one case you are referring to AppBundle:Product
, in the second to TestBundle:Company
and you are talking about subdomain fetching
lazy loading always happens unless you use join
, or explicitly specify to fetch relation entities likeEAGER
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question