i need able use following clause in doctrine:
where al.userid = 41 , (thedate between date_sub(curdate(), interval 2 week) , curdate())
and i'm doing is:
$results = $em->getrepository('mybundle:mytable')->findoneby(array('userid' => $userid));
however, haven't been able filter results last 2 weeks without writing sql or dql.
is there way can achieve through doctrine's methods?
you can use querybuilder. assuming using mysql between
, 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 cleaner code , should work across different database platforms supported doctrine2 orm.
Comments
Post a Comment