Symfony2 - CalendarBundle - 如何从数据库中获取用户信息以呈现在日历上



所以,我是Symfony的新手,我正在尝试创建一个基于日历的功能应用程序,使用日历包从数据库呈现的事件。

通过文档,我能够在用户和日历上呈现的事件之间建立关系,但我无法传递特定数据,更准确地说是用户名。

下面显示了负责日历事件详细信息的事件实体。

<?php
namespace ADesignsCalendarBundleEntity;
/**
* Class for holding a calendar event's details.
*
* @author Mike Yudin <mikeyudin@gmail.com>
*/
class EventEntity
{
/**
* @var mixed Unique identifier of this event (optional).
*/
protected $id;
/**
* @var string Title/label of the calendar event.
*/
protected $title;
/**
* @var string URL Relative to current path.
*/
protected $url;
/**
* @var string HTML color code for the bg color of the event label.
*/
protected $bgColor;
/**
* @var string HTML color code for the foregorund color of the event label.
*/
protected $fgColor;
/**
* @var string css class for the event label
*/
protected $cssClass;
/**
* @var DateTime DateTime object of the event start date/time.
*/
protected $startDatetime;
/**
* @var DateTime DateTime object of the event end date/time.
*/
protected $endDatetime;
/**
* @var boolean Is this an all day event?
*/
protected $allDay = false;
/**
* @var array Non-standard fields
*/
protected $otherFields = array();
public function __construct($title, DateTime $startDatetime, DateTime $endDatetime = null, $allDay = false)
{
$this->title = $title;
$this->startDatetime = $startDatetime;
$this->setAllDay($allDay);
if ($endDatetime === null && $this->allDay === false) {
throw new InvalidArgumentException("Must specify an event End DateTime if not an all day event.");
}
$this->endDatetime = $endDatetime;
}
/**
* Convert calendar event details to an array
*
* @return array $event
*/
public function toArray()
{
$event = array();
if ($this->id !== null) {
$event['id'] = $this->id;
}
$event['title'] = $this->title;
$event['start'] = $this->startDatetime->format("Y-m-dTH:i:sP");
if ($this->url !== null) {
$event['url'] = $this->url;
}
if ($this->bgColor !== null) {
$event['backgroundColor'] = $this->bgColor;
$event['borderColor'] = $this->bgColor;
}
if ($this->fgColor !== null) {
$event['textColor'] = $this->fgColor;
}
if ($this->cssClass !== null) {
$event['className'] = $this->cssClass;
}
if ($this->endDatetime !== null) {
$event['end'] = $this->endDatetime->format("Y-m-dTH:i:sP");
}
$event['allDay'] = $this->allDay;
foreach ($this->otherFields as $field => $value) {
$event[$field] = $value;
}
return $event;
}
public function setId($id)
{
$this->id = $id;
}
public function getId()
{
return $this->id;
}
public function setTitle($title)
{
$this->title = $title;
}
public function getTitle()
{
return $this->title;
}
public function setUrl($url)
{
$this->url = $url;
}
public function getUrl()
{
return $this->url;
}
public function setBgColor($color)
{
$this->bgColor = $color;
}
public function getBgColor()
{
return $this->bgColor;
}
public function setFgColor($color)
{
$this->fgColor = $color;
}
public function getFgColor()
{
return $this->fgColor;
}
public function setCssClass($class)
{
$this->cssClass = $class;
}
public function getCssClass()
{
return $this->cssClass;
}
public function setStartDatetime(DateTime $start)
{
$this->startDatetime = $start;
}
public function getStartDatetime()
{
return $this->startDatetime;
}
public function setEndDatetime(DateTime $end)
{
$this->endDatetime = $end;
}
public function getEndDatetime()
{
return $this->endDatetime;
}
public function setAllDay($allDay = false)
{
$this->allDay = (boolean) $allDay;
}
public function getAllDay()
{
return $this->allDay;
}
/**
* @param string $name
* @param string $value
*/
public function addField($name, $value)
{
$this->otherFields[$name] = $value;
}
/**
* @param string $name
*/
public function removeField($name)
{
if (!array_key_exists($name, $this->otherFields)) {
return;
}
unset($this->otherFields[$name]);
}
}

除此之外,我还有 CalendarEventListener.php它负责发送以在日历上呈现的数据:

