Where clause to parents - php

I have for example two tables: Parent and Children. They have relations many-to-many between each other. Everything is working well, but I don't know how to use where clause in query builder for this.
My code:
$parent = $em->getRepository('AppBundle:Parent')->find(1);
$qb = $em->createQueryBuilder();
$qb->select('c, p')
->from('AppBundle:Children', 'c')
->leftJoin('c.parents', 'p')
->where('p.id = :parent')
->setParameter('parent', $parent)
;
$childrens = $qb->getQuery()->getResult();
This always return me null.
I know - I can use $parent->getChildrens(), but I would like use createQueryBuilder for AppBundle:Children.
How should the where clause look?

Related

Doctrine QueryBuilder from with subquery

I need to write some complex query builder expression. The sql looks like:
SELECT query,popularity,nb_words
FROM gemini_suggestion
WHERE query IN
(
SELECT * FROM
(
SELECT query
FROM gemini_suggestion
GROUP BY query
HAVING COUNT(query) > 1
) AS subquery
);
The code im stucked at is:
$queryBuilder = $em->createQueryBuilder();
$queryBuilder->select('a')
->from($this->entity['class'], 'a')
->where($queryBuilder->expr()->In('a.query', $subQuery->getDQL()));
$subQuery2 = $em->createQueryBuilder()
->select('b.query')
->from($this->entity['class'], 'b')
->groupBy('b.query')
->having('count(b.query)>1')
;
$subQuery = $em->createQueryBuilder();
$subQuery->select('a')
->from(...)
;
Any help would be nice!
You can try something like this. I'm assuming you have a namespace Entities where you have all your entities classes.
// Subquery to get the queries greater than 1
$subQueryBuilder = $this->entityManager->createQueryBuilder();
$subQueryBuilder->select('gemini_suggestion.query')
->from('Entities\gemini_suggestion', 'gemini_suggestion')
->groupBy('gemini_suggestion.query')
->having($subQueryBuilder->expr()->gt('COUNT(gemini_suggestion.query)', 1));
// Main query where you use the subquery to filter out the records you want
$queryBuilder = $this->entityManager->createQueryBuilder();
$queryBuilder->select('query, popularity, nb_words')
->from('Entities\gemini_suggestion', 'gemini_suggestion')
->where($queryBuilder->expr()->in('gemini_suggestion.query', $subQueryBuilder->getDQL()));
$result = $queryBuilder->getQuery()->getResult();
return $result;

Symfony Doctrine findBy and then map

Basically I want to execute this mysql query with doctrine:
select distinct user_id from work_hour where project_id = ?;
But I don't know how I can do this with pretty Doctrine code. Is it possible to make it look like the following pseudo code or do I have to use the query builder?:
$project = new Project();
...
$entities = $em->getRepository('AppBundle:WorkHour')
->findByProject($project)->selectUser()->distinct();
Where $entities is an array of User objects
WorkHour and Project have a ManyToOne relation
WorkHour and User have a ManyToOne relation
You'll need to use a QueryBuilder for that, but that would still be quite a "pretty Doctrine code" and would still look quite like your pseudo-code.
Something like this should work:
$queryBuilder = $em->createQueryBuilder();
$query = queryBuilder
->select('u')
->distinct()
->from('AppBundle:User', 'u')
->join('AppBundle:WorkHour', 'wh')
->where('u.workHour = wh')
->andWhere('wh.project = :project')
->getQuery();
$query->setParameter(':project', $project);
$entities = $query->getResult();
public function findByProject($project)
{
$qb = $this->getEntityManager()->createQueryBuilder('User');
$qb
->select('User')
->from('Path\Bundle\Entity\User', 'User')
->join('Path\Bundle\Entity\WorkHour', 'wh',
'WITH', 'User.workHour = wh')
->where('wh.project = :project'))
->distinct()
->setParameter('project', $project)
;
$query = $qb->getQuery();
return $query->getResult();
}
If you have a complicated query you should do it in QueryBuilder, it'll be more efficient.
http://doctrine-orm.readthedocs.org/en/latest/reference/query-builder.html
If you have complicated queries you shouldn't do it directly into the controller, it shouldn't know this logic, you have to do it in the repository and call it from there

