php - Symfony 4 - Autowiring 不工作

标签 php symfony

我有一个将值写入特定实体的表单,我试图通过 Controller 中的函数编辑信息,非常简单。

银行 Controller .php

<?php

namespace App\Controller;

use App\Entity\Banco;
use App\Form\BancoType;
use App\Repository\BancoRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;

class BancoController{

    private $twig;

    private $bancoRepository;

    private $formFactory;

    private $entityManager;

    private $router;

    private $flashBag;

    private $authorizationChecker;

    public function __construct(
        \Twig_Environment $twig, 
        BancoRepository $bancoRepository, 
        FormFactoryInterface $formFactory, 
        EntityManagerInterface $entityManager, 
        RouterInterface $router,
        FlashBagInterface $flashBag,
        AuthorizationCheckerInterface $authorizationChecker
    ){
        $this->twig = $twig;
        $this->bancoRepository = $bancoRepository;
        $this->formFactory = $formFactory;
        $this->entityManager = $entityManager;
        $this->router = $router;
        $this->flashBag = $flashBag;
        $this->authorizationChecker = $authorizationChecker;
    }

    /**
    * @Route("/cadastro/bancos", name="cadastro_banco")
    */
    public function index(TokenStorageInterface $tokenStorage){

        $usuario = $tokenStorage->getToken()->getUser();

        $html = $this->twig->render('bancos/index.html.twig', [
            'bancos' => $this->bancoRepository->findBy(array('usuario' => $usuario))
        ]);

        return new Response($html);
    }

    /**
    * @Route("/banco/{id}", name="editar")
    */
    public function editarBanco(Banco $banco, Request $request) {

        $form = $this->formFactory->create(
            BancoType::class,
            $banco
        );
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()){
            $this->entityManager->flush();

            return new RedirectResponse($this->router->generate('cadastro_banco'));
        }

        return new Response(
            $this->twig->render(
                'bancos/cadastro.html.twig',
                ['form' => $form->createView()]
            ));
    }

    /**
    * @Route("/cadastro/cadastrar-banco", name="cadastro_banco-nova")
    */
    public function cadastrar(Request $request, TokenStorageInterface $tokenStorage){

        $usuario = $tokenStorage->getToken()->getUser();

        $banco = new Banco();
        $banco->setUsuario($usuario);

        $form = $this->formFactory->create(BancoType::class, $banco);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()){
            $this->entityManager->persist($banco);
            $this->entityManager->flush();

            return new RedirectResponse($this->router->generate('cadastro_banco'));
        }

        return new Response(
            $this->twig->render(
                'bancos/cadastro.html.twig',
                ['form' => $form->createView()]
        )
        );
    }
}

问题是,当我访问/banco/{id} 时,出现错误:

"Cannot autowire argument $banco of "App\Controller\BancoController::editarBanco()": it references class "App\Entity\Banco" but no such service exists."

我的 service.yaml 都是默认的,所以我想它应该会自动运行。 实体未显示在 bin/console debug:container 中。

如果我像这样在 services.yaml 中手动声明实体

App\Entity\Banco:
    autowire: true
    autoconfigure: true
    public: false

它可以工作,但现在当我访问/banco/{id} 时,表单变成空的,没有数据库中存在的信息,如果我输入一些内容并提交,数据库中没有任何变化。

如果我转到调试工具栏并检查查询,它似乎是在查询登录用户的 ID,而不是实体“Banco”的 ID。

顺便说一句,这个实体表有一个 FK user_id。也许这就是问题所在? 我迷路了,所以我需要一点帮助。我是 PHP/Symfony 的新手。 谢谢

最佳答案

嗯,由于某些未知原因 sensio/framework-extra-bundle 没有完全安装。

我刚刚运行了 composer require annotations,现在一切正常。

该死,浪费了 2 天的时间试图解决这个问题..

谢谢大家!

关于php - Symfony 4 - Autowiring 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51269736/

相关文章:

php - 如何在 MySQL 中生成唯一 ID?

php - Shopify 本地安装

php - 延迟提交表单 5 秒并显示 jquery 对话框?

ajax - 如何在 Sonata Admin 表单中使用 Ajax?

php - 使用注释的安全方法

php - 使用 Native SQL 的 Doctrine 查询总是返回空数组

php - 如何创建强大的 api key

php - 如何通过 php 将这些 Jquery 变量发送到 Mysql?

symfony - 从 symfony 2.0 升级到 2.3

php - 我怎样才能写用户名登录Symfony2