简体   繁体   English

Doctrine 中间件不刷新

[英]Doctrine middleware do not make flush

My config/messanger.yaml looks like:我的 config/messanger.yaml 看起来像:

framework:
    messenger:
        default_bus: command.bus
        buses:
            command.bus:
                middleware: 'Symfony\Bridge\Doctrine\Messenger\DoctrineTransactionMiddleware'
            query.bus: ~

And the command handler for command UpdateUser looks like this:命令UpdateUser的命令处理程序如下所示:

final class UpdateUserHandler implements CommandHandlerInterface
{
    /**
     * @var Users
     */
    private Users $users;

    public function __construct(Users $users)
    {
        $this->users = $users;
    }

    public function __invoke(UpdateUser $command)
    {
        $user = $command->getUser();
        $user->setName($command->getName());
    }

And now I am dispatching this command from the controller:现在我从 controller 发送这个命令:

final class UserController extends AbstractController
{
    private CommandBus $commandBus;

    public function __construct(CommandBus $commandBus)
    {
        $this->commandBus = $commandBus;
    }

    public function __invoke(Request $request, User $user): Response
    {
        $this->commandBus->dispatch(new UpdateUser($user, $name));

        return $this->render('user.html.twig', []);
    }
}

Could you tell me why automatic flush on middleware DoctrineTransactionMiddleware does not work?你能告诉我为什么中间件DoctrineTransactionMiddleware上的自动刷新不起作用吗? What I am missing?我错过了什么?

final class UpdateUserHandler implements CommandHandlerInterface
{
/**
 * @var Users
 */
private Users $users;

private EntityManagerInterface $em;


public function __construct(Users $users, EntityManagerInterface $em)
{
    $this->users = $users;
    $this->em = $em;
}

public function __invoke(UpdateUser $command)
{
    $user = $command->getUser();
    $user->setName($command->getName());
    $this->em->flush();
}

} }

Better call flush method explicitly.更好地显式调用flush方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM