【问题标题】:Symfony2 ContainerAware fails to get elementsSymfony2 ContainerAware 无法获取元素
【发布时间】:2014-02-18 09:51:33
【问题描述】:

我正在尝试使用 EntityManager 从自定义类中的实体获取数据,但出现此错误

错误:在第 28 行调用非对象上的成员函数 get()

我不知道为什么$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();
    }
}

编辑也尝试通过 services.yml 仅注入教义2

<?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]

但是当我从实体调用这个类时,我收到了这个错误

可捕获的致命错误:参数 2 传递给 WhiteBear\UsersBundle\Security\UserDependentRole::__construct() 必须 是 Doctrine\ORM\EntityManager 的一个实例,没有给出

那是因为我从我的实体中这样做,因为我不知道如何让 EntityManager 解析到构造函数中......

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

【问题讨论】:

    标签: php class symfony service


    【解决方案1】:

    您必须通过setter provided by the ContainerAware class 注入容器。

    这是您可以通过 DIC 管理这种注入的方法,

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

    但是,

    由于您只是针对实体管理器,您不需要让您的类具有 Container Aware 意识。只有当您的服务依赖于一组其他服务时才应该注入容器(这里不是这种情况)

    那么,请考虑只注入doctrine.orm.entity_manager 服务。 Check this relevant Example.

    【讨论】:

    • 在我的 services.yml 上这样做什么也没做,得到同样的错误:(
    • 好的,检查更新,请考虑我回复的“但是”部分。
    • 是的,这是我的第一次尝试,但我失败了,请参阅我关于出错的更新,这只是一件愚蠢的事情,但我不知道如何将 EntityManager 设置为类调用者的构造函数(在这种情况下,一个实体)
    • 您应该重新考虑您的实施,因为您的实体不应该知道(或依赖)任何实体管理器以保持关注点的清晰分离。
    • 重点是我想得到这个stackoverflow.com/questions/21836340/…,这是我认为我能得到我需要的东西的唯一方法......
    猜你喜欢
    • 1970-01-01
    • 2019-07-06
    • 2016-08-28
    • 2019-11-28
    • 2020-05-30
    • 2021-04-05
    • 2016-08-20
    • 2021-07-04
    • 2019-11-18
    相关资源
    最近更新 更多