How does Doctrine2 One-To-Many fetch=EAGER work? - symfony

I'm using Symfony 2.8 / Doctrine ORM 2.5.2.
I have 2 entities, Gallery OneToMany File
class Gallery
{
/**
* #var File[]
*
* #ORM\OneToMany(targetEntity="File", mappedBy="gallery", fetch="EAGER")
*/
private $files;
}
I see 2 things in the documentation.
First, now the OneToMany relationship does have the fetch=EAGER option (specified here). It was not there in previous versions.
Second, the manual setting for this fetch method per query seems not available for OneToMany but I don't know if the documentation is up-to-date as it states:
Changing the fetch mode during a query is only possible for one-to-one
and many-to-one relations.
I have anyway tried both, here is my query:
public function findWithEager()
{
$qb = $this->createQueryBuilder('g');
$query = $qb->getQuery();
$query->setFetchMode("CommonBundle\\Entity\\Gallery", "files", ClassMetadata::FETCH_EAGER);
return $query->getResult();
}
But when I do:
foreach ($galleryRepository->findWithEager() as $gallery) {
foreach ($gallery->getFiles() as $file) {
$file->getId();
}
}
Then I got 1+n queries. The first is SELECT * FROM Gallery and the n following ones are SELECT * FROM File WHERE id = :galleryId
I would like Doctrine to do 1+1 queries, the second one being SELECT * FROM File WHERE id IN (:galleryListIds)
Did I miss something? Is this behavior implemented in Doctrine?
The latest doctrine changelog states:
When marking a one-to-many association with fetch="EAGER" it will now
execute one query less than before and work correctly in combination
with indexBy.
It is not clear at all what is the expected behavior.
Any insight is welcome, thanks!

After much searching and some testing (using Doctrine ORM 2.5.6 on PHP 5.6) I have some results.
At this time it is not possible to get your Gallery entities with one query and all related File entities with a second query.
You have two options
Get Gallery and File entities in one query using Left Join.
This is what the ->find* methods do when you set fetch="EAGER" on your annotation.
You can do this manually with DQL: SELECT g, f FROM Gallery g LEFT JOIN g.files f
As noted in the docs, you cannot call ->setFetchMode('Gallery', 'files', ClassMetadata::FETCH_EAGER) on a DQL query to achieve the same result
... For one-to-many relations, changing the fetch mode to eager will cause to execute one query for every root entity loaded. This gives no improvement over the lazy fetch mode which will also initialize the associations on a one-by-one basis once they are accessed.
This will result in n additional queries being run immediately after your first query to fetch your Gallery entities.
Get Gallery entities with one query and lazy-load the File entities.
This is Doctrine's default behaviour (fetch="LAZY") and will result in 1 query plus an additional query for each set of Gallery#$files you access (1+n queries if you access them all).
Possible Future Option
There is a PR to add an EAGER_BATCHED fetch option which would do exactly what you want (fetch Gallery entities with one query then fetch all File entities with a second query) but there doesn't seem to be much happening with it, unfortunately.
Conclusion
If you're using the ->find* methods, fetch="EAGER" annotations will be respected. Doctrine will do this with a LEFT JOIN. Depending on your data set this could be ok or very expensive.
If you're writing DQL manually or using the query builder you must LEFT JOIN one-to-many relations AND remember to add any entities you want fetched to the select clause.
My preference is to write DQL whenever you want a one-to-many relation fetched eagerly because it makes your intention clear.

Related

Problem optimization with symfony 6 / doctrine : too many querys

