Symfony Doctrine QueryBuilder - php

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

Related

Order multiple entities by date with query-builder (Symfony)

I was wondering if I could possibly order multiple entities by their date of creation with the query builder in Symfony.
This is my query:
public function findContributions(Association $association)
{
return $this->createQueryBuilder('a')
->select('d, ms, m')
->addSelect('greatest(ms.createdAt, d.createdAt) AS date')
->from(Membership::class, 'ms')
->from(Donation::class, 'd')
->leftJoin('ms.member', 'm')
->Where('m.association = a.id')
->andWhere('d.association = a.id')
->orderBy('date', 'DESC')
->getQuery()
->getResult();
}
My results so far :
I would like to order by the column created_at located in both of my entities (membership and donation).
Is there any way I can achieve this?
Thank you !
I think you can use
public function addOrderBy($sort, $order = null); // Default $order = 'ASC'
Doing something like
return $this->createQueryBuilder('a')
->select('d, ms, m')
->addSelect('greatest(ms.createdAt, d.createdAt) AS date')
->from(Membership::class, 'ms')
->from(Donation::class, 'd')
->leftJoin('ms.member', 'm')
->Where('m.association = a.id')
->andWhere('d.association = a.id')
->orderBy('m.date', 'DESC')
->addOrderBy('d.date', 'DESC')
->getQuery()
->getResult();
(taken from the doctrine documentation : https://www.doctrine-project.org/projects/doctrine-orm/en/2.8/reference/query-builder.html#high-level-api-methods)

How to implement subqueries in Symfony2 with Doctrine?

I need to count the number of items returned in the subquery. If I write the subquery how DQL - all good, but if I try to build a query via QueryBuilder - I get an error.
Subquery DQL:
$qb3 = $this->createQueryBuilder('c')
->select('COUNT(c.id)')
->where('c.id IN (SELECT cl.id FROM Acme\AppBundle\Entity\ClassC cl INNER JOIN Acme\AppBundle\Entity\ClassP p WHERE p.var1 = :var1 AND p.var2 = cl.id GROUP BY cl.id)')
->setParameter('var1', $var);
Subquery via QueryBuilder:
$qb = $this->createQueryBuilder('c');
$qb->select('COUNT(c.id)')
->where(
$qb->expr()->in(
'c.id',
$this->createQueryBuilder('cl')
->select('cl.id')
->innerJoin('Acme\AppBundle\Entity\ClassP', 'p')
->where('p.var1 = :var1')
->setParameter('var1', $var)
->andWhere('p.var2 = cl.id')
->groupBy('cl.id')
->getDQL()
)
);
Both versions return the same DQL.
Error:
screen
Try to move setParameter() to main level of query.
$qb = $this->createQueryBuilder('c');
$qb->select('COUNT(c.id)')
->where(
$qb->expr()->in(
'c.id',
$this->createQueryBuilder('cl')
->select('cl.id')
->innerJoin('Acme\AppBundle\Entity\ClassP', 'p')
->where('p.var1 = :var1')
->andWhere('p.var2 = cl.id')
->groupBy('cl.id')
->getDQL()
)
)
->setParameter('var1', $var);

Codeigniter and doctrine join three tables

Hello there I am trying to join 3 tables in doctrine codeigniter i have successfully join two tables but have problem in joining the third one.
Here is my codeigniter active record Query. I want to handle this query in doctrine
$query = $this->db->select('table1.name, table3.date')
->from('table1')
->join('table2', 'table2.id = table1.Uid')
->join('table3', 'table3.id = table1.Rid')
->where('table1.Uid', $Uid)
->get();
return $query->result();
This is my Doctrine query builder query where i join two tables successfully.
$qb = $this->em->createQueryBuilder();
$query = $qb->select('t1, t2')
->from('table1', 't1')
->join('table2', 't2')
// ->join('table3', 't3')
->where('t1.Uid = :Uid')
->andwhere('t2.Yid = :Yid')
->setParameters(array('Uid'=> $Uid, 'Yid' => $user_id))
->getQuery()
->getResult();
Try using the following code, seems like your syntax is not correct.
$qb = $this->em->createQueryBuilder();
$query = $qb->select('t1.name', 't2.name')
->from('table1', 't1')
->leftJoin('t1', 'table2', 't2', 't2.id = t1.Uid')
->leftJoin('t1', 'table3', 't3', 't3.id = t1.Rid')
->where('t1.Uid = :Uid')
->andwhere('t2.Yid = :Yid')
->setParameters(array('Uid'=> $Uid, 'Yid' => $user_id))
->getQuery()
->getResult()
More information about Constructing a new QueryBuilder object

Doctrine2 DQL issue with COUNT = 0

I have the following query so far:
$shopQuery = $qb->select('DISTINCT u')
->from("BlahUserBundle:User", 'u')
->innerJoin('u.followers', 'followers')
->andWhere('followers.id != :userId')
->setParameter('userId', $user->getId())
->orWhere('') //or where those user who doesn't have a follower yet
//->setMaxResults(5)
;
I am trying to find a way to query all users who doesn't have a follower and whose follower is not my self (in this case my self is $user->getId()). How do I do so?
Try this
$shopQuery = $qb->from("BlahUserBundle:User", 'u')
->leftJoin(
'u.followers',
'followers',
'on',
'followers.id != :userId'
)
->where('followers.id IS NULL')
->setParameter('userId', $user->getId());
$shopQuery->getQuery()->getResults();

Subquery in doctrine2 notIN Function

I'd like to select members who are not in specific service. I have 3 tables :
membre
service
membre_service (relation between membre and service)
I'm using doctrine 2 and in SQL my query is :
SELECT m.* FROM membre m WHERE m.`id` NOT IN (
SELECT ms.membre_id FROM membre_service ms WHERE ms.service_id != 29
)
In Doctrine, I do :
$qb = $this->_em->createQueryBuilder();
$qb2 = $qb;
$qb2->select('m.id')
->from('Custom\Entity\MembreService', 'ms')
->leftJoin('ms.membre', 'm')
->where('ms.id != ?1')
->setParameter(1, $service);
$qb = $this->_em->createQueryBuilder();
$qb->select('m')
->from('Custom\Entity\Membre', 'm')
->where($qb->expr()->notIn('m.id', $qb2->getDQL())
);
$query = $qb->getQuery();
//$query->useResultCache(true, 1200, __FUNCTION__);
return $query->getResult();
I got the following error :
Semantical Error] line 0, col 123 near 'm WHERE ms.id': Error: 'm' is already defined.
The same alias cannot be defined 2 times in the same query
$qb = $this->_em->createQueryBuilder();
$qb2 = $qb;
$qb2->select('m.id')
->from('Custom\Entity\MembreService', 'ms')
->leftJoin('ms.membre', 'm')
->where('ms.id != ?1');
$qb = $this->_em->createQueryBuilder();
$qb->select('mm')
->from('Custom\Entity\Membre', 'mm')
->where($qb->expr()->notIn('mm.id', $qb2->getDQL())
);
$qb->setParameter(1, $service);
$query = $qb->getQuery();
return $query->getResult();
Ideally you should use many-to-many relation for your entity, in this case your query is going to be much simpler.
Actually if you are using the Symfony2 repository class you could also do the following:
$in = $this->getEntityManager()->getRepository('Custom:MembreService')
->createQueryBuilder('ms')
->select('identity(ms.m)')
->where(ms.id != ?1);
$q = $this->createQueryBuilder('m')
->where($q->expr()->notIn('m.id', $in->getDQL()))
->setParameter(1, $service);
return $q->getQuery()->execute();
You can use (NOT) MEMBER OF:
<?php
$query = $em->createQuery('SELECT m.id FROM Custom\Entity\Membre WHERE :service NOT MEMBER OF m.services');
$query->setParameter('service', $service);
$ids = $query->getResult();
See the documentation for more examples.

Categories