<?php
namespace AppBundleEventListener;
use ADesignsCalendarBundleEventCalendarEvent;
use ADesignsCalendarBundleEntityEventEntity;
use DoctrineORMEntityManager;
class CalendarEventListener
{
private $entityManager;
public function __construct(EntityManager $entityManager)
{
$this->entityManager = $entityManager;
}
public function loadEvents(CalendarEvent $calendarEvent)
{
$startDate = $calendarEvent->getStartEventDate();
$endDate = $calendarEvent->getEndEventDate();
// 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:companyEvents')
->createQueryBuilder('companyEvents')
->where('companyEvents.startEventDate BETWEEN :startEventDate and :endEventDate')
->setParameter('startEventDate', $startDate->format('Y-m-d H:i:s'))
->setParameter('endEventDate', $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) {

$eventEntity = new EventEntity($companyEvent->getEventName(),
//   
   $companyEvent->getStartEventDate(),
   $companyEvent->getEndEventDate()
   ,null, true);
//optional calendar event settings
$eventEntity->setAllDay(true); // default is false, set to true if this is an all day event
$eventEntity->setBgColor('#3366ff'); //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);
}
}
}

以及用于构建事件的 companyEvents 实体:

<?php
namespace AppBundleEntity;
use DoctrineORMMapping as ORM;
use SymfonyComponentSecurityCoreUserUserInterface;
use DoctrineCommonCollectionsArrayCollection;
/**
* @ORMEntity
* @ORMTable(name="companyEvents")
*/
class companyEvents
{
/**
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* events are created by users
* @ORMManyToOne(targetEntity="User", inversedBy="events")
* @ORMJoinColumn(name="user_id", referencedColumnName="id")
*/
private $user;
/**
* @ORMColumn(name="event_name")
*/
private $eventName;
/**
* @ORMColumn(name="event_date", type="datetime")
*/
private $eventDate;
/**
* @ORMColumn(name="startEventDate", type="datetime")
*/
private $startEventDate;
/**
* @ORMColumn(name="endEventDate", type="datetime")
*/
private $endEventDate;
/**
* Set eventName
*
* @param string $eventName
*
* @return companyEvents
*/
public function setEventName($eventName)
{
$this->eventName = $eventName;
return $this;
}
/**
* Get eventName
*
* @return string
*/
public function getEventName()
{
return $this->eventName;
}
/**
* Set eventDate
*
* @param string $eventDate
*
* @return CompanyEvents
*/
public function setEventDate($eventDate)
{
$this->eventDate = $eventDate;
return $this;
}
/**
* Get eventDate
*
* @return string
*/
public function getEventDate()
{
return $this->eventDate;
}
/**
* Set start event date
* @param string $startEventDate
*
* @return companyEvents
*/
public function setStartEventDate($startEventDate)
{
$this->startEventDate = $startEventDate;
return $this;
}
/**
*Get start event date
* @return string
*/
public function getStartEventDate()
{
return $this->startEventDate;
}
/**
* Set start event date
* @param string $endEventDate
*
* @return companyEvents
*/
public function setEndEventDate($endEventDate)
{
$this->endEventDate = $endEventDate;
return $this;
}
/**
*Get start event date
* @return string
*/
public function getEndEventDate()
{
return $this->endEventDate;
}
/**
* set user relationship
* @param string $user_id
*
* @return companyEvents
*/
public function setUser($user)
{
$this->user = $user;
return $this;
}
/**
* @return string
*/
public function getUser()
{
return $this->user;
}

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

}

所以,我的问题是这里传递的不是getEventName:

$eventEntity = new EventEntity($companyEvent->getEventName(),
$companyEvent->getStartEventDate(),
$companyEvent->getEndEventDate()
,null, true);

我想传递属于特定事件的特定用户名,但是当我使用任何其他函数更改 getEventName()(呈现事件的标题)时(我甚至尝试只传递来自实体的user_id),日历中没有显示任何其他内容而不会转储任何错误。

关于解决这个问题的任何提示都很难:)赞赏!

如果要在事件标题中呈现用户名,可以更改getEventName方法以返回用户名:

public function getEventName()
{
return $this->user->getUsername();
}

这样,您就不必更改CalendarEventListener代码。

请记住从companyEvents实体中删除eventName属性及其 setter 方法。您还应该将实体命名为骆驼,并以单数形式命名,就像这个CompanyEvent一样。

最新更新