The project is develop with symfony 6.0.8.
I have 2 entitys : utilisateur (user) & compte_client (customer) - link by OneToOne relation because an user may be not a customer. A user can be : an employee, a partner or a customer. The foreign-key is in the customer table (compte_client -> utilisateur_id)
Here i want to recover informations from users who are customers too. I use the queryBuilder, i recover user's informations with this :
$queryBuilder->andWhere('u.type_utilisateur = :val');
$queryBuilder->setParameter('val', $type);
$queryBuilder->join('u.compte_client','c')
->addSelect('c');
$queryBuilder->orderBy('u.login', 'ASC');
$query = $queryBuilder->getQuery();
$results = $query->getResult();
return $results;
I do a join to limit the number of querys but here, to recover 2 user's datas, it realized 7 querys.
If I delete the join request, it realise 3 querys, but i think it can be reduce to 1 query (as i see in online courses - that used join request too).
I tried to add some attributs on my entites like "fetch:EAGER" or "fetch:EXTRA_LAZY" but it's ineffective.
Before all of it, I had similar problem to recover data's from users that aren't customers. When i checked querys realized by doctrine for loading only users who are employee, their was so many useless querys to customer's table despite of I didn't try to recover any customer's information, and their was no result expected. To reduce the number of query's i had to add a join request like that :
$queryBuilder
->Where('u.type_utilisateur LIKE :type')
->setParameter('type', 'Entreprise%')
->leftjoin('u.compte_client','c')
->addSelect('c')
->orderBy('u.login', 'ASC');
$query = $queryBuilder->getQuery();
$results = $query->getResult();
return results;
This, reduce query at only 1.
So is there some good pratice to optimize the number of querys generat by doctrine ?
You have to think of Doctrine OneToOne as a form of inheritance: the inverse side "inherits" the owning side's properties (reminder: The owning side of a OneToOne association is the entity with the table containing the foreign key) so when you fetch the inverse side Doctrine always fetches the owning side.
That means the good practice here would be to change the owning side as it makes more sense for customer to inherit from user.

Find the right DQL

I have some difficulties in DQL (and SQL !). I always write simple querys but now I need something more specific and I don't know how to do it.
I have an entity User in OneToMany with my entity Program which is in OneToMany with my entity Exercice which is in OneToMany with my Reps.
I would like to know for an specific User all the reps is done.
What would be the best way to do it ?
$this->createQueryBuilder('user')
->select('rep')
->join('user.programs', 'program')
->join('program.exercices', 'exercice')
->join('exercice.reps', 'rep')
->getQuery()
->getResult();
Explication:
This code must be in your repository. The user repository one.
$this->createQueryBuilder('user')
is equivalent to
$this->_em->createQueryBuilder()
->select('user')
->from('user')
You can override the select with the second line:
->select('rep')
Then do the join as described in your question.
programs must be the entity attribute you defined in user to join program
Same goes for exercices and reps. They correspond to the inversed side of the relationship.
The second paramter in join() function is the alias. It can be anything you want. You use aliases in select and in the next join()

Doctrine, NativeQuery, Join and Limit for first entity

I used Doctrine and NativeQuery (setResultMapping). In my query, I use multiple joins to fetch entities that are related using OneToMany. I want to create pagination only for my first entity (For in Html, one col and many data in one row (oneToMany)).
If I use LIMIT 0, 20, joins multiply the result and distorts it.
Can you help me ?
With DQL queries, Doctrine's Paginator is your man - Doctrine Docs. What concerns NativeQuery there seems to be a solution in this post - How to paginate a native query in Doctrine 2?

Doctrine2 (Doctrine 2.1) eager loading in Symfony2

