Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Symfony 4.4事件侦听器错误(MongoDB)_Symfony_Events_Doctrine_Event Listener - Fatal编程技术网

Symfony 4.4事件侦听器错误(MongoDB)

Symfony 4.4事件侦听器错误(MongoDB),symfony,events,doctrine,event-listener,Symfony,Events,Doctrine,Event Listener,我正在尝试创建一个侦听器,以便在创建新的分级时使用。我遵循了所有的文档,但我一直得到相同的错误: 传递给“Symfony\Component\EventDispatcher\EventDispatcherInterface::dispatch()”的参数1必须是给定的“Symfony\Component\EventDispatcher\Event”、“App\Event\AverageEvent”的实例 我试图在事件中使用Symfony\Component\EventDispatcher\Eve

我正在尝试创建一个侦听器,以便在创建新的分级时使用。我遵循了所有的文档,但我一直得到相同的错误: 传递给“Symfony\Component\EventDispatcher\EventDispatcherInterface::dispatch()”的参数1必须是给定的“Symfony\Component\EventDispatcher\Event”、“App\Event\AverageEvent”的实例

我试图在事件中使用Symfony\Component\EventDispatcher\Event,但它一直说它已被弃用,并且根据文档使用Symfony\Contracts\EventDispatcher\Event

我在服务中注册我的事件,下面是我的事件、eventlistener和类

等级评定

class RatingApiController extends AbstractController
{
        
    /**
     * @Route("api/rating/create", name="CreateRating",  methods={"POST"})
     * @param DocumentManager $dm
     * @param Request $request
     * @param EventDispatcher $eventDispatcher
     * @return RedirectResponse|Response
     * @throws MongoDBException
     * 
     */
    public function addRating(Request $request, EventDispatcherInterface $eventDispatcher)
    {
        $response = [];

        $form = $this->
        createForm(RatingType::class, new Rating() ,array('csrf_protection' => false));  
        
        $request = json_decode($request->getContent(), true);
        
        
        $form->submit($request);

        if($form->isSubmitted() && $form->isValid())
        {
        
           $rating = $form->getData();

           $this->documentManager->persist($rating);
           $this->documentManager->flush();
           
           $averageRatingEvent = new AverageRatingEvent($rating);
           $eventDispatcher->dispatch( AverageRatingEvent::NAME, $averageRatingEvent);
            

           $status = 200;
           $response = ["status" => $status, "success" => true, "data" => $rating->getId()];
           // return $this->redirectToRoute('rating_list'); 
       }



}
事件

<?php
namespace App\Event;

use App\Document\Rating;
use Symfony\Contracts\EventDispatcher\Event;

class AverageRatingEvent extends Event
{
    /**
     * @var Rating $rating
     */
    protected $rating;

    public const NAME = "average.rating";
    
    public function __construct(Rating $rating)
    {
        $this->rating = $rating;        
    }

    public function getRating()
    {
        return $this->rating;
    }
    
}

在AverageRatingEvent中扩展事件

用途需要从

use Symfony\Contracts\EventDispatcher\Event;


条令事件和Symfony事件是两件不同的事情,您不需要分派sf事件来使用条令事件。顺便说一下,您的
dispatch
参数是反向的:
dispatch($averageRatingEvent,averageRatingEvent::NAME)
use Symfony\Contracts\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\Event;