所以,我是Symfony的新手,我正在尝试创建一个基于功能日历的应用程序,其中包含使用calendar-bundle从数据库呈现的事件。
通过文档,我能够在用户和日历上呈现的事件之间建立关系,但我无法传递特定数据,更确切地说是用户名。
下面是EventEntity,它负责日历活动的详细信息。
<?php
namespace ADesigns\CalendarBundle\Entity;
/**
* 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-d\TH: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-d\TH: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 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->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 AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Entity
* @ORM\Table(name="companyEvents")
*/
class companyEvents
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* events are created by users
* @ORM\ManyToOne(targetEntity="User", inversedBy="events")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $user;
/**
* @ORM\Column(name="event_name")
*/
private $eventName;
/**
* @ORM\Column(name="event_date", type="datetime")
*/
private $eventDate;
/**
* @ORM\Column(name="startEventDate", type="datetime")
*/
private $startEventDate;
/**
* @ORM\Column(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()(WHICH RENDERS THE TITLE OF EVENT)时(我试图甚至只传递来自的user_id)实体),日历中没有显示任何其他内容而没有转储任何错误。
任何解决这个问题的提示都很难被人理解:)!
答案 0 :(得分:0)
如果要在事件标题中呈现用户名,可以更改getEventName
方法以返回用户名:
public function getEventName()
{
return $this->user->getUsername();
}
这样您就不必更改CalendarEventListener
代码。
请记住从eventName
实体中删除companyEvents
属性及其setter方法。您还应该将实体命名为CamelCased,并以单数形式命名,如CompanyEvent
。