Let's say I have two entities in my Symfony2 project : Category and Article (a category having many articles).
In my CategoryRepository, I have this method:
findAllDummy(){
return $this->createQueryBuilder('c')
->leftJoin('c.Articles a')
->getQuery()->getResult();
}
If I remember well, in Symfony1.4 (and the corresponding version of Doctrine), the returned objects would have their 'articles' attribute filled by the corresponding Article objects.
Now, in Symfony2, Proxy objects are returned.
So if I loop through a specific category's articles, As many queries as iterations will be executed.
foreach($category->getArticles() as $article){
echo $article->getDoctrine()
->getRepository('')getTitle();
}
I understand this is Doctrine2.1's default lazy loading behavior.
Question 1: how is this a better solution?
N queries instead of 1.
I tried to force eager loading by doing the following:
findAllDummy(){
return $this->createQueryBuilder('c')
->leftJoin('c.articles a')
->getQuery()
->setFetchMode('Category', 'articles', 'EAGER')
->getResult();
}
But the result remains the same.
Question 2: how to force eager loading in Doctrine2?
You're joining a table but you're not selecting anything from it. Add ->addSelect('a') to your query builder. Consider two following SQL queries to understand the difference:
SELECT a.id, a.title
FROM article a
JOIN category c ON a.category_id = c.id
WHERE a.id = 123;
SELECT a.id, a.title, c.id, c.name
FROM article a
JOIN category c ON a.category_id = c.id
WHERE a.id = 123;
Eager/lazy joining has nothing to do with DQL queries. It defines what should be loaded when you use $articleRepository->find(123).
In the part where you try to "force eager loading" the problem might be that you use the fetchMode method with the wrong variable type for the $fetchMode argument. You pass a string 'EAGER' but the method doesn't expect a string but an integer.
The method expects constants from the ClassMetadata class:
/**
* Specifies that an association is to be fetched when it is first accessed.
*/
const FETCH_LAZY = 2;
/**
* Specifies that an association is to be fetched when the owner of the
* association is fetched.
*/
const FETCH_EAGER = 3;
In the Doctrine documentation chapter 14.7.6.6. Temporarily change fetch mode in DQL you can see an example on how to use this:
$query->setFetchMode("MyProject\User", "address", \Doctrine\ORM\Mapping\ClassMetadata::FETCH_EAGER);
So pass either a reference to the constant or an integer that corresponds to the mode you want to use.
As it says in the doctrine docs, eager loading in this case won't make any difference because you have one-to-many relationship between Category and Article.
For one-to-many relations, changing the fetch mode to eager will cause to execute one query for every root entity loaded. This gives no improvement over the lazy fetch mode which will also initialize the associations on a one-by-one basis once they are accessed.
So contrary to what #Crozin has said, you can still do eager loading in DQL.
If you have a one-to-one or many-to-one relationship, eager loading will solve the problem of making extra queries. However to solve your problem in this case you should use ->addSelect('a') as #Crozin mentioned.
It is a better solution because joins are a much more expensive process than a simple query. While it may seem inefficient, it isn't much of a waste, and quickly becomes more efficient when you aren't loading every bit of every related object.

Nhibernate -- Excuting some simple HQL

I have map the entities in .hmb.xml and define attribute for all entity in classes.
I have some basic accomplishment and get all the record using below code.
public List<DevelopmentStep> getDevelopmentSteps()
{
List<DevelopmentStep> developmentStep;
developmentStep = Repository.FindAll<DevelopmentStep>(new OrderBy("Id", Order.Asc));
return developmentStep;
}
I have check from net that we can write HQL, Now the problem is how to execute this HQL like..
string hql = "From DevelopmentSteps d inner join table2 t2 d.id=t2.Id where d.id=IDValue";
What additional Classes or other thing I need to add to execute this kind of HQL?
Please help me ---- Thanks
To write dynamic queries, I recommend using the Criteria API. This is dynamic, because you have a single query for several different types and you also want to set the ordering dynamically.
The queries are always object oriented. You don't need to join by foreign keys, you just navigate through the class model. There also no "tables" in the queries, but entities.
Getting (single) instances by ID should always be done using session.Get (or session.Load). Only then NHibernate can also take it directly from the cache without database roundtrip, it it had already been loaded.
for instance:
public IList<T> GetAll<T>(string orderBy)
{
return session.CreateCriteria(typeof(T))
.AddOrder(Order.Asc(orderBy))
.List<T>();
}

Resources