Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Can't get EntityManager in Command class #697

Open
sherwomeister opened this issue Apr 26, 2024 · 2 comments
Open

[BUG] Can't get EntityManager in Command class #697

sherwomeister opened this issue Apr 26, 2024 · 2 comments
Assignees
Labels
needs triage Waiting for a valid tag

Comments

@sherwomeister
Copy link

Nutgram version

4.19.0

PHP version

8.3

Framework

Symfony 7

Cache driver

apcu

OS

Ubuntu

Dev environment

Docker

Describe the bug

I get this error if I try to get EM autowired: Too few arguments to function App\Telegram\Command\SomeCommand::__construct(), 0 passed in /var/www/app/vendor/nutgram/nutgram/src/Handlers/Listeners/MessageListeners.php on line 44 and exactly 1 expected

This also does not work:

public function handle(Nutgram $bot, EntityManagerInterface $entityManager): void
{
   // ...
}

Nor this:

    App\Telegram\Command\SomeCommand:
        arguments:
            $entityManager: '@doctrine.orm.entity_manager'

To Reproduce

$bot->registerCommand(SomeCommand::class)->middleware(SomeMiddleware::class);
class SomeCommand extends Command
{

    // ...

    public function __construct(
        private readonly EntityManagerInterface $entityManager,
    )
    {
        parent::__construct();
    }
    
    // ..
}

Expected behavior

How to get EM in command?

Screenshots

No response

Additional context

No response

@Lukasss93 Lukasss93 added the needs triage Waiting for a valid tag label Apr 27, 2024
@sergix44
Copy link
Member

Can you please try instead of using the registerCommand method, trying to register it using the onCommand: https://nutgram.dev/docs/usage/handlers#oncommand

@TePossinoAcciaccatte
Copy link

Hi @sergix44, I'm having the same issue.

I tried what you asked:

MyCommand.php

public function __construct(private readonly ParameterBagInterface $parameterBag)
{
    parent::__construct();
}

telegram.php

$bot->onCommand('something', [MyCommand::class, 'handle']);

This way it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage Waiting for a valid tag
Projects
None yet
Development

No branches or pull requests

4 participants