Я пытаюсь использовать EntityManager, чтобы получить данные от объекта внутри моего пользовательского класса, но я получаю эту ошибкуSymfony2 ContainerAware не удается получить элементы
Error: Call to a member function get() on a non-object on line 28
Я не знаю, почему $this->container
не имеет дочерние элементы, я простирающийся ContainerAware
...
это мой код
<?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 Также попытался с только инъекционные doctrine2 через 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]
Но, как я называю этот класс от сущности, я получаю эту ошибку
Catchable Fatal Error: Argument 2 passed to WhiteBear\UsersBundle\Security\UserDependentRole::__construct() must be an instance of Doctrine\ORM\EntityManager, none given
Это потому что из моей сущности я делаю это, потому что я не знаю, как заставить EntityManager анализировать конструктор ...
/**
* @inheritDoc
*/
public function getRoles() {
return array(new UserDependentRole($this));
}
Выполнение этого на моем service.yml ничего не делает, получилась та же ошибка :( –
Хорошо, проверьте обновление и, пожалуйста, рассмотрите раздел «НО» моего ответа. –
Да, это была моя первая попытка, но я не смог увидеть мое обновление о том, что пошло не так, просто глупо, но я не знаю, как установить EntityManager для конструктора из вызывающего класса (в данном случае, Entity). –