Using left join with query builder doctrine - symfony

Having some problems retrieving ManytoOne relationships when using left join.
Before was using this query to query for conferences
$qb = $this->createQueryBuilder('u')
->select('u.id,u.comment,
IDENTITY(u.place) AS place_id,
IDENTITY(u.sponsor) AS sponsor_id,
IDENTITY(u.tour) AS tour_id,
u.startat
');
Now I'm trying to left join with diffusion which is tied to the diffusion in a many to many relationship.
$qbt = $this->createQueryBuilder('u')
->select('u','c')
->from('AppBundle:Conference', 'p')
->leftJoin('p.diffusion', 'c');
However this query doesn't return the u.place, u.sponsor and u.tour which are ManyToOne relationships.

leftJoin must be followed by 'WITH'. So for example:
->leftJoin('p.diffusion', 'p', 'WITH', 'p.user=u.id', 'u.id');
But i think it's better to post both your entities so i can give you the exact answer.

Found the issue , I had to add
->setHint(\Doctrine\ORM\Query::HINT_INCLUDE_META_COLUMNS, true)
to the getQuery , because getArrayResults by default doesn't return foreign keys( the place, sponsor and tour respectivly).
Here is my final query in the conference repository
$qbt = $this->_em->createQueryBuilder();
$qbt->select('conference','diffusion')
->from('AppBundle:Conference', 'conference')
->leftJoin('conference.diffusion', 'diffusion');
return $qbt
->getQuery()
->setHint(\Doctrine\ORM\Query::HINT_INCLUDE_META_COLUMNS, true)
->useQueryCache(true)
->useResultCache(true,3600)
->getArrayResult();

Related

Use the Doctrine's QueryBuilder to get Categories with a minimum number of products

I want to use the Doctrine's QueryBuilder to get Categories with a specified attribute and a number minimum of products inside. I precise that's the first time I use this Doctrine's function. I discovered that and I realized that it's so much powerful than basics repositories functions. I'm a very beginner with SQL because I used to use Repository's functions.
But I think I achieve this in pure SQL:
SELECT category.*,COUNT(*)
FROM category_product
INNER JOIN category ON category_product.category_id = category.id
WHERE category.name = 'region'
GROUP BY category_product.category_id HAVING COUNT(*) > 20
At the moment I'm totally lost with DQL construction. My Category and Product Entities both have a Many to Many relationship and I can't reach traducing this to Doctrine. I tried using the category_product table auto-generated by doctrine's but It doesn't want to access this table...
$qb = $this->createQueryBuilder('c');
$qb
->select('c')
->where("c.name = 'region'")
->innerJoin('c.products', 'p', 'WITH', 'COUNT(c.products) > :minimum')
->setParameter('minimum', $minimum);
dump($qb->getQuery(), $qb->getQuery()->getResult());
Each Time I uses a new construction I have Semantical errors I'm not able to correct...
Thanks a lot for your help
You could try some thing like here (piece of code from my working model)
$qb = $this->createQueryBuilder('c')
->select('c')
->where('c.name = region')
->addSelect('COUNT(c.products) AS counter')
->innerJoin('c.products', 'p')
->groupby('p.id')
->having('count(p.id) >= :minimum')
->setParameter('region', $region)
->setParameter('minimum', $minimum)
;
dump($qb->getQuery()->getResult());
related post: here
Ok Thanks to you I got it.
I shouldn't select the COUNT because I typed the return to be an Array of categories but it returned an array of arrays with contains the category and the count... I had to group by c too.
Thanks a lot for your help !
The answer is :
$qb = $this->createQueryBuilder('c')
->select('c')
->where('c.name = :region')
->innerJoin('c.products', 'p')
->groupBy('c')
->having('SIZE(c.products) > :minimum')
->setParameter('region', $region)
->setParameter('minimum', 20);

Doctrine querybuilder returns not all records in one to many relation with leftJoin

I have an Ad entity that represents an advertisement. This Ad entity has a one too many relation with adRemark. Reason to do this is that the adRemark contains multiple records because of multi language support.
An Ad can have one only one adRemark per language but can mis records for language that are not filled in or can even have no adRemark record as no language data is filled in.
I'm building a query that retrieves all ads including the adRemark.
$query = $this->createQueryBuilder('ad')
->select('ad.id, ad.title, ad.year, ad.hours, ad.status')
->addSelect('rem.remark')
->leftJoin('ad.remark', 'rem')
->andWhere("rem.language = 'NL' or rem.language is null")
->getQuery()
->getResult();
With this query i'm getting all ads that have for example the dutch (NL) remark filled in or no adRemark records. But i'm missing the ads the have for example no NL adRemaks record but do have an EN or DE record.
I working this for hours but i'm not able to define a good query. Help is really appreciated.
Herby the sql dump:
"SELECT ad.id, ad.title, ad.year, ad.hours, ad.status, rem.remark FROM
Mtr\Bundle\Entity\Ad LEFT JOIN ad.remark rem WHERE (rem.language = 'NL' or rem.language is null)"
You do not want to filter all the result set, but only on the join, so move the condition to the join clause.
Change the query to:
use Doctrine\ORM\Query\Expr\Join;
$query = $this->createQueryBuilder('ad')
->select('ad.id, ad.title, ad.year, ad.hours, ad.status')
->addSelect('rem.remark')
->leftJoin('ad.remark', 'rem', Join::WITH, "rem.language = 'NL' OR rem.language is null")
->getQuery()
->getResult();
References
SQL join: where clause vs. on clause
Why and when a LEFT JOIN with condition in WHERE clause is not equivalent to the same LEFT JOIN in ON?
How to do left join in Doctrine?

Convert SQL into Doctrine 2 QueryBuilder

I need help to convert a valid SQL query into createQueryBuilder. The problem I have is I don't know how to LEFT JOIN on a SELECT in the createQueryBuilder.
SELECT username, count(c.user_owner_id) as num_contact, a_g.name as
group_name
FROM `oro_user` as u
INNER JOIN `oro_user_access_group` as u_g on u.id=u_g.user_id
INNER JOIN `oro_access_group` as a_g on u_g.group_id=a_g.id
LEFT JOIN
(SELECT cc.user_owner_id
FROM `orocrm_contact` as cc
INNER JOIN`orocrm_contact_to_contact_grp` as cc_g on cc_g.contact_id=cc.id
INNER JOIN `orocrm_contact_group`
as c_g on cc_g.contact_group_id=c_g.id
WHERE c_g.label='New One' and cc.semester_contacted='2017A')
as c on u.id=c.user_owner_id
WHERE a_g.name='Full-timer' and u.enabled = 1 and u.gender='male'
GROUP BY u.id
ORDER BY num_contact
I have two queries below, I want user to LEFT JOIN the results from contact
$user = $this->em->getRepository('OroUserBundle:User')->createQueryBuilder('u')
->select('u.username')
->innerJoin('u.groups','g')
->andWhere('g.name = :group')
->setParameter('group', 'Full-timer')
->getQuery();
$contacts = $this->em->getRepository('OroContactBundle:Contact')->createQueryBuilder('c')
->select('c')
->innerJoin('c.groups','g')
->andWhere('g.label = :group')
->andWhere('c.semester_contacted = :sem')
->setParameter('group', 'New One')
->setParameter('sem', '2017A')
->setMaxResults(1)
->getQuery();
This is a pretty complex queries and since the ORM QueryBuilder works closer to your entities than the database I'm not sure if you can just "dump" the DQL into a ->join(). The good news with the DBAL QueryBuilder that works:
$dbalQueryBuilder
->from('user_table as u')
...
->join('u', '('.$otherDbalQueryBuilder->getSQL().')', 'c')
This is from memory so it might be a little different, but something like that.
With that you can get all the fields you require, but you won't get any entities. Luckily Doctrine provides ways to build entities from Native SQL using ResultSetMapping.
$userWithContacts = $entityManager->createNativeQuery(
$dbalQueryBuilder->getSQL(),
$yourResultSetMapping
);
I know this is will require more code and is probably not as nice as just using the ORM QueryBuilder, but I find it oftentimes to be the best way to deal with existing queries that need to be ported to ORM somehow.

Doctrine left join on unrelated entity

Is there any way to do this. I'm getting more and more confused trying different things.
I have an entity conferences that can have a place.
Places are in a many to one relationship with city.
In my query I'm trying to retrieve the city info but can't seem to retrieve it in the same place result.
This is the query used:
$qbt = $this->_em->createQueryBuilder();
$qbt
->select('conference', 'diffusion', 'speaker', 'placediff', 'confcity')
->from('AppBundle:Conference', 'conference')
->leftJoin('conference.diffusion', 'diffusion')
->leftJoin('conference.speaker','speaker')
->leftJoin('conference.place','placediff')
->leftJoin('AppBundle:City', 'confcity', 'WITH', 'confcity.id = placediff.city');
return $qbt
->getQuery()
->setHint(\Doctrine\ORM\Query::HINT_INCLUDE_META_COLUMNS, true)
->useQueryCache(true)
->useResultCache(true, 3600)
->getArrayResult();
This is what is returns, currently have only one conference.
Would love to have the second array inside place though.
Any ideas on how to accomplish this? Much obliged ~
(UPDATE) In the meantime I switched to raw sql query but really looking for a way to do this using dql
public function rawConf()
{
$conn = $this->getEntityManager()->getConnection();
$sql = 'SELECT
c0_.id AS id,
c0_.startAt AS startat,
c0_.comment AS comment,
d1_.id AS diffusion_id,
d1_.hour AS diffusion_hour,
s2_.id AS speaker_id,
c0_.place_id AS place_id,
c0_.sponsor_id AS sponsor_id,
c0_.tour_id AS tour_id_8,
d1_.movie_id AS diffusion_movie_id,
s2_.contact_id AS speaker_contact_id,
c6_.name AS ville_name,
c6_.postal AS ville_post,
c6_.department as ville_depart
FROM
conference c0_
LEFT JOIN conference_diffusion c3_ ON c0_.id = c3_.conference_id
LEFT JOIN diffusion d1_ ON d1_.id = c3_.diffusion_id
LEFT JOIN conference_speaker c4_ ON c0_.id = c4_.conference_id
LEFT JOIN speaker s2_ ON s2_.id = c4_.speaker_id
LEFT JOIN place p5_ ON c0_.place_id = p5_.id
LEFT JOIN city c6_ ON (c6_.id = p5_.city_id)';
$stmt = $conn->prepare($sql);
$stmt->execute();
return $stmt->fetchAll();
}
You should get "city" from "place" as "$city" is a property of Place entity. Try this simplified query to see it works:
$qbt = $this->_em->createQueryBuilder();
$qbt
->select('conference')
->addSelect('place')
->addSelect('city')
->from('AppBundle:Conference', 'conference')
->innerJoin('conference.place', 'place')
->innerJoin('place.city', 'city')
;
And if it works - you could easily add more joins if you need.

Optimizing doctrine query using DQL

I use in my code a simple query that works fine in all the cases. This means that if the user has not a photo (for example) the query goes fine:
$user=$em->getRepository('UserBundle:User')->findOneById($id_user);
The entity User has a lot of relations with other entities, this is the reason to optimize the number of queries to avoid Doctrine's lazy loading. Then I make this query with DQL using QueryBuilder:
public function findUsuario($id_user){
$em = $this->getEntityManager();
$qb = $em->createQueryBuilder();
$qb->select('u, p, co, ci, f, s, q, b, r, se, com, t, beb, prof, v, h, i, idi, usf')
->from('UserBundle:User', 'u')
->innerJoin("u.pais", 'p')
->innerJoin("u.comunidad", 'co')
->innerJoin("u.ciudad", 'ci')
->innerJoin("u.fotos", 'f')
->innerJoin("u.sexo", 's')
->innerJoin("u.quiero", 'q')
->innerJoin("u.busco", 'b')
->innerJoin("u.relacionPareja", 'r')
->innerJoin("u.sexualidad", 'se')
->innerJoin("u.complexion", 'com')
->innerJoin("u.tabaco", 't')
->innerJoin("u.bebida", 'beb')
->innerJoin("u.profesion", 'prof')
->innerJoin("u.vivienda", 'v')
->innerJoin("u.hijo", 'h')
->innerJoin("u.ingreso", 'i')
->innerJoin("u.idiomas", 'idi')
->innerJoin("u.usuarioFavoritos", 'usf')
->where('u.id = :id')
->setParameter('id', $id_user);
$query= $qb->getQuery();
return $query->getSingleResult();
}
This works well when the user has information on all related entities, but if for example a user has no photo, the following exception occurs: "No result was found for query although at least one row was expected"
I don't understand why, can anyone shed some light? Thanks
An inner join should be used when you want all your rows to have data on the other side of the relationship, if no data exists on the other side of the relationship then the row will be omitted.
You can find more details about the different type of JOINs on this page.
Instead of the innerJoin method you will need to use the leftJoin method.
(Also, you have a bigger problem: you have way too many JOINs, I'd advise you to review the organization of your entities.)

Resources