how to do a join in doctrine with createQuiry or DQL - symfony

What i want is to do is this query in doctrine :
SELECT *
FROM `reservation`
JOIN res_chauf ON reservation.reservation_id = res_chauf.reservation_id
WHERE res_chauf.chauffeur_id =1
in doctrine/symfony2 with DQL
i know how to do just a select like this :
$qb = $em->createQueryBuilder();
$qb->select('c')
->from('myBundle:Client', 'c')
->where('c.name LIKE :data')
->orderBy('c.name', 'ASC')
->setParameter('data', '%'.$data.'%');
$query = $qb->getQuery();
$entities = $query->getResult();
but now i have to join two tables
some help please
Thank you

you have to use methods like join, innerJoin or leftJoin
see http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/query-builder.html
Best regard

Related

DISTINCT Statement is not working in Doctrine

I try get distinct rows from doctrine, but it not work (uniq hotels)
$qb = $this->createQueryBuilder('self');
$qb
->distinct('hotel')
->join('self.hotel', 'hotel')
->where('self.request = :id')
->setParameter('id',$requestId);
return $qb->getQuery()->getResult();
This DQL returns all hotels.
SQL qwery from Symfony:
I want this qwery:
SELECT count(DISTINCT hotel_id) FROM symfony.search_result where request_id=#requrst_id
Based on your update the DQL you want to build is this
$qb = $this->createQueryBuilder('self');
$qb
->select($qb->expr()->countDistinct('hotel.id'))
->join('self.hotel', 'hotel')
->where('self.request = :id')
->setParameter('id',$requestId);
return $qb->getQuery()->getSingleScalarResult();
note that i changed the
->distinct('hotel') to ->select($qb->expr()->countDistinct('hotel.id'))
also i changed the
getResult() to getSingleScalarResult()
now it will return just one result with the count of the unique hotel_ids

Using left join with query builder doctrine

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();

Doctrine Query Builder Bug?

I have run into an issue in Doctrine. I have built the following query with queryBuilder
$qb = $query = $this->repoLibrary->createQueryBuilder('l');
$query = $qb
->innerJoin('l.productVariant', 'v')
->innerJoin('v.product', 'p')
->innerJoin('p.taxons', 't', 'WITH', 't.id IN (:array)')
->where('l.user = :user')
->groupBy('l.id HAVING count(DISTINCT t.id) >= :count')
->setParameter('user', $user)
->setParameter('array', $s)
->setParameter('count', count($taxons))
->getQuery();
Here is the query that is logged prior to execution:
SELECT s0_.id AS id0, s0_.consumed_at AS consumed_at1, s0_.created_at AS created_at2, s0_.updated_at AS updated_at3, s0_.user_id AS user_id4, s0_.variant_id AS variant_id5
FROM src_library s0_
INNER JOIN src_variant s1_ ON s0_.variant_id = s1_.id
INNER JOIN src_product s2_ ON s1_.product_id = s2_.id
INNER JOIN src_taxon_product s4_
ON s2_.id = s4_.product_id
INNER JOIN src_taxon s3_ ON s3_.id = s4_.taxon_id
AND (s3_.id IN (1,4))
WHERE s0_.user_id = 1
GROUP BY s0_.id HAVING count(DISTINCT s3_.id) = ? ["1,4",1,2]
When I execute this query (after inserting the parameters seen above) directly in MySQL it works perfectly, returning the 2 results that I'm looking for.
However, when it is executed by Doctrine it returns an empty array.
Any Ideas??
After scouring the interwebs I found the following answer. The problem is with the 'IN' clause. As articulated here:
https://groups.google.com/forum/#!topic/doctrine-dev/-_cINyk2dvs
My problem was being caused by the fact that I was building the 'IN' array as a string.
$s = "1,4"
instead of
$s = array(1,4);
This made all the difference in the world, and also made me feel like a n00b.
Your code looks fine and should work. I can't see your whole code but I'm guessing that it is returning an empty array because you have not actually executed the prepared sql statement yet. You should call the "getResult()" to do this.
Try this:
$qb = $query = $this->repoLibrary->createQueryBuilder('l');
$query = $qb
->select('l, v.id, p.id')
->innerJoin('l.productVariant', 'v')
->innerJoin('v.product', 'p')
->innerJoin('p.taxons', 't', 'WITH', 't.id IN (:array)')
->where('l.user = :user')
->groupBy('l.id HAVING count(DISTINCT t.id) >= :count')
->setParameter('user', $user)
->setParameter('array', $s)
->setParameter('count', count($taxons))
->getQuery()
->getResult();

