21780

Symfony 2 + Doctrine 2: find a record based on a date field using a DateTime value

Question:

I have an entity called Event, with a field startDate (type="date"), and another called slug (type="string"). I have created a controller action to get a specific event based on these parts. The router passes eventDate as the format 'Y-m-d', and the eventSlug. Now I have successfully created a DateTime object with $startDate = DateTime::createFromFormat('Y-m-d', $eventDate). Now if I issue

$event = $eventRepo->findOneBy(array( 'startDate' => $startDate, 'slug' => $eventSlug, ));

it gets nothing ($event is NULL). Do I miss something in Doctrine documentation, or did I find a bug?

Answer1:

First ensure that actually an Event with the given slug and start date exists. Is slug unique field?

Anyway you can let Doctrine do the param conversion for you, without using PHP format function:

$repo = $this->getDoctrine()->getRepository('AcmeHelloBundle:Event'); $qb = $repo->createQueryBuilder('e'); $event = $qb ->andwhere($qb->expr()->eq('e.slug', ':slug')) ->andWhere($qb->expr()->eq('e.start_date', ':start_date')) ->setParameter('slug', $eventSlug) ->setParameter('start_date', $startDate) ->getQuery() ->getOneOrNullResult();

Or, use the Doctrine param converter:

/** * @Route("/event/show/{slug}/{start_date}") * @Method("GET") * @ParamConverter("event", class="AcmeHelloBundle:Event") * @Template */ public function showAction(Event Event) { }

Internally, Doctrine param converter do it for you: It checks for any parameter in the request and find an entity using slug and start_date.

Recommend

  • org.hibernate.QueryException: could not resolve property: filename
  • Create a new date from php array
  • Populating a UITableView with plist dictonary
  • Format yyymmdd date
  • PHP DateTime::createFromFormat not working for time without leading zero
  • Query for a specific date with MongoDB and NodeJS
  • How to handle date lower than 1970 in MongoDB
  • Laravel 4 carbon date format change only once
  • PHP: Filter array by date range
  • How to do a Lucene search with Sitecore item with specific date?
  • Left and right button misbehaving when trying to add an empty span to contenteditable div
  • writing file in heroku filesystem and reading it with web app
  • YAJSW window service did not start
  • proxy request in node.js / express
  • MAVEN : Run Multiple Maven Project using Maven Test
  • How to implement Deep Linking in Roku SG application?
  • Extract zip entries to another Zip file
  • Invalid object name 'dbo.Item'
  • Jenkins: FATAL: Could not initialize class hudson.util.ProcessTree$UnixReflection
  • TextToSpeech.setEngineByPackageName() triggers NullPointerException
  • ActiveRecord query for a count of new users by day
  • $wpdb not working in file of WordPress plugin
  • Cannot connect to cassandra from Spark
  • Display issues when we change from one jquery mobile page to another in firefox
  • Illegal mix of collations for operation for date/time comparison
  • Different response to non-authenticated users and AJAX calls
  • Apache 2.4 and php-fpm does not trigger apache http basic auth for php pages
  • Fill an image in a square container while keeping aspect ratio
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • Font Awesome Showing Box instead of Icons
  • Properly structure and highlight a GtkPopoverMenu using PyGObject
  • Akka Routing: Reply's send to router ends up as dead letters
  • AT Commands to Send SMS not working in Windows 8.1
  • Data Validation Drop Down Box Arrow Disappearing
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How do I configure my settings file to work with unit tests?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Is it possible to post an object from jquery to bottle.py?
  • Python/Django TangoWithDjango Models and Databases
  • How can I use threading to 'tick' a timer to be accessed by other threads?