类型错误:传递给AppBundle \ EventListener的参数1:CalendarEventListener :: __ construct()必须是Doctrine \ ORM \ EntityManager的实例

时间:2017-04-10 13:31:32

标签: php symfony orm doctrine

所以我是Symfony的新手,我一直在尝试使用https://github.com/adesigns/calendar-bundle创建一个应用。

我已经解析了创建CalendarEventListener的所有步骤但是我遇到了错误:

  

CalendarEventListener.php中的FatalThrowableError第13行:输入错误:传递给AppBundle \ EventListener的参数1:CalendarEventListener :: __ construct()必须是Doctrine \ ORM \ EntityManager的实例,Symfony \ Bundle \ TwigBundle \ TwigEngine的实例给出,在1825行的/home/intern/Desktop/newapp/var/cache/dev/appDevDebugProjectContainer.php中调用

我试图通过研究类似的问题来解决这个问题,但我没想出来。

我的EventCalendarListener:

<?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.

    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);
    }
}
}

也是我的services.yml

parameters:
#    parameter_name: value
services:
  app.form_login_authenticator:
      class: AppBundle\Security\FormLoginAuthenticator
      arguments: ["@router", "@security.password_encoder"]
  kernel.listener.allotaxi_exception_listener:
      class: AppBundle\EventListener\CalendarEventListener
      arguments: ["@templating", "@kernel",]
    tags:
    - { name: kernel.event_listener, event: kernel.exception, method: onKernelException }

的services.xml

<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services  http://symfony.com/schema/dic/services/services-1.0.xsd">

<parameters>
    <parameter key="fullcalendar.event.class">ADesigns\CalendarBundle\Entity\EventEntity</parameter>
    <parameter key="fullcalendar.loader.event">calendar.load_events</parameter>
</parameters>

</container>
在config.yml中

导入所有服务

任何帮助将不胜感激:)

2 个答案:

答案 0 :(得分:0)

您正在向事件侦听器发送2个参数,但在构造中您需要实体管理器。您应该在参数下添加实体管理器,如下所示:

kernel.listener.allotaxi_exception_listener:
      class: AppBundle\EventListener\CalendarEventListener
      arguments: ["@doctrine.orm.entity_manager"]
    tags:
    - { name: kernel.event_listener, event: kernel.exception, method: onKernelException }

答案 1 :(得分:0)

感谢您的回复。确实存在通过services.yml传递的参数的问题,此外我必须为orm实体管理器传递一些日历事件标记,如下面的代码所示。

kernel.listener.allotaxi_exception_listener:
    class: AppBundle\EventListener\CalendarEventListener
    arguments: ["@doctrine.orm.entity_manager"]
    tags:
        - { name: kernel.event_listener, event: calendar.load_events, method: loadEvents }