在symfony2中向CalendarBundle添加事件

时间:2015-01-21 13:20:17

标签: php jquery symfony calendar

我按照此链接上的教程安装了CalendarBundle -jQuery fullcalendar:https://github.com/adesigns/calendar-bundle我成功显示了日历。但问题是我无法通过点击日历来添加事件,尽管我按照本教程的这一部分添加了一个Event Listener类:https://github.com/adesigns/calendar-bundle#adding-events。另外,我不知道如何使用我安装的软件包处理数据库。

那么,我该怎么做?

我已经从名为AppBundle的新捆绑包中呈现了日历,并且在文件夹EventListener中添加了CalendarEventListener.php类。下面是CalendarEventListener.php的代码

<?php


   namespace AppBundle\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('AppBundle:MyCompanyEvents')
                      ->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.

    //echo sizeof($companyEvents);
    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);
    }
}

}

最后我还在AppBundle / Resource / Config / service.xml中添加了service.xml 下面是service.xml的代码

<?xml version="1.0" ?>
 <container xmlns="http://symfony.com/schema/dic/services">

   <services>
    <service id="appbundle.calendar_listener" class="AppBundle\EventListener\CalendarEventListener">
        <argument type="service" id="doctrine.orm.entity_manager" />
        <tag name="kernel.event_listener" event="calendar.load_events" method="loadEvents" />
    </service>

</services>

它没有显示错误,但同时也没有添加事件。 提前谢谢。

1 个答案:

答案 0 :(得分:2)

确保在与ADesigns相同的捆绑包中注册事件加载服务。另一件事是检查您的网络选项卡是否将请求发送到日历控制器以及您获得的响应。如果不相应地更新路由文件。