Symfony2 - Query Builder multiple orderBy - symfony

I have the following code
public function queryAssociationsSorted($id){
$qb = $this->createQueryBuilder('a')
->leftJoin('a.category', 'c')
->where('a.job = :id')
->setParameter('id', $id)
//->addOrderBy('c.rank', 'DESC')
//->addOrderBy('a.updated', 'DESC')
->add('orderBy','c.rank DESC THEN a.updated DESC')
;
return $query = $qb->getQuery();
}
and this or the commented out options both only sort by a.updated. I have looked at other posts on this subject and can't find a solution. Can anyone suggest where I am going wrong?

If I understand correctly, and this is the result yu want:
id - rank - updated
xx - 4 - 2014-01-01
xx - 3 - 2014-01-02
xx - 3 - 2014-01-01
This actually works:
$qb = $this->createQueryBuilder('a')
->leftJoin('a.category', 'c')
->where('a.job = :id')
->setParameter('id', $id)
->addOrderBy('c.rank', 'DESC')
->addOrderBy('a.updated', 'DESC')
;
return $query = $qb->getQuery();

I'm not sure how doctrine works with multiple sort by..
why dont you try to use dql or even raw sql query?
http://docs.doctrine-project.org/en/2.1/reference/dql-doctrine-query-language.html
http://doctrine-orm.readthedocs.org/en/latest/reference/native-sql.html
Edit:
something like that will work I think:
->add('orderBy','c.rank DESC, a.updated DESC')
If you still have no success the raw sql query will save you ;)

Over a year later and I've stumbled across the correct answer in this answer by #red777
This will help you:
$adapter = new DoctrineORMAdapter($queryBuilder, false);
Had the same problem this morning. By default Pagerfanta is treating your query as
one with joins. Setting second argument to false makes it use simple
query handling.
In Kunstmaan Bundle, in AdminListConfiguration class, you have to
overide function that is creating Pagerfanta, if you want to sort
simple entity.
The syntax in the query had always been correct, but the error crept in in the DoctrineOrmAdapter. Adding false as the second parameter sorted the problem

Related

Doctrine sort by 2 different entities

I'm building a forum with Symfony and I'm currently wondering on how I'm about to list the latest activity using Doctrine.
I have a OneToMany relationship between Topic & Post.
I'd have to check for the latest things happened, which might be a post on a topic, or a topic itself.
How would you guys do this as clean as possible?
I would probably UNION both somehow sort by date and select last 5.
Thank you in advance!
You can try something like this (It's a function of repository post) :
public function lastTopics(){
// Build query
$qb = $this
->createQueryBuilder('p')
->select('p')
->leftJoin('p.topic', 't')
->addSelect('t')
->addGroupBy('p.id')
->addGroupBy('t.id')
->orderBy('p.dateCreation', 'DESC')
->setMaxResults(5)
;
// Get query
$query = $qb->getQuery();
// Return results
return $query->getResult();
}
Then just list the last 5 posts by viewing topics. Enjoy ;)

doctrine query onetonmany / notIn with objects / symfony forms querybuilder

I am using symfony 2 and doctrine to prefilter a form field type 'entity' with the help of a querybuilder.
My querybuilder should return all products which the user has not already added to a list.
All relations are bidirectionnal.
I have products linked to userIngredients (oneToMany) each linked to one user (manyToOne)
I have come with this so far but it's not working, I get products not added by other users.
return $this
->createQueryBuilder('p')
->leftJoin('p.userIngredients', 'i')
->where('i.user <> ?1')
->setParameter(1,$user);
1; Any clue on how to correct this ?
Alternatively, I could select the products I don't want and then reselect those who don't match but using an expression and NotIn seems to only work for strings
$products = $this
->createQueryBuilder('p')
->leftJoin('p.userIngredients', 'i')
->where('i.user = ?1')
->setParameter(1,$user)
->getQuery()
->getResult();
return $this
->createQueryBuilder('p')
->where($this->createQueryBuilder('p')->expr()->notIn('p', $products));
2; how could we correct this to make it work with objects ?
3; alternatively : is there a way to pass not a querybuilder but an array of results to symfony form builders ?
I got thinks thanks to Javad:
(slight modification, I'm using an array result, not dql):
$qb = $this->_em->createQueryBuilder();
$ids = $qb
->select('p.id')
->from('AppBundle:MarketPlace\Product','p','p.id')
->leftJoin('p.userIngredients', 'i')
->where('i.user = ?1')
->setParameter(1,$user)
->getQuery()
->getResult();
//I don't know why I couldn't directly get an array of ids otherwise... if you know how to do better directly from the query, I'm interested (getScalarResult does not make it)
$ids=array_keys($ids);
$result = $this
->createQueryBuilder('p')
->where($this->createQueryBuilder('p')->expr()->notIn('p.id', $ids));
return $result;

doctrine2 order by array with ids

How I can with doctrine2 order by array with ids ?
I have this query:
$qb = $this->createQueryBuilder('u')
->select('u', 'n', 'c')
->leftJoin('u.notifications', 'n')
->leftJoin('u.channel', 'c')
->andWhere('u.id IN (:ids)')
->setParameter('ids', $ids);
I want that the result has the same order that array with ids, is possible do it ?
Thanks
SOLUTION:
Use FIELD mysql extension with https://github.com/beberlei/DoctrineExtensions
:)
Thanks
Simple solution that doesn't require presorting query result:
$idPositions = array_flip($userIds); // Mapping of id to position
usort($users, function($userA, $userB) use ($idPositions) {
return $idPositions[$userA->id] - $idPositions[$userB->id];
});
If you are using MySQL, sorting with the FIELD() function could accomplish this. DQL doesn't have built-in support for this function, so you'll either have to create a user defined function (see this answer), or create a native query.
I have a solution that is probably very inefficient, but it works. Make sure your results are ordered by id.
$users = $entityManager
->getRepository('User')
->findById($userIds, ['id' => 'ASC']);
$userIdsCopy = $userIds;
sort($userIdsCopy);
array_multisort($userIds, $userIdsCopy);
array_multisort($userIdsCopy, $users);
It's hard to explain how this works, but basically you "remember" the operations that happen when you sort the userId-array, then you apply the opposite of that to the query result.
I found the solution, it is possible using FIELD mysql extension with https://github.com/beberlei/DoctrineExtensions
Thanks
Marc
Which id do you want to order by?
You can use...
->orderBy('u.id', 'asc')
// or n.id
// or c.id
Or you can use multiple order by's ("by's" doesn't seem right to me)...
->addOrderBy('u.id', 'asc')
->addOrderBy('u.name', 'desc') // if it exists

