在 symfony2 中向 CalendarBundle 添加事件

Posted

技术标签:

【中文标题】在 symfony2 中向 CalendarBundle 添加事件【英文标题】:add events to CalendarBundle in symfony2 【发布时间】:2015-03-20 00:51:12 【问题描述】:

我按照以下链接上的教程安装了 CalendarBundle - jQuery fullcalendar:https://github.com/adesigns/calendar-bundle 并且我成功地显示了日历。但问题是我无法通过单击日历来添加事件,尽管我按照教程的这一部分添加了事件侦听器类: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】:

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

【讨论】:

所以你是对的,路由文件和service.xml路径有问题!

以上是关于在 symfony2 中向 CalendarBundle 添加事件的主要内容,如果未能解决你的问题,请参考以下文章

Symfony2 无法连接到 sqlite

Symfony2 在 Twig 中获取用户角色

如何在 Symfony2 中创建和注册新角色

在 Symfony2 中使用 GenemuFormBundle

CSRF 和 RESTful API (symfony2, php)

Symfony2 禁用选择