Symfony 3.4 Controler中的EntityMerger注入

时间:2018-03-03 23:25:11

标签: php symfony

我一直在使用symfony 3.4创建Web应用程序,我想在控制器中使用PATCH方法内的EntityMerger。

当我在控制器的构造函数中注入EntityMerger时,它在函数patchMovieAction中取值为null。

namespace AppBundle\Entity;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\ORM\Mapping\Id;
class EntityMerger
{
    private $annotationReader;
    public function __constructor(AnnotationReader $annotationReader)
    {
        $this->annotationReader = $annotationReader;
    }
    public function merge($entity, $changes): void
    {
     ....
    }
}
控制器:

class MoviesController extends AbstractController
{
    use ControllerTrait;  
    private $entityMerger;
    public function __constructor(EntityMerger $entityMerger)
    {
        $this->entityMerger = $entityMerger;
    }

    public function patchMovieAction(Movie $movie, Movie $modifiedMovie, ConstraintViolationListInterface $validationErrors)
    {
    if(null ===$movie) {
        return $this->view(null,404);
    }
    if(count($validationErrors)>0){
        throw new ValidationException($validationErrors);
    }
    //Merge entities
    $this->entityMerger->merge($movie,$modifiedMovie);
    //Persist
    $em = $this->getDoctrine()->getManager();
    $em->persist($movie);
    $em->flush();

    //Return
    return $movie;
    }

当我尝试运行php bin/console debug:router时,我收到错误

 Cannot autowire service "AppBundle\Controller\MoviesController": argument "$entityMerger" of method "__construct()" references class "AppBundle\Entity\EntityMerger" but  
   no such service exists. 

0 个答案:

没有答案