Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method findBy() from Doctrine with DateTime

I have the following code, but this would not work:

$event= $this->getDoctrine()
        ->getRepository('AppBundle:Event')
        ->findBy(
            array(
                "datestart" => array(new \DateTime())
            )
        );

I need a list of the events, which are newer then now.

Where is my misstake?

Or isn't that able to get with the default findBy() function of doctrine ?

like image 780
Miracle Johnson Avatar asked Dec 14 '22 10:12

Miracle Johnson


1 Answers

findBy only search for exactly matches while you need greater then. In this case and also many other cases you can write your own query. You could use DQL or the queryBuilder. If you want to be able to re-use your query on other places then you could place your query in a repository class which is a good practice too. (check the docs). Now here you find a DQL example that you can place into your controller:

$events = $this->getDoctrine()
        ->getManager()
        ->createQuery('SELECT e FROM AppBundle:Event e WHERE e.datestart > CURRENT_DATE()')
        ->getResult();

i changed your variabele to $events (with s) because you are expecting possible more than one event.

like image 75
Frank B Avatar answered Dec 17 '22 06:12

Frank B