Order by innerJoin table with doctrine - symfony

I have a OneToMany relationship between two entities: Shop and Comment. I would like to display a list of Shops ordered by the date of the last review they received.
This is what I tried so far:
return $this->createQueryBuilder('s')
->innerJoin('s.reviews', 'r', 'WITH', 'r.shop = s.id')
->orderBy('r.createdAt') // <- that does nothing on the order of the shops
->getQuery()
->getResult();
This returns all the shops, but they aren't ordered at all...

Try to set OrderBy directly on your #ORM\OneToMany mappedBy relation like this:
/**
* #ORM\OneToMany...
* #ORM\OrderBy({"createdAt" = "DESC"})
*/
private $reviews;

If you want to use it inside your ShopRepository you should try it this way
return $this->getEntityManager()
->createQueryBuilder()
->select('s, MAX(r.createdAt) AS maxCreatedAt')
->from('AppBundle:Comment', 'r')
->join('AppBundle:Shop', 's', 'WITH', 'r.shop = s.id')
->groupBy('s')
->orderBy('maxCreatedAt', 'DESC')
->getQuery()
->getResult();
Don't forget to modify the bundle namespaces (AppBundle) accordingly.

Related

Symfony QueryBuilder andWhere for User-Relation not Working

I want to load items for a given User. Between Item and User lives a One-To-Many relationship.
If I use the following, everything is working well:
$result = $em→getRepository(Item::class)→findBy(['user' => $user]);
The $result has 3 entries.
Now I have to give additional conditions to the query. So I want to use the queryBuilder insight the repository:
$result = $this->createQueryBuilder('i')
->andWhere('i.user = :user')
->setParameter('user', $user)
->getQuery()
->getResult();
The result is empty but the query runs without any errors.
Can someone tell me what I'm doing wrong?
In both cases the $user is the related App\Entity\User entity.
I found the solution. It didn't work because of the Uuid. This works:
$result = $this->createQueryBuilder('i')
->andWhere('i.user = :user')
->setParameter('user', $user->getId()->toBinary())
->getQuery()
->getResult();

symfony querybuilder for search by relation in collection

