mehmet mehmet - 5 months ago 42
jQuery Question

fullcalendar not rendering the event

I am using full calendar with symfony. However the events not been rendered. I tried every way.

Controller Code

/**
* @Route("/get-booking-to-calendar/{id}", name="booking-to-calendar")
*/
public function BookingCalendarAction($id)
{
$em=$this->getDoctrine()->getManager();

$field = $em->getRepository('AppBundle:Field')->find($id);

$bookings= $em->getRepository('AppBundle:Booking')->findBy(array('field_booking' => $field->getId()));

foreach($bookings as $booking)
{
$booking_array[] = array(
'id' => $booking->getId(),
'title' => $booking->getDuration(),
'start' => $booking->getStartTime(),
'end' => $booking->getEndTime(),
'allDay'=>true,
);

}
$response = new Response(json_encode($booking_array));
$response->headers->set('Content-Type', 'application/json');
return $response;


}


Twig

<script>
$(document).ready(function() {

var calendar = $('#calendar').fullCalendar({
editable: true,
timeFormat: 'h:mm',
allDaySlot:false,

slotDuration : '00:15:00',
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},

events: "{{ path ('booking-to-calendar', {'id':field_id}) }}",
// Convert the allDay from string to boolean

eventRender: function(event, element, view) {
if (event.allDay === 'true') {
event.allDay = true;
} else {
event.allDay = true;
}
},

});
});

</script>


and my json respond is

[{"id":2,"title":4,"start":{"date":"2016-06-18 11:00:00.000000","timezone_type":3,"timezone":"Europe\/London"},"end":{"date":"2016-06-18 12:00:00.000000","timezone_type":3,"timezone":"Europe\/London"},"allDay":true}]


it seems eveything is ok. but the events are not coming on the calendar.

here is html render

click to view image

Answer

finally I sort it.

First I create a repository class and add following function.

 public function GetBookings($fieldId)
{
    $sql = "SELECT b.id, b.start_time as start, b.end_time as end, CONCAT( c.first_name,', ', c.last_name) AS title FROM bookings b  LEFT JOIN clients c on b.client_id = c.id WHERE b.field_id=:fieldId ";
    $params = array(
        ':fieldId'=>$fieldId
    );

    return $this->getEntityManager()->getConnection()->executeQuery($sql, $params)->fetchAll();

}

then change my controller

/**
 * @Route("/get-booking-to-calendar/{id}", name="booking-to-calendar")
 */
public function BookingCalendarAction($id)
{
    $em=$this->getDoctrine()->getManager();

    $bookings= $em->getRepository('AppBundle:Booking')->GetBookings($id);
    $response = new Response(json_encode($bookings));
    $response->headers->set('Content-Type', 'application/json');
    return $response;

}