Doctrine2 get object without relations

An user has one role.
A role has zero or many users.
I would like to find roles without users.
I need to have this query without using IN or NOT IN
I tried with join:
$qb = $this->createQueryBuilder('role');
$qb
->leftJoin('role.users', 'users')
->where('users IS NULL')
without join
$qb = $this->createQueryBuilder('role');
$qb
->where('role.users IS NULL')
with id:
$qb = $this->createQueryBuilder('role');
$qb
->leftJoin('role.users', 'users')
->where('users.role != role')
Do you have other ideas? Do I have no other choices than to use IN / NOT IN queries?
Thanks in advance
You can find roles that don't have any users by using a count query
$qb = $this->createQueryBuilder('role');
$qb ->addSelect('COUNT(users.id) AS total_users')
->leftJoin('role.users', 'users')
->groupBy('role.id')
->having('total_users = 0')
->getQuery()->getResult();

How to perform a join query using Symfony and Doctrine Query Builder

I have two entities which are connected through a 1:1 relationship, e.g: MyEntity.idRelatedEntity
I want to create a Doctrine query where I can retrieve data from MyEntity depending on a value from a certain column in RelatedEntity. Something like this (it doesn't work of course):
$entity = $em
->getRepository('MyBundle:RelatedEntity')
->createQueryBuilder('e')
->leftJoin('MyBundle:RelatedEntity', 'r')
->where('r.foo = 1')
->getQuery()
->getResult();
Any help would be much appreciated :)
$entity = $em
->getRepository('MyBundle:MyEntity')
->createQueryBuilder('e')
->join('e.idRelatedEntity', 'r')
->where('r.foo = 1')
->getQuery()
->getResult();
Also left join makes no sense here (because of where clause that will make it work like inner join)
Note that you should write this query in your MyEntityRepository
public function getMyEntityWithRelatedEntity($parameter)
{
$query = $this->createQueryBuilder('e')
->addSelect('r') // to make Doctrine actually use the join
->leftJoin('e.relatedEntity', 'r')
->where('r.foo = :parameter')
->setParameter('parameter', $parameter)
->getQuery();
return $query->getResult();
}
And then use it in your controller/service :
$manager = $this->getDoctrine()->getManager();
$results = $manager->getRepository(MyEntity::class)->getMyEntityWithRelatedEntity(1);

Doctrine 2 - Find all entities in a repository except one with a particular id value

I'm using Doctrine 2.
I want to get all Entities of an entity class except for the one with id = 0.
I could use QueryBuilder like this:
// $em is EntityManager
$em->createQueryBuilder()
->select('c')
->from('Category', 'c')
->where('c.id <> 0')
->getQuery()
->getResult();
But looking at this code, it feels like too much for such a simple task.
I'm curious to know if there is any other simpler way in Doctrine for doing this.
Nop, that's how you should do it. You could omit query builder and pass entire DQL query:
$em->createQuery("SELECT c FROM Category c WHERE c.id != 0")->getResult();
It's not the better practice to pass the ID number, you might pass the Category object like this :
public function findAllExcept($category) {
$qb = $this->createQueryBuilder('Category');
$qb->add('select', 'c')
->add('from', 'Category c')
->add('where', 'c != :category')
->setParameter('category', $category);
return $qb->getQuery()->getResult();
}
For comparisons and conditions I recommend use Doctrine as well. It will save your time in case of switch between databases, for instance MySQL to PostgreSQL:
$this->createQueryBuilder()
->select('c')
->from('Category', 'c')
->where($qb->expr()->neq('c.id', '?1'))
->setParameter('1', $category)
->getQuery()
->getResult()

Categories