Robert W. Hunter
Robert W. Hunter

Reputation: 3003

Symfony2 ContainerAware fails to get elements

I'm trying to use EntityManager to get data from an entity inside my custom class, but I'm getting this error

Error: Call to a member function get() on a non-object on line 28

I don't know why $this->container has no child elements, I'm extending ContainerAware...

this is my code

<?php
namespace WhiteBear\UsersBundle\Security;
use Symfony\Component\Security\Core\Role\RoleInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\DependencyInjection\ContainerAware;
use WhiteBear\CustomerPortalBundle\Entity\VtigerContactdetails;
use WhiteBear\CustomerPortalBundle\Entity\VtigerContactscf;
class UserDependentRole extends ContainerAware implements RoleInterface
{
    private $user;

    public function __construct(UserInterface $user)
    {
        $this->user = $user;
    }

    public function getRole()
    {
        $rol = $this->getEntityManager()->getRepository('WhiteBearCustomerPortalBundle:VtigerContactscf')
        ->findBy(array(
                'contactid' => $this->user->getId()
        ));
        $role = $rol['groups'] == '1' ? "AGENT" : "USER";
        return 'ROLE_' . strtoupper($role);
    }

    public function getEntityManager() {
        return $this->container->get('doctrine')->getEntityManager();
    }
}

EDIT Also tried with only injecting doctrine2 via services.yml

<?php
namespace WhiteBear\UsersBundle\Security;
use Symfony\Component\Security\Core\Role\RoleInterface;
use Symfony\Component\Security\Core\User\UserInterface;

use Doctrine\ORM\EntityManager;

use WhiteBear\CustomerPortalBundle\Entity\VtigerContactdetails;
use WhiteBear\CustomerPortalBundle\Entity\VtigerContactscf;
class UserDependentRole implements RoleInterface
{
    private $user;
    private $em;

    public function __construct(UserInterface $user, EntityManager $em)
    {
        $this->user = $user;
        $this->em = $em;
    }

    public function getRole()
    {
        $rol = $this->em->getRepository('WhiteBearCustomerPortalBundle:VtigerContactscf')
        ->findBy(array(
                'contactid' => $this->user->getId()
        ));
        $role = $rol['groups'] == '1' ? "AGENT" : "USER";
        return 'ROLE_' . strtoupper($role);
    }
}

services.yml

services:
    white_bear.userdepend:
        class: WhiteBear\CustomerPortal\Security\UserDependentRole
        arguments: [@doctrine.orm.entity_manager]

But as I'm calling this class from an Entity, I'm getting this error

Catchable Fatal Error: Argument 2 passed to WhiteBear\UsersBundle\Security\UserDependentRole::__construct() must be an instance of Doctrine\ORM\EntityManager, none given

That's because from my Entity I'm doing this, because I don't know how to get EntityManager to parse into constructor...

/**
 * @inheritDoc
 */
public function getRoles() {
    return array(new UserDependentRole($this));
}

Upvotes: 0

Views: 1967

Answers (1)

Ahmed Siouani
Ahmed Siouani

Reputation: 13891

You've to inject the container via the setter provided by the ContainerAware class.

Here's the way you can manage this kind of injection via the DIC,

your_service_id:
    class:  Path_to_your_service_class
    calls:
        - [setContainer, ['@service_container']]

BUT,

As you're just targeting the Entity Manager, you don't need to make your class Container Aware. Injecting the container should be done only when your service rely on a set of other services (which is not the case here)

So then, please consider injecting only the doctrine.orm.entity_manager service. Check this relevant Example.

Upvotes: 4

Related Questions