delete several rows where attribute = x

I need to delete all rows where the image_id = x applies
like this
DELETE FROM `ImageVoters` WHERE image_id =1
How do I do this using DQL?
Since im trying to delete several rows at once the remove() function won't work
EntityManager#remove() expects parameter 1 to be an entity object, array given.
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery('SELECT u FROM GabrielUploadBundle:ImageVoters u WHERE u.image_id = 1');
$db_imageactions = $query->getResult();
$em->remove($db_imageactions);
$em->flush();
This is the code that works
$qb = $em->createQueryBuilder();
$qb->delete('GabrielUploadBundle:ImageVoters', 'v')
->where($qb->expr()->eq('v.image_id', ':imageId'))
->setParameter('imageId', $current_imageId);
$qb->getQuery()->execute();
You could create an ORM queryBuilder, to create a clean/safe query object by using internal orm methods
$qb->delete('My\Image\Namespace\ImageVoters', 'ivoter')
->where($qb->expr()->eq('ivoter.image_id', ':imageId')
->setParameter('imageId', 1);
$qb->getQuery()->execute();
You can also execute any raw SQL like this:
$sql = "DELETE FROM ImageVoters WHERE image_id =".$image_id;
$q = $em->getConnection()->exec($sql);

'where not in' query with doctrine query builder

Im trying to reproduce this query:
SELECT * FROM `request_lines`
where request_id not in(
select requestLine_id from `asset_request_lines` where asset_id = 1
)
in doctrine query builder,
I am stuck on the where request_id not in(select
I currently have:
$linked = $em->createQueryBuilder()
->select('rl')
->from('MineMyBundle:MineRequestLine', 'rl')
->where()
->getQuery()
->getResult();
You need to use query builder expressions, and this means you need access to the query builder object. Also, the code is easier to write if you generate the subselect list ahead of time:
$qb = $em->createQueryBuilder();
$nots = $qb->select('arl')
->from('$MineMyBundle:MineAssetRequestLine', 'arl')
->where($qb->expr()->eq('arl.asset_id',1))
->getQuery()
->getResult();
$linked = $qb->select('rl')
->from('MineMyBundle:MineRequestLine', 'rl')
->where($qb->expr()->notIn('rl.request_id', $nots))
->getQuery()
->getResult();
It is possible to do this in one Doctrine query:
$qb = $this->_em->createQueryBuilder();
$sub = $qb;
$sub = $qb->select('arl')
->from('$MineMyBundle:MineAssetRequestLine', 'arl')
->where($qb->expr()->eq('arl.asset_id',1));
$linked = $qb->select('rl')
->from('MineMyBundle:MineRequestLine', 'rl')
->where($qb->expr()->notIn('rl.request_id', $sub->getDQL()))
->getQuery()
->getResult();
Check the reference in this answer here
Using Symfony 5, this solution might help those, who are trying to set parameters on a subquery, the notIn() 2nd argument accepts an array or you could pass a DQL instead and that's what we are doing here and keep in mind that the parameters should be added to the main query as below.
$main = $this->em->createQueryBuilder();
$sub = $main;
$sub = $sub->select('arl')
->from('$MineMyBundle:MineAssetRequestLine', 'arl')
->where($sub->expr()->eq('arl.asset_id',':id'));
$linked = $main->select('rl')
->from('MineMyBundle:MineRequestLine', 'rl')
->where($main->expr()->notIn('rl.request_id', $sub->getDQL()))
->setParameter('id', 1)
->getQuery()
->getResult();

Resources