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

[SF4] Container is null into Builder.php #388

Closed
mysterty opened this issue Apr 9, 2018 · 5 comments
Closed

[SF4] Container is null into Builder.php #388

mysterty opened this issue Apr 9, 2018 · 5 comments

Comments

@mysterty
Copy link

mysterty commented Apr 9, 2018

Hello,

I installed knp_menu_bundle as described in the doc.

I use it as service as it's needed in SF4.

I use ContainerAwareTrait;

But if i try to call container as described in the doc for geting entityManager, i get :

$em = $this->container->get('doctrine')->getManager();
// returns : Call to a member function get() on null

Did i forgot something or is ContainerAwareTrait incompatible with SF4 ?

@stof
Copy link
Collaborator

stof commented Apr 10, 2018

Are you implementing the ContainerAwareInterface ? Using the trait is not what is detected by the bundle (as traits don't exist at runtime). The trait is handy to implement the needed method of the interface for you, but you still need to mark your class as implementing the interface.

@mysterty
Copy link
Author

@stof Yes, i'm using it exactly as the doc says :

use Knp\Menu\FactoryInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;

class Builder implements ContainerAwareInterface
{
    use ContainerAwareTrait;
    // ...
}

Note that for quick fixing it, i just pass container interface to the service, but it still not according the documentation :

app.menu_builder:
        class: App\Menu\Builder
        arguments: ["@knp_menu.factory", "@service_container"]

@stof
Copy link
Collaborator

stof commented Apr 10, 2018

hmm, for builders defined as a service, the bundle does not implement any special support for ContainerAwareInterface (all the instantiation is handled by the service definition).

And in your case, you are trying to inject the container in the constructor, not through setContainer

@mysterty
Copy link
Author

In fact, we can only use the bundle through a service in V4.
So, maybe the bundle should be update for V4 or the doc should be updated ?

PS : I don't know the "setContainer" method.

@stof
Copy link
Collaborator

stof commented Sep 16, 2018

well, the trait you are using is precisely here to define a setContainer method. But for builders defined as services, it is much better to inject the dependencies you need only.

Regarding updating the doc, I'm changing the main way to define menus in #392, moving the convention-based one to a separate chapter, as it is indeed not compatible with Flex.

@garak garak closed this as completed Dec 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants