在 symfony2 中将事件添加到 CalendarBundle
add events to CalendarBundle in symfony2
我按照 link 上的教程安装了 CalendarBundle - jQuery fullcalendar:https://github.com/adesigns/calendar-bundle and I succeeded to display the calendar. But the problem is that I can't add events by clicking on the calendar despite I added an Event Listener class by following this part of the tutorial: https://github.com/adesigns/calendar-bundle#adding-events。另外我不知道如何使用我安装的包处理数据库。
那么,我该怎么做呢??
我从名为 AppBundle 的新包中渲染了日历,并按照说明在文件夹 EventListener 中添加了 CalendarEventListener.php class。下面是 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 中添加了 service.xml。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>
它没有显示错误,但同时也没有添加事件。
提前致谢。
确保在与 ADesigns 相同的包中注册事件加载服务。另一件事是检查您的网络选项卡是否将请求发送到日历控制器以及您得到了什么响应。如果没有相应地更新您的路由文件。
我按照 link 上的教程安装了 CalendarBundle - jQuery fullcalendar:https://github.com/adesigns/calendar-bundle and I succeeded to display the calendar. But the problem is that I can't add events by clicking on the calendar despite I added an Event Listener class by following this part of the tutorial: https://github.com/adesigns/calendar-bundle#adding-events。另外我不知道如何使用我安装的包处理数据库。
那么,我该怎么做呢??
我从名为 AppBundle 的新包中渲染了日历,并按照说明在文件夹 EventListener 中添加了 CalendarEventListener.php class。下面是 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 中添加了 service.xml。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>
它没有显示错误,但同时也没有添加事件。 提前致谢。
确保在与 ADesigns 相同的包中注册事件加载服务。另一件事是检查您的网络选项卡是否将请求发送到日历控制器以及您得到了什么响应。如果没有相应地更新您的路由文件。