Php Symfony 4-自动接线不工作

Php Symfony 4-自动接线不工作,php,symfony,Php,Symfony,我有一个将值写入特定实体的表单,我试图通过控制器中的函数编辑信息,非常简单 BancoController.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\FormF

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

BancoController.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()]
        )
        );
    }
}
myservice.yaml都是默认的,所以我想它应该会自动工作。 实体不显示在bin/console debug:container中

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

App\Entity\Banco:
    autowire: true
    autoconfigure: true
    public: false
它可以工作,但现在当我访问/banco/{id}时,表单将为空,没有数据库中存在的信息,如果我键入某个内容并提交它,则数据库中不会发生任何更改

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

顺便说一句,这个实体表有一个FK用户id。也许这就是问题所在? 我迷路了,所以我需要一些帮助。我对PHP/Symfony不熟悉。
谢谢

符号将自动关联实体,但它将是空的。更好的方法是自动连接存储库,从请求中获取ID,并根据其ID从存储库中获取银行。

嗯,由于未知原因,
sensio/framework额外捆绑包
未完全安装

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

该死,花了两天时间来修复这个


谢谢大家

SensioFrameworkExtraBundle的路由注释从5.2版开始就被弃用,因为它们现在是Symfony的核心功能

您可以尝试这些方法进行清理(尝试前备份composer.json)

更新composer.json “要求”:{ “symfony/symfony”:“^4.3”, }, 运行这些命令

composer update symfony/symfony

composer update "symfony/*" --with-all-dependencies

您的控制器需要扩展AbstractController才能自动工作,如果我没记错的话,您是否尝试重新安排参数?将请求放在第一位,然后将实体作为第二个参数。我认为,如果一切正常,ParamConverter应该能够通过ID获取实体。而且,仅作为信息,错误消息有点误导。这实际上与autowire无关。问题是,如果没有正确安装额外的捆绑包,该过程将无法工作,因此没有$banco.Nice。我和你一样犯了同样的错误。谢谢你的帮助
composer update symfony/symfony

composer update "symfony/*" --with-all-dependencies