How can I generate this query in doctrine or query builder?
SELECT EndDate from helios.fsa_audits order by StartDate desc limit 1;
Any idea or Advice about how to make it?
Seeing your entity here, in your controller you can try this:
$em = $this->getDoctrine()->getManager();
$qb = $em->createQueryBuilder()
->select(array('a'))
->from(FsaAudits::class, 'a')
->orderBy("a.StartDate","DESC")
->setMaxResults(1);
$resultset = $qb->getQuery()->getResult();
if (count($resultset) <= 0) {
$fsaobj = $resultset[0];
echo $fsaobj->getEndDate();
}
See Working with query builder
Related
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;
I have a query which looks like below:
SELECT *
FROM mydb.users
left join mydb.job on
users.id = job.userid;
Now I am using doctrine orm in querying database but I am newbie here.
What I have done so far is below but it doesn't work as expected.
$em = $this->getEntityManager();
$qb = $em->createQueryBuilder();
$qb->select(array('a', 'c'))
->from('Admin\Entity\User', 'a')
->leftJoin('a.id', 'c');
$query = $qb->getQuery();
$results = $query->getResult();
return $results;
you do not need the from clause and array from clause select
$em = $this->getEntityManager();
$qb = $em->createQueryBuilder('u');
$qb->select('u', 'j')
->leftJoin('u.jobs', 'j')
$query = $qb->getQuery();
$results = $query->getResult();
return $results;
it is mandatory that your users entity contains a "one to many" jobs for the attribute.
trick to display the SQL output :
var_dump($qb->getQuery()->getSQL());
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
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);
I have a previously writen query in other Doctrine project using Query Builder.
$qb->select('c', 'm')
->from('Chapter c')
->leftJoin('c.Book m')
->orderBy('c.chapterno DESC')
->addOrderBy('m.id ASC')
->groupBy('m.id')
->where('c.chapterno = (SELECT MAX(v.chapterno) FROM Chapter v WHERE v.mid = m.id)')
->limit($max);
How can I replicate this in my inherited class from Doctrine_table in symfony?
I am using symfony 1.4
In your ChapterTable class, something like the following:
public function getChapters($max)
{
$qry = self::getInstance()
->createQuery("c")
->leftJoin('c.Book m')
->orderBy('c.chapterno DESC')
->addOrderBy('m.id ASC')
->groupBy('m.id')
->where('c.chapterno = (SELECT MAX(v.chapterno) FROM Chapter v WHERE v.mid = m.id)')
->limit($max);
return $qry->execute();
}
Then you can call this with eg:
$myMaxValue = 25;
$results = Doctrine::getTable("Chapter")->getChapters($myMaxValue);
Here is a nice solution to organize your queries: