Convert SQL inner join query to Doctrine - symfony

Hey I have problem with Convert this SQL
SELECT DISTINCT izo_client.name
FROM izo_client
INNER JOIN izo_measurement
ON izo_client.id=izo_measurement.client_id;
into Doctrine query builder
$qb = $this->_em->createQueryBuilder();
$qb->select('u')
->from('AppBundle:Client', 'u')
->innerJoin('AppBundle:Measurement', 'm', 'WITH', 'u.id = m.client_id')
->distinct();
return $qb->getQuery()->getResult();
The error is:
[Semantical Error] line 0, col 91 near 'client_id': Error: Class AppBundle\Entity\Measurement has no field or association named client_id

DQL is not an SQL. In DQL you operate on objects, not database relations. Properties are used as opposed to database columns.
Since your field is called $clientsBelongsTo, your query should look more like:
$qb = $this->_em->createQueryBuilder();
$qb->select('u')
->from('AppBundle:Client', 'u')
->innerJoin('AppBundle:Measurement', 'm', 'WITH', 'u.id = m.clientsBelongsTo')
->distinct();

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

Symfony 2 query builder join without relation (cross join)

I am trying to use the query builder to join 2 tables which have no relation.
Desired end result:
SELECT x, y
FROM x
JOIN y
Query builder code:
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select('x');
$qb->from('Test1', 'x');
$qb->join('Test2', 'y');
$qb->orderBy('x.name', 'ASC');
Produces the following DQL:
SELECT x FROM Test1 x INNER JOIN Test2 y ORDER BY x.name ASC
Which results in a syntax error:
[Syntax Error] line 0, col 137: Error: Expected Literal, got 'BY'
The entities Test1 and Test2 don't have a relation (not in the code, nor in the database).
Is there any way to accomplish this?
I would like to use the query builder, because I have a lot of other functionality for the query that depends on the query builder (for filtering and sorting etc.).
I know this is possible with plain SQL, or DQL queries (not produced by the query builder).
You can try the following possibility:
public function getYourData($users) {
$qb = $this->entityManager->createQueryBuilder();
$qb
->select('x', 'y')
->from('Test1', 'x')
->leftJoin(
'Test2',
'y',
\Doctrine\ORM\Query\Expr\Join::WITH,
'x.id = y.reference_id'
)
->orderBy('x.name', 'ASC');
return $qb->getQuery()->getResult();
}

how to do a join in doctrine with createQuiry or DQL

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

query with dateformat in where clause in symfony2

when I run a query with date in where clause, following error is showed...
[Syntax Error] line 0, col 129: Error: Expected known function, got 'DATE_FORMAT'
the query is given below
$query = $this->getEntityManager()->createQuery(
"SELECT a.id, a.amont, a.paymentDescrip, a.paymentType, a.paymentDate
FROM RegalSmsBundle:DailyTransaction a
WHERE DATE_FORMAT(a.paymentDate,'%Y-%m-%d') = :paymentDate
and a.students = :studentId"
)->setParameter('studentId', $studentId)
->setParameter('paymentDate','2013-03-11');
return $query->getResult();
Doctrine doesn't have DATE_FORMAT function defined by default. It's possible to Register Custom DQL Function.
But you can compare date easily (assuming a.paymentDate is of type date):
$query = $this->getEntityManager()->createQuery("
SELECT a.id, a.amont, a.paymentDescrip, a.paymentType, a.paymentDate
FROM RegalSmsBundle:DailyTransaction a
WHERE a.paymentDate = :paymentDate AND a.students = :studentId
")
->setParameter('studentId', $studentId)
->setParameter('paymentDate', new \DateTime('2013-03-11'))
;
return $query->getResult();
Edit: I prefer using querybuider to writing DQL. It would look like this:
$qb = $this->getEntityManager()->getRepository('RegalSmsBundle:DailyTransaction')->createQueryBuilder('a');
$qb
->select('a') // select whole entity
->where($qb->expr()->andX(
$qb->expr()->eq('a.paymentDate', ':paymentDate')
$qb->expr()->eq('a.students', ':studentId')
))
->setParameter('studentId', $studentId)
->setParameter('paymentDate', new \DateTime('2013-03-11'))
;
return $qb->getQuery()->getResult();

Left join with Pagerfanta Doctrine

I'd like show results of two tables but I have this Exception: "Cannot count query which selects two FROM components, cannot make distinction".
My query is:
$queryBuilder = $em->createQueryBuilder()
->select('e, e.id AS id, e.f14010101 AS f14010101, e.f14010105 AS f14010105')
->from('T140101Bundle\Entity\T140101', 'e')
->leftJoin('T140102Bundle\Entity\T140102', 'c', 'WITH', 'c.f14010201= e.id')
->groupBy('e.id');
Can you help me?

Resources