Doctrine2 findOneBy argument equivalent to BETWEEN in SQL

I need to be able to use the following WHERE clause in Doctrine:
WHERE AL.UserID = 41 AND (TheDate BETWEEN DATE_SUB(CURDATE(), INTERVAL 2 WEEK) AND CURDATE())
and what I'm currently doing is:
$results = $em->getRepository('MyBundle:MyTable')->findOneBy(array('userId' => $userId));
However, I haven't been able to filter results by the last 2 weeks without writing SQL or DQL.
Is there any way I can achieve this through Doctrine's methods?
You can use QueryBuilder. Assuming you are using MySQL BETWEEN, something like:
<?php
// $em instanceof EntityManager
$qb = $em->createQueryBuilder();
$now = new DateTime();
$qb->select('c')
->from('MyClass', 'c')
->where($qb->expr()->andX(
$qb->expr()->eq('c.UserID', '?1'),
$qb->expr()->gte('c.TheDate', '?2'),
$qb->expr()->lte('c.TheDate', '?3')
))
->setParameter(1, 41)
->setParameter(2, $now.sub(new DateInterval('P2W')))
->setParameter(3, $now);
$query = $qb->getQuery();
$c = $query->getSingleResult();
This is cleaner code and should work across the different database platforms supported by Doctrine2 ORM.

How to use wildcards in createQueryBuilder?

In my repository class i use:
public function getItemsByTag($tag)
{
$qb = $this->createQueryBuilder('c')
->select('c')
->where('c.tags LIKE %bipolar%')
->addOrderBy('c.id');
return $qb->getQuery()
->getResult();
}
But unfortunately this doesn't work.. Anybody knows how this can work? Or do I have to build a custom query without the QueryBuilder?
Thanks!
Searching based on a single parameter:
I think it should go:
public function getItemsByTag($tag)
{
$qb = $this->createQueryBuilder('c')
->select('c')
->where('c.tags LIKE :tag')
->addOrderBy('c.id')
->setParameter('tag', $tag);
return $qb->getQuery()->getResult();
}
But I think that it is discouraged to do a LIKE as part of a where using the query builder so you should do:
$qb = $this->createQueryBuilder('c');
$qb->select('c')
->where($qb->expr()->like('c.tags', '?1'))
->addOrderBy('c.id')
->setParameter(1, $tag);
return $qb->getQuery()->getResult();
Check out the docs for more information, there is an example of a like expression in the section entitled Helper Methods
I should also point out that I used a different convention in each example for passing a parameter into a query, the first used a named parameter :tag which is set by setParameter('tag', $value) the second is just a numbered parameter ?1, you could have just as easily have used a named parameter in the second example if you wished to as well.
Searching with an array of parameters:
You also asked about doing an array of likes. Here it is with an OR expression but if you wanted to search for all tags you could change it to an AND.
In order to make a "LIKE array" you just have to build up the expression on its own.
$qb = $this->createQueryBuilder('c');
$orExpr = $qb->expr()->orX();
for ($i = 0; $i < count($tags); $i++) {
$orExpr->add($qb->expr->like('c.tags', "?$i"));
// You may have to set params later in a loop after $orExpr has been
// added to the queryBuilder.
$qb->setParameter($i, $tags[$i]);
}
$qb->select('c')->where($orExpr)->addOrderBy('c.id');
return $qb->getQuery()->getResult();
If you don't want to substitute your query with variables but use a static string you have to put the string in apostrophes.
You have to use apostrophes instead of quotes! Otherwise the Doctrine2 Lexer will throw an Exception.
So in your case Mike you can use:
'c.tags LIKE \'%bipolar%\''
or
"c.tags like '%bipolar%'"
I don't know much about Symfony, but based on what I know about PHP and MySQL, I imagine you mean 'c.tags LIKE "%bipolar%"'. You likely need quotation marks around %bipolar%.
simply:
public function getItemsByTag($tag)
{
$qb = $this->createQueryBuilder('c')
->select('c')
->where( $qb->expr()->like('c.tags', ':tags') )
->addOrderBy('c.id');
$qb->setParameter('tags', '%' . $tag . '%' );
return $qb->getQuery()->getResult();
}

Resources