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

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)

Related

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

Symfony doctrine repository order by count

I have few tables, and get resuts for it.
$qb = $this->createQueryBuilder('a');
$qb->select("a, c, ac")
->leftJoin('a.categories', 'c')
->leftJoin('a.comments', 'ac')
->where('c.slug = :category')
->setParameter('category', $category)
->orderBy('a.name', 'ASC');
return $qb->getQuery();
I want this result, order by comments (ac) count. How can I solve this?
$qb = $this->createQueryBuilder('a');
$qb->select("a, c, ac, count(a.comments) AS HIDDEN nbcomments")
->leftJoin('a.categories', 'c')
->leftJoin('a.comments', 'ac')
->where('c.slug = :category')
->setParameter('category', $category)
->orderBy('a.name', 'ASC')
->addOrderBy('nbcomments', 'ASC');
return $qb->getQuery();

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

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