I hope I'm able to ask this question without annoying or giving someone a headache. I'm new to Symfony and trying to wrap my head around everything.
Here is what I want to do: In a bundle called CalendarBundle I have an Event
Entity where the Repository class looks like this:
class EventRepository extends EntityRepository
{
/**
* @param $start
* @param $end
* @return array $events_array
*/
public function queryByRange($start = "", $end = "")
{
// set start and end to this month if empty
$start = ($start == "")
? date('Y-m-d H:i:s', mktime(0, 0, 0, date('m'), 1, date('Y')))
: $start;
$end = ($end == "")
? date('Y-m-d H:i:s', mktime(0, 0, 0, date('m'), date('t'), date('Y')))
: $end;
$query = $this->createQueryBuilder('e')
->where('e.start > :start')
->andWhere('e.end < :end')
->setParameter('start', $start)
->setParameter('end', $end)
->orderBy('e.start', 'ASC');
return $query;
}
/**
* @param string $start
* @param string $end
* @return array
*/
public function findByRange($start = "", $end = "")
{
// set start and end to this month if empty
$start = ($start == "")
? date('Y-m-d H:i:s', mktime(0, 0, 0, date('m'), 1, date('Y')))
: $start;
$end = ($end == "")
? date('Y-m-d H:i:s', mktime(0, 0, 0, date('m'), date('t'), date('Y')))
: $end;
$events = $this->queryByRange($start, $end)->getQuery()->getResult();
$events_array = array();
$prev_date = '';
foreach($events as $event) {
$start = $event->getStart()->format('Ymj');
if ($prev_date == $start) {
$events_array[$prev_date][] = $this->eventToArray($event);
} else {
$events_array[$start][] = $this->eventToArray($event);
}
$prev_date = $start;
}
return $events_array;
}
/**
* @param Event $event
* @return array
*/
private function eventToArray(Event $event)
{
return array(
'item' => array(
'id' => $event->getId(),
'name' => $event->getName(),
'description' => $event->getDescription(),
'type' => $event->getType(),
'status' => $event->getStatus(),
'start' => $event->getStart()->format('Ymd H:i:s'),
'end' => $event->getEnd()->format('Ymd H:i:s'),
),
);
}
}
What I'm trying to accomplish is to extend this base Event Entity with several more specific events adding different event meta for each of these. I figure the queryByRange
function needs to be rewritten to also get meta data, and so does the eventToArray
function.
Later I'm displaying events using different twig templates using the Event item type to decide on templates used.
Now the thing that gives me a headache. Because I don't know what Events that may exist, I want to use this EventRepository class when getting all kinds of events in the Controller like this:
$repository = $this->getDoctrine()->getRepository('AcmeCalendarBundle:Event');
$events = $repository->findByRange($start, $end);
and the correct extended class is used automagically to query and return events.
I found this question where extending Bundles was the right way to do it. But I don't think this solves my question.
Here's an example of what I think you wanted to achive:
And your event repository could be simply:
I didn't include the start and end fields on the entities, but assuming you have them, then you can do:
Assuming $start and $end are correct \DateTime objects, the result would be a list of entities with match the specified date range, but each individual object would be of appropriate class (eg. MusicEvent, SportEvent).
Read more here: http://doctrine-orm.readthedocs.org/en/latest/reference/inheritance-mapping.html#single-table-inheritance