I have Entity Application with relation to Applicant
/**
* #ORM\ManyToOne(targetEntity=Applicant::class, inversedBy="applications")
* #ORM\JoinColumn(nullable=false)
*/
private $applicant;
now I try create QueryBuilder for search application by Applicant name in ApplicantRepository i have
public function searchByName($searchString)
{
return $this->createQueryBuilder('a')
->andWhere('a.name LIKE :phrase')->setParameter('phrase', '%'.$searchString.'%')
->getQuery()
->getResult();
}
in controller I have
$applicants = $applicantRepository->searchByName($searchString);
Now I want search Application with applicant name in this applicants collection. May I use QueryBuilder fot that?
I am trying something like this
public function getApprovedSearchByApplicants($applicants)
{
return $this->createQueryBuilder('a')
->andWhere('a.applicant IN (:applicants)')
->setParameter('applicants', $applicants)
->getQuery()
->getResult();
}
so, looking to your configuration, your Application::$applicant === Applicant::$name, just because Application::$applicant property has Applicant::$id value, by default. You can check the documentation.
So, this way, you need to make smth like this:
/**
* #ORM\ManyToOne(targetEntity=Applicant::class, inversedBy="applications")
* #ORM\JoinColumn(name="applicant_name", referencedColumnName="name", nullable=false)
*/
private $applicant;
It should work.
UPDATE after question update and discussions:
So, the problem was in the testing data in the database. Bad question.
I did not test it, but something like the following code should do the trick. It is almost the same solution as goulashsoup proposed, but without typing raw DQL.
/**
* #param array|Applicant[] $applicants
*
* #return array|Application[]
*/
public function findByApplicants(array $applicants): array
{
$qb = $this->createQueryBuilder('a')
return $qb->innerJoin('a.applicant', 'at')
->where(
$qb->expr()->in('at.id', ':applicants')
)
->setParameter('applicants', $applicants)
->getQuery()
->getResult();
}
I don't think you need to name the function wtih "ApprovedSearch" since the method is only aware of a list of Applicant for whom you want the list of Application.
Search by search string:
$entityManager
->createQuery('
SELECT ct
FROM App\Entity\Application ct
JOIN ct.applicant nt
WHERE nt.name LIKE :phrase
')
->setParameters(['phrase' => "%$searchString%"])
->getResult();
Search by applicants:
$entityManager
->createQuery('
SELECT ct
FROM App\Entity\Application ct
JOIN ct.applicant nt
WHERE nt IN (:nts)
')
->setParameters(['nts' => $applicants])
->getResult();

Doctrine query to search entities based on related entity field

I've ManyToOne relationship in doctrine (Many results to One PollingStation):
/**
* #ORM\ManyToOne(targetEntity="Iballot\CmsBundle\Entity\PollingStation2", inversedBy="results", cascade={"persist"})
* #ORM\JoinColumn(nullable=false)
* #Expose
*/
private $pollingStation2;
I'd like search for all the result that belong to the polling Station that have a name similar to a key word. I try the following method but it does not work:
public function getForSearch($keyWord)
{
$query = $this->_em->createQueryBuilder();
$query
->select('r')
->from('IballotCmsBundle:Result', 'r')
->where($query->expr()->like('p.pollingStation2', $query->expr()->literal('%' . $keyWord . '%')))
//->orderBy('p.', 'ASC')
->getQuery()
->setParameter('keyWord', '%'.$keyWord.'%');
return $query->getQuery()->getResult();
}
I get the following error
[Semantical Error] line 0, col 48 near 'pollingStation2': Error:
Invalid PathExpression. Must be a StateFieldPathExpression.
As said by #Cerad (one more time), you need a JOIN to make the associated entity available while building your query.
Try this :
$query = $this->_em->createQueryBuilder();
$query
->select('r')
->from('IballotCmsBundle:Result', 'r')
->leftJoin('r.pollingStation2', 'p') // The missing join
->where('p.name LIKE :keyword') // where p.name like %keyword%
->setParameter('keyword', '%'.$keyword.'%')
->orderBy('p.name', 'ASC') // order by p.name ASC
->getQuery()
return $query->getResult();
BTW I fixed your orderBy, simplified your where and fixed the keyword parameter that is wrongly defined.
Try putting this method in your Result EntityRepository class:
public function getForSearch($keyWord)
{
$query = $this->createQueryBuilder('r');
$query
->join('r.pollingStation2', 'p')
->where('p.name LIKE :keyword')
->setParameter('keyword', '%' . $keyWord . '%')
//->orderBy('p.', 'ASC')
;
return $query->getQuery()->getResult();
}

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;

Count Rows in Doctrine QueryBuilder

I'm using Doctrine's QueryBuilder to build a query, and I want to get the total count of results from the query.
$repository = $em->getRepository('FooBundle:Foo');
$qb = $repository->createQueryBuilder('n')
->where('n.bar = :bar')
->setParameter('bar', $bar);
$query = $qb->getQuery();
//this doesn't work
$totalrows = $query->getResult()->count();
I just want to run a count on this query to get the total rows, but not return the actual results. (After this count query, I'm going to further modify the query with maxResults for pagination.)
Something like:
$qb = $entityManager->createQueryBuilder();
$qb->select('count(account.id)');
$qb->from('ZaysoCoreBundle:Account','account');
$count = $qb->getQuery()->getSingleScalarResult();
Some folks feel that expressions are somehow better than just using straight DQL. One even went so far as to edit a four year old answer. I rolled his edit back. Go figure.
Here is another way to format the query:
return $repository->createQueryBuilder('u')
->select('count(u.id)')
->getQuery()
->getSingleScalarResult();
It's better to move all logic of working with database to repositores.
So in controller you write
/* you can also inject "FooRepository $repository" using autowire */
$repository = $this->getDoctrine()->getRepository(Foo::class);
$count = $repository->count();
And in Repository/FooRepository.php
public function count()
{
$qb = $repository->createQueryBuilder('t');
return $qb
->select('count(t.id)')
->getQuery()
->getSingleScalarResult();
}
It's better to move $qb = ... to separate row in case you want to make complex expressions like
public function count()
{
$qb = $repository->createQueryBuilder('t');
return $qb
->select('count(t.id)')
->where($qb->expr()->isNotNull('t.fieldName'))
->andWhere($qb->expr()->orX(
$qb->expr()->in('t.fieldName2', 0),
$qb->expr()->isNull('t.fieldName2')
))
->getQuery()
->getSingleScalarResult();
}
Also think about caching your query result - http://symfony.com/doc/current/reference/configuration/doctrine.html#caching-drivers
public function count()
{
$qb = $repository->createQueryBuilder('t');
return $qb
->select('count(t.id)')
->getQuery()
->useQueryCache(true)
->useResultCache(true, 3600)
->getSingleScalarResult();
}
In some simple cases using EXTRA_LAZY entity relations is good
http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/tutorials/extra-lazy-associations.html
If you need to count a more complex query, with groupBy, having etc... You can borrow from Doctrine\ORM\Tools\Pagination\Paginator:
$paginator = new \Doctrine\ORM\Tools\Pagination\Paginator($query);
$totalRows = count($paginator);
Since Doctrine 2.6 it is possible to use count() method directly from EntityRepository. For details see the link.
https://github.com/doctrine/doctrine2/blob/77e3e5c96c1beec7b28443c5b59145eeadbc0baf/lib/Doctrine/ORM/EntityRepository.php#L161
Example working with grouping, union and stuff.
Problem:
$qb = $em->createQueryBuilder()
->select('m.id', 'rm.id')
->from('Model', 'm')
->join('m.relatedModels', 'rm')
->groupBy('m.id');
For this to work possible solution is to use custom hydrator and this weird thing
called 'CUSTOM OUTPUT WALKER HINT':
class CountHydrator extends AbstractHydrator
{
const NAME = 'count_hydrator';
const FIELD = 'count';
/**
* {#inheritDoc}
*/
protected function hydrateAllData()
{
return (int)$this->_stmt->fetchColumn(0);
}
}
class CountSqlWalker extends SqlWalker
{
/**
* {#inheritDoc}
*/
public function walkSelectStatement(AST\SelectStatement $AST)
{
return sprintf("SELECT COUNT(*) AS %s FROM (%s) AS t", CountHydrator::FIELD, parent::walkSelectStatement($AST));
}
}
$doctrineConfig->addCustomHydrationMode(CountHydrator::NAME, CountHydrator::class);
// $qb from example above
$countQuery = clone $qb->getQuery();
// Doctrine bug ? Doesn't make a deep copy... (as of "doctrine/orm": "2.4.6")
$countQuery->setParameters($this->getQuery()->getParameters());
// set custom 'hint' stuff
$countQuery->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, CountSqlWalker::class);
$count = $countQuery->getResult(CountHydrator::NAME);
For people who are using only Doctrine DBAL and not the Doctrine ORM, they will not be able to access the getQuery() method because it doesn't exists. They need to do something like the following.
$qb = new QueryBuilder($conn);
$count = $qb->select("count(id)")->from($tableName)->execute()->fetchColumn(0);
To count items after some number of items (offset), $qb->setFirstResults() cannot be applied in this case, as it works not as a condition of query, but as an offset of query result for a range of items selected (i. e. setFirstResult cannot be used togather with COUNT at all). So to count items, which are left I simply did the following:
//in repository class:
$count = $qb->select('count(p.id)')
->from('Products', 'p')
->getQuery()
->getSingleScalarResult();
return $count;
//in controller class:
$count = $this->em->getRepository('RepositoryBundle')->...
return $count-$offset;
Anybody knows more clean way to do it?
Adding the following method to your repository should allow you to call $repo->getCourseCount() from your Controller.
/**
* #return array
*/
public function getCourseCount()
{
$qb = $this->getEntityManager()->createQueryBuilder();
$qb
->select('count(course.id)')
->from('CRMPicco\Component\Course\Model\Course', 'course')
;
$query = $qb->getQuery();
return $query->getSingleScalarResult();
}
You can also get the number of data by using the count function.
$query = $this->dm->createQueryBuilder('AppBundle:Items')
->field('isDeleted')->equals(false)
->getQuery()->count();

Resources