Doctrine QueryBuilder leftJoin + MEMBER OF fails to work - symfony

This piece of code failse to produce the correct results:
$queryBuilder
->leftJoin(sprintf('%s.building', $rootAlias), 'building')
->andWhere(':user MEMBER OF building.owners OR :user MEMBER OF building.managers')
->setParameter('user', $user);
whereas this returns a correct result:
$queryBuilder
->leftJoin(sprintf('%s.building', $rootAlias), 'building')
->andWhere('building.id = :id')
->setParameter('id', 1);
$user is properly defined and member of building.managers.

It was a caching issue apparently. The above code works.

Related

How to fetch a particular field from Symfony repository query result

Hello, I made a query in the repository Library and i am getting this result. From that i need to fetch the "code" of each of the users. Then i will load the resulting file in the form in order to build a "choice_type" with the codes. Is there a simple way to get these codes. Thank you.
Code for the query:
public function findUCodesByLibrairy($value)
{
return $this->createQueryBuilder('l')
->andWhere('u.librairy = :id')
->addSelect('u')
->leftJoin('l.users', 'u')
->setParameter('id', $value)
->getQuery()
->getResult();
}
You almost did as you need, try to request not all properties, but a specific one as shown in the example.
public function findUCodesByLibrairy($value)
{
return $this->createQueryBuilder('l')
->andWhere('u.librairy = :id')
->Select('u.code') // hier we are requesting a specific property
->leftJoin('l.users', 'u')
->setParameter('id', $value)
->getQuery()
->getResult();
}

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

DQL query ManyToMany IN

This query in not working. What could be wrong? Post has author(User entity). User has Following(ManyToMany self-directing)
I'm trying to get all Posts of the Users which I'm following
$userId = $this->getUser()->getId();
$qb = $em->createQueryBuilder();
$qb2 = $qb;
$qb2 = $em->createQueryBuilder()
->select('u.following')
->from('AppBundle\Entity\User', 'u')
->where('u.id = :userId');
$qb->select('p')
->from('AppBundle\Entity\Post', 'p')
->where($qb->expr()->In('p.author', $qb2->getDQL()));
$qb->setParameter('userId', $userId);
$dql = $qb->getDQL();
I will assume that your code is in repository as it should be...
src/AppBundle/Controller/PostController.php
$em=$this->getDoctrine()->getManager();
$myResultCollection=$em->getRepository(Post::class)->myCustomQuery($this->getUser());
src/AppBundle/Repository/PostRepository.php
public function myCustomQuery(User $user) {
$em=$this->getEntityManager();
$qb=$em->createQueryBuilder();
return $qb->select("p")
->from(Post::class, "p")
->where($qb->expr()->in("p.author",
$qb->select("u.following")
->from(User::class, "u")
->where("u.id=:USERID")
->getDQL()))
->setParameters(array('USERID'=>$user->getId()))
->getQuery()
->execute();
}
As side note, you should be careful with quotes.
Make sure to use double quotes when using DQL (it's true for SQL too).
The reason is because you need simple quote for strings in queries.

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();
}

Symfony Query Builder: remove results from a request from another requests

I have a first request (I used QueryBuilder but it is a simple findAll()):
public function findAllRosters(){
$qb = $this->createQueryBuilder('r')
;
return $qb->getQuery()
->getResult();
}
I want to remove from the results of this first query all results from another query:
public function findOtherRosters($user){
$qb = $this->createQueryBuilder('r')
->leftJoin('r.members', 'm')
->addSelect('m')
->where('m.user = :user')
->setParameter('user', $user)
;
return $qb->getQuery()
->getResult();
}
Is there a simple way? It seems that using a where NOT IN might be the way forward..
EDIT
I have tried to follow this exemple: https://stackoverflow.com/a/22616937/4228086 see my answer
Have a look at this stackoverflow solution
Here q2 is your second query, the sub result you want to reduce the first result with.
Hope that helps.
After much googling; I finally found the right Query:
public function findOtherRosters($user){
$q2 = $this->createQueryBuilder('r')
->leftJoin('r.members', 'm')
->addSelect('m')
->where('m.user = :user')
->setParameter('user', $user);
$qb = $this->createQueryBuilder('ro');
$qb
->where('ro not IN (:rosters)')
->setParameter('rosters', $q2->getQuery()->getResult())
;
return $qb->getQuery()
->getResult();
}
Hope it can help others with the same issue

Resources