0

In my Symfony2 application there is an entity called 'Appointment'. This entity has a field/property called "date". It has a format like: '2012-12-24'.

I have created a form called AppointmentType, to be able to search for appointments based on their properties. The form is used in AppointmentController, and rendered to appointment.html.twig.

So far so good. I can search appointments based on all fields. I can for instance search on a specific date (that is, on an exact match).

However, I would like to be able to search for appointments given a date range (using a start and end date). The question would be: "Give me all appointments between this and that date." I've tried many things but can't figure it out in Symfony2.

Some random thoughts here:

  • Should I create two extra fields in the Appointment entity beside "date", call them "startdate" and "enddate", not link them to the MySql table but only use them for search?
  • Should I do something in the form, creating two date fields linked to the same date property of the entity (seems to cause violation)?
  • Should I not use a Symfony2 form for this, but do it the old fashion way (manually write and process the form)? This works at the moment but it is far from elegant in a Symfony2 environment.

Any help is welcome. Especially a (complete) working code example would be awesome, so I can study it.

4

1 回答 1

0
public function getAppointmentsByDate($startDate, $endDate)
{
   $query = $em->createQuery('SELECT a FROM Appointment a WHERE a.date BETWEEN ?1 AND ?2');
   $query->setParameter(1, $startDate);
   $query->setParameter(2, $endDate);
   return $query->getResult();
}
于 2012-05-18T10:11:37.330 回答