【问题标题】:Symfony2 - CalendarBundle - How to fetch user informations from database to render on calendarSymfony2 - 日历包 - 如何从数据库中获取用户信息以在日历上呈现
【发布时间】:2017-04-14 13:04:40
【问题描述】:

所以,我是 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()(呈现事件的标题)时(我什至尝试仅传递来自实体),日历中不会显示任何其他内容而不会出现任何错误。

任何解决此问题的提示都将不受欢迎:)!

【问题讨论】:

  • 你想通过将用户名传递给 EventEntity 来实现什么?
  • 我想呈现创建事件的人的用户名而不是事件的标题。我创建了 getUser() 函数来传递 user_id 并用它替换了 getEventName() ,但没有任何反应,事件也不再呈现。
  • companyEvents 和 User 之间有关系吗?因此,您可以更改 getEventName() 以返回用户名,如下所示 public function getEventName() { return $this->user->getName(); }
  • 我添加了一个答案,其中包含有关您可以做什么的更多详细信息。

标签: php sql symfony calendar


【解决方案1】:

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

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

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

记得从companyEventsentity 中删除eventName 属性和它的setter 方法。您还应该以单数形式命名实体 CamelCased,例如 CompanyEvent

【讨论】:

  • 像魅力一样工作!对这些功能不是很了解。非常感谢您的支持:)
  • 编辑:我尝试使用 getName() 而不是 getUsername();获得用户的全名并且工作出色。再次非常感谢!
猜你喜欢
  • 2016-08-15
  • 1970-01-01
  • 2012-03-07
  • 2014-04-30
  • 2021-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多