Doctrine query SQL to DQL - php

I have a select with SQL which is working perfectly, but I cannot translate this query to DQL with createQueryBuilder().
Here's my query:
SELECT distinct c.name_id
FROM cultures AS c
LEFT JOIN ilots AS i ON i.exploitation_id = 1
My current code:
return $this->createQueryBuilder('c')
->leftJoin(Ilots::class, 'i', 'ON', 'i.exploitation = 1')
->distinct()
->getQuery()
->getResult();
And the error:
[Syntax Error] line 0, col 74: Error: Expected end of string, got 'ON'

In DQL ON doesn't exist, you have to use WITH instead.
return $this->createQueryBuilder('c')
->leftJoin(Ilots::class, 'i', 'WITH', 'i.exploitation = 1')
->distinct()
->getQuery()
->getResult();
Documentation

If there is relationship between the Entity:
$qb = $this->entityManager->createQueryBuilder('c')
->select('c')
->distinct()
->from('cultures', 'c')
->leftJoin('c.ilots', 'i')
->where('i.exploitation = 1')
;
return $qb->getQuery()->getResult();

Related

Transforming SQL into Doctrine Query [duplicate]

This is my function where I'm trying to show the User history. For this I need to display the user's current credits along with his credit history.
This is what I am trying to do:
public function getHistory($users) {
$qb = $this->entityManager->createQueryBuilder();
$qb->select(array('a','u'))
->from('Credit\Entity\UserCreditHistory', 'a')
->leftJoin('User\Entity\User', 'u', \Doctrine\ORM\Query\Expr\Join::WITH, 'a.user = u.id')
->where("a.user = $users ")
->orderBy('a.created_at', 'DESC');
$query = $qb->getQuery();
$results = $query->getResult();
return $results;
}
However, I get this error :
[Syntax Error] line 0, col 98: Error: Expected Doctrine\ORM\Query\Lexer::T_WITH, got 'ON'
Edit: I replaced 'ON' with 'WITH' in the join clause and now what I see is only 1 value from the joined column.
If you have an association on a property pointing to the user (let's say Credit\Entity\UserCreditHistory#user, picked from your example), then the syntax is quite simple:
public function getHistory($users) {
$qb = $this->entityManager->createQueryBuilder();
$qb
->select('a', 'u')
->from('Credit\Entity\UserCreditHistory', 'a')
->leftJoin('a.user', 'u')
->where('u = :user')
->setParameter('user', $users)
->orderBy('a.created_at', 'DESC');
return $qb->getQuery()->getResult();
}
Since you are applying a condition on the joined result here, using a LEFT JOIN or simply JOIN is the same.
If no association is available, then the query looks like following
public function getHistory($users) {
$qb = $this->entityManager->createQueryBuilder();
$qb
->select('a', 'u')
->from('Credit\Entity\UserCreditHistory', 'a')
->leftJoin(
'User\Entity\User',
'u',
\Doctrine\ORM\Query\Expr\Join::WITH,
'a.user = u.id'
)
->where('u = :user')
->setParameter('user', $users)
->orderBy('a.created_at', 'DESC');
return $qb->getQuery()->getResult();
}
This will produce a resultset that looks like following:
array(
array(
0 => UserCreditHistory instance,
1 => Userinstance,
),
array(
0 => UserCreditHistory instance,
1 => Userinstance,
),
// ...
)

DOCTRINE - Number of query result change for no apparent reason

i'm facing a strange problem: here is the deal.
When i am doing this:
$query = $this->createQueryBuilder('s')
->select('s')
->leftJoin('s.Cluster', 'u')
->where('u.id = 15')
->orWhere('u.id=37')
->getQuery()
->getResult();
var_dump(count($query));
the output is 2
but if i do :
$query = $this->createQueryBuilder('s')
->select('s')
->leftJoin('s.Cluster', 'u')
->where('u.id = 15')
->orWhere('u.id=37');
$query
->getQuery()
->getResult();
var_dump(count($query));
the output is 1.. can someone explain? i don't understand where it could come from

Query and subquery in symfony

I use query and subquery in symfony query builder, but when execute it, it returns an error.
My code is :
$subQb = $em->createQueryBuilder();
$subquery = $subQb->select('COUNT(v.id)')
->from('AdminBundle:Visitsite', 'v')
->where('v.site = s.id')
->Andwhere('v.createdate > :date')
->setParameter('date', $date->format('Y-m-d'))
->getDQL();
$subQb2 = $em->createQueryBuilder();
$subquery2 = $subQb2->select('quantity')
->from('AdminBundle:Limitviewday', 'l')
->where($subQb2->expr()->eq('s.limitviewday', 'l.id'))
->getDQL();
$qb = $em->createQueryBuilder();
$query = $qb->select('s')
->from('AdminBundle:Sites', 's')
->where('s.quantity > 1')
->Andwhere('s.status = 1')
->Andwhere($qb->expr()->lte("($subquery)", "($subquery2)"));
$settlements = $query->getQuery()->getResult();
And my result is
[Semantical Error] line 0, col 183 near 'quantity FROM': Error: 'quantity' is not defined.
Please help me.
I think that error come from:
$subquery2 = $subQb2->select('quantity') // Expected '<alias>' or '<alias>.<property>'
instead:
$subquery2 = $subQb2->select('l.quantity')

Symfony Doctrine QueryBuilder

I try to convert this query to the querybuilder:
SELECT *, landingpages_content.id as cid FROM `landingpages_content` LEFT JOIN content ON landingpages_content.content_id = content.id WHERE content.enabled = 1 AND landingpages_content.landingpage_id = ? ORDER BY landingpages_content.`order`
This is my QueryBuilder code:
$queryBuilder
->select('*, landingpages_content.id as cid')
->from('landingpages_content', 'landingpages_content')
->leftJoin('landingpages_content.content_id', 'content.id', null)
->where('content.enabled = 1')
->andWhere('landingpages_content.landingpage_id = :id')
->setParameter('id', $id)
->orderBy('landingpages_content.`order`', 'ASC');
It returns
InvalidArgumentException: The query builder cannot have joins.
You are using leftJoin method wrong. Try something like this:
$queryBuilder
->select('*, landingpages_content.id as cid')
->from('landingpages_content', 'landingpages_content')
->leftJoin('landingpages_content.content', 'content', 'ON', 'landingpages_content.content_id = content.id')
->where('content.enabled = 1')
->andWhere('landingpages_content.landingpage_id = :id')
->setParameter('id', $id)
->orderBy('landingpages_content.order', 'ASC');

How to do left join in Doctrine?

This is my function where I'm trying to show the User history. For this I need to display the user's current credits along with his credit history.
This is what I am trying to do:
public function getHistory($users) {
$qb = $this->entityManager->createQueryBuilder();
$qb->select(array('a','u'))
->from('Credit\Entity\UserCreditHistory', 'a')
->leftJoin('User\Entity\User', 'u', \Doctrine\ORM\Query\Expr\Join::WITH, 'a.user = u.id')
->where("a.user = $users ")
->orderBy('a.created_at', 'DESC');
$query = $qb->getQuery();
$results = $query->getResult();
return $results;
}
However, I get this error :
[Syntax Error] line 0, col 98: Error: Expected Doctrine\ORM\Query\Lexer::T_WITH, got 'ON'
Edit: I replaced 'ON' with 'WITH' in the join clause and now what I see is only 1 value from the joined column.
If you have an association on a property pointing to the user (let's say Credit\Entity\UserCreditHistory#user, picked from your example), then the syntax is quite simple:
public function getHistory($users) {
$qb = $this->entityManager->createQueryBuilder();
$qb
->select('a', 'u')
->from('Credit\Entity\UserCreditHistory', 'a')
->leftJoin('a.user', 'u')
->where('u = :user')
->setParameter('user', $users)
->orderBy('a.created_at', 'DESC');
return $qb->getQuery()->getResult();
}
Since you are applying a condition on the joined result here, using a LEFT JOIN or simply JOIN is the same.
If no association is available, then the query looks like following
public function getHistory($users) {
$qb = $this->entityManager->createQueryBuilder();
$qb
->select('a', 'u')
->from('Credit\Entity\UserCreditHistory', 'a')
->leftJoin(
'User\Entity\User',
'u',
\Doctrine\ORM\Query\Expr\Join::WITH,
'a.user = u.id'
)
->where('u = :user')
->setParameter('user', $users)
->orderBy('a.created_at', 'DESC');
return $qb->getQuery()->getResult();
}
This will produce a resultset that looks like following:
array(
array(
0 => UserCreditHistory instance,
1 => Userinstance,
),
array(
0 => UserCreditHistory instance,
1 => Userinstance,
),
// ...
)

Categories