adesigns / calendar-bundle

This bundle allows you to integrate the jQuery FullCalendar plugin into your Symfony2 application.
MIT License
97 stars 60 forks source link

Can't load events #59

Closed Mistwostem closed 7 years ago

Mistwostem commented 7 years ago

Hi everyone and @mikeyudin , i have encountered an issue with the bundle. In fact everything works well, at least the calendar displays itself on my page but unfortunately, i can't load events by clicking on it. I think that i might have an issue with my EntityEvent class which is below :

There is the entity code that i took from a stackoverflow issue :

https://stackoverflow.com/questions/37980272/extending-adesigns-evententity-class

My Entity Code :

<?php

namespace Permisfute\PermisfuteBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use ADesigns\CalendarBundle\Entity\EventEntity;

/**
 * LeconEvent
 *
 * @ORM\Table(name="lecon_event")
 * @ORM\Entity(repositoryClass="Permisfute\PermisfuteBundle\Repository\LeconEventRepository")
 */

class LeconEvent extends EventEntity
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var  string
     * @ORM\Column(name="title", type="string", length=255)
     */
    protected $title;

    /**
     * @var  string
     * @ORM\Column(name="url", type="string", length=255, nullable=true)
     */
    protected $url;

    /**
     * @var  string
     * @ORM\Column(name="bgColor", type="string", length=255)
     */
    protected $bgColor;

    /**
     * @var  string
     * @ORM\Column(name="fgColor", type="string", length=255)
     */
    protected $fgColor;

    /**
     * @var  string
     * @ORM\Column(name="cssClass", type="string", length=255, nullable=true)
     */
    protected $cssClass;

    /**
     * @var  bool
     * @ORM\Column(name="allDay", type="boolean")
     */
    protected $allDay;

    /**
     * @var  DateTime
     * @ORM\Column(name="startDatetime", type="datetime")
     */
    protected $startDatetime;

    /**
     * @var  DateTime
     * @ORM\Column(name="endDatetime", type="datetime")
     */
    protected $endDatetime;

    public function __construct($title, \DateTime $startDatetime, \DateTime $endDatetime = null, $allDay = false, $hall) {
        parent::__construct($title, $startDatetime, $endDatetime, $allDay);
        $this->hall = $hall;
    }

    /**
     * Get id
     *
     * @return int
     */
    public function getId() {
        return $this->id;
    }
}

The thing is when im loading the page, the calendar displays itself nicely but i cant click to load events, when i go into the symfony profiler, ive got this error :

[Semantical Error] line 0, col 109 near 'event_datetime': Error: Class Permisfute\PermisfuteBundle\Entity\LeconEvent has no field or association named event_datetime

It appears that the : company_events.event_datetime from the loadEvent function in the CalendarListener is not working.

there is also my CalendarEventListener class just in case :

<?php

namespace Permisfute\PermisfuteBundle\EventListener;

use ADesigns\CalendarBundle\Event\CalendarEvent;
use ADesigns\CalendarBundle\Entity\EventEntity;
use Doctrine\ORM\EntityManager;

class CalendarEventListener
{
    private $entityManager;

    public function __construct(EntityManager $entityManager)
    {
        $this->entityManager = $entityManager;
    }

    public function loadEvents(CalendarEvent $calendarEvent)
    {
        $startDate = $calendarEvent->getStartDatetime();
        $endDate = $calendarEvent->getEndDatetime();

        // The original request so you can get filters from the calendar
        // Use the filter in your query for example

        $request = $calendarEvent->getRequest();
        $filter = $request->get('filter');

        // load events using your custom logic here,
        // for instance, retrieving events from a repository

        $companyEvents = $this->entityManager->getRepository('PermisfuteBundle:LeconEvent')
                          ->createQueryBuilder('company_events')
                          ->where('company_events.event_datetime BETWEEN :startDate and :endDate')
                          ->setParameter('startDate', $startDate->format('Y-m-d H:i:s'))
                          ->setParameter('endDate', $endDate->format('Y-m-d H:i:s'))
                          ->getQuery()->getResult();

        // $companyEvents and $companyEvent in this example
        // represent entities from your database, NOT instances of EventEntity
        // within this bundle.
        //
        // Create EventEntity instances and populate it's properties with data
        // from your own entities/database values.

        foreach($companyEvents as $companyEvent) {

            // create an event with a start/end time, or an all day event
            if ($companyEvent->getAllDayEvent() === false) {
                $eventEntity = new EventEntity($companyEvent->getTitle(), $companyEvent->getStartDatetime(), $companyEvent->getEndDatetime());
            } else {
                $eventEntity = new EventEntity($companyEvent->getTitle(), $companyEvent->getStartDatetime(), null, true);
            }

            //optional calendar event settings
           /* $eventEntity->setAllDay(true); // default is false, set to true if this is an all day event
            $eventEntity->setBgColor('#FF0000'); //set the background color of the event's label
            $eventEntity->setFgColor('#FFFFFF'); //set the foreground color of the event's label
            $eventEntity->setUrl('http://www.google.com'); // url to send user to when event label is clicked
            $eventEntity->setCssClass('my-custom-class'); // a custom class you may want to apply to event labels
            */

            //finally, add the event to the CalendarEvent for displaying on the calendar
            $calendarEvent->addEvent($eventEntity);
        }
    }
}

Do you have any ideas?

Thanks in advance for your help

Mistwostem commented 7 years ago

And there is my service code as well :

services:
    permisfuteBundle.calendar_listener: 
        class: Permisfute\PermisfuteBundle\EventListener\CalendarEventListener
        arguments: [ @doctrine.orm.entity_manager ]
        tags:
            - { name: kernel.event_listener, event: calendar.load_events, method: loadEvents}
mikeyudin commented 7 years ago

This bundle does not care how you load events. All it does is dispatch the event "calendar.load_events", and expects your code to add events to the CalendarEvent class. It has no knowledge of how you are querying your database, or where the events are coming from. You are copying an example EventListener from the docs. That example does not need to match how your code works at all.

EventEntity is not meant to be used as a Doctrine Entity. You don't need to extend it. You just need to populate new instantiated EventEntity objects and pass them to the CalendarEvent.

Mistwostem commented 7 years ago

Hi @mikeyudin, thanks a lot for yourquick and clear answer! :) I understand better now how the bundle works, i ll try to apply your advices to my project.

Have a good evening 👍