I have two entities Question and Answer with appropriate relationships.
I want an optimized way (less resource) to find all questions (only id and title) without answers with doctrine.
Thanks for your answer.
In your controller…
Using the QueryBuilder:
$repository = $this->getDoctrine()->getRepository('AcmeStoreBundle:Question');
$qb = $repository->createQueryBuilder('Question');
$query = $qb
->select('DISTINCT Question.id, Question.title')
->leftJoin('Question.answers', 'Answer', $qb->expr()->isNull('Answer.id'))
->getQuery();
$questions = $query->getArrayResult();
Or DQL (partial objects:
$query = $em->createQuery("select partial Question.{id,title}
from MyApp\Domain\Question Question
left join Question.answers Answer
where Answer.id is NULL");
$questions = $query->getResult();
The query builder will return an array while the DQL will return partial objects.
See:
http://docs.doctrine-project.org/en/latest/reference/query-builder.html
http://docs.doctrine-project.org/en/latest/reference/partial-objects.html
Related
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();
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;
I have a table of videos and in that table I have field comment which contains id of comment in other table, now I used join query to get that in one query, but how do I get that comment?
Here is my code:
$Actions = $this->EntityManager()->getRepository('AppBundle:Video')
->createQueryBuilder('V')
->join('AppBundle:VideoComment', 'VC')
->where('V.videoId = :VideoID')
->andWhere('VC.videoId = :VideoID')
->setParameter('VideoID', $VideoID)
->getQuery()
->getResult();
How do I get the actual comment from that joined entity?
You can do what #cezar said earlier but with one little change: you have to define field to retrieve related entries from comments table.
So, your query might look like this:
$em = $this->get('doctrine.orm.entity_manager');
$videos = $em->createQuery('select v
from YourBundle:Video v
left join YourBundle:Comment c
where v.comment = c.id')
->getResult();
or you can do the similar stuff using query builder:
$videos = $em->createQueryBuilder('v')
->add('select', 'v, c')
->add('from', 'YourBundle:Video v')
->leftJoin('YourBundle:Comment', 'c')
->where('v.comment = c.id')
... // some other conditions if you need
->getQuery()
->getResult();
Both cases I described account for that Video and Comment entity might not be in formal relations (I mean their relations might not be described in your doctrine/orm file).
Here is one proposal:
<?php
namespace You\AppBundle\Repository; // You is your vendor name, AppBundle is your bundle
use Doctrine\ORM\EntityRepository;
class VideoCommentRepository extends EntityRepository
{
public function getVideoComment($VideoId)
{
$query = $this->getEntityManager()->createQuery(
'SELECT v FROM YouAppBundle:Video v LEFT JOIN v.comment c
WHERE v.id = :id'
)->setParameter('id', $VideoId);
return $query->getResult();
}
}
As you said you have a table 'video' and in that table there is a field 'comment' that contains the IDs of the comments. I suppose you have 'oneToMany' relation from 'video' to 'comment'. With this simple query you should be able to get all comments for a given VideoID. I didn't test this, but I think it should work. Try it out and adapt it as needed.
I can't find the answer in the documentation.
I use symfony for an application, i have a database with a datetime for each of my table and would like to get only the row with the most recent datetime is there a function like $repository->findOneById($id) to obtain this object ?
Here is a solution using QueryBuilder:
$repository = $this->getDoctrine()
->getRepository('AcmeStoreBundle:Product');
$query = $repository->createQueryBuilder('p')
->orderBy('p.datetime', 'DESC')
->setMaxResults(1)
->getQuery();
$product = $query->getResult();
Regards.
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.