Php 将参数传递给实体

Php 将参数传递给实体,php,symfony,Php,Symfony,我是新来的。我希望能够为我的应用程序配置管理员角色名称。我需要这样做:(在控制器中) 在用户实体中,我可以将isAdmin定义为: function isAdmin() { $this->hasRole('ROLE_ADMIN'); } 但这样,就无法配置角色\ u ADMIN。请注意,我不想将“角色名称”作为参数(或默认参数)传递给isAdmin函数。我希望它就像我可以将对象传递给用户实体一样: public function __construct(AuthConfigurat

我是新来的。我希望能够为我的应用程序配置管理员角色名称。我需要这样做:(在控制器中)

在用户实体中,我可以将
isAdmin
定义为:

function isAdmin()
{
   $this->hasRole('ROLE_ADMIN');
}
但这样,就无法配置角色\ u ADMIN。请注意,我不想将“角色名称”作为参数(或默认参数)传递给
isAdmin
函数。我希望它就像我可以将对象传递给用户实体一样:

public function __construct(AuthConfiguration $config)
{
   $this->config = $config;
}

public function isAdmin()
{
   return $this->hasRole($this->config->getAdminRoleName());
}

但由于用户创建由存储库处理,如何将对象传递给用户实体?

您可以使用此捆绑包为角色设置自定义原则DBAL枚举类型:

将用户的角色字段配置为枚举角色类型:

use Fresh\Bundle\DoctrineEnumBundle\Validator\Constraints as DoctrineAssert;

...

/**
 * @DoctrineAssert\Enum(entity="AppBundle\DBAL\Types\RoleType")
 * @ORM\Column(name="role", type="RoleType")
 */
protected $role = RoleType::ROLE_USER;
并在您的实体或存储库或其他任何地方以这种方式使用它:

use AppBundle\DBAL\Types\RoleType;

...

public function isAdmin()
{
   $this->hasRole(RoleType::ROLE_ADMIN);
}

只有在使用关键字
new
创建对象的新实例时,才会调用构造函数。条令不调用构造函数,即使它是实体

您可以创建并调用实体的构造函数,但是我还没有尝试过这个解决方案。它可能无法维护

我想提供一个我喜欢的替代方案(你可能不喜欢)

在我的所有项目中,架构如下所示:
控制器服务存储库实体

这种体系结构的优点是使用了

为您效劳

services:
    my.user:
        class: Acme\HelloBundle\Service\MyUserService
        arguments:
            # First argument
            # You could also define another service that returns
            # a list of roles.
            0:
                admin: ROLE_ADMIN
                user: ROLE_USER
为您服务:

namespace Acme\HelloBundle\Service;

use Symfony\Component\Security\Core\User\UserInterface;

class MyUserService {

    protected $roles = array();

    public function __constructor($roles)
    {
        $this->roles = $roles;
    }

    public function isAdmin(UserInterface $user = null)
    {
        if ($user === null) {
            // return current logged-in user
        }

        return $user->hasRole($this->roles['admin']);
    }
}
在控制器中:

// Pass a user
$this->get('my.user')->isAdmin($this->getUser());

// Use current logged-in user
$this->get('my.user')->isAdmin();
它与您正在寻找的解决方案相去甚远,但在我看来,它似乎更符合Symfony2提供的功能

另一个优点是可以扩展管理员的定义。
例如,在我的项目中,我的用户服务具有额外的逻辑。

听起来像是不必要的复杂性。您已经连接到一个显式定义了术语admin的接口,因为您希望能够动态更改admin角色的名称?我个人从未见过有必要这样做。
services:
    my.user:
        class: Acme\HelloBundle\Service\MyUserService
        arguments:
            # First argument
            # You could also define another service that returns
            # a list of roles.
            0:
                admin: ROLE_ADMIN
                user: ROLE_USER
namespace Acme\HelloBundle\Service;

use Symfony\Component\Security\Core\User\UserInterface;

class MyUserService {

    protected $roles = array();

    public function __constructor($roles)
    {
        $this->roles = $roles;
    }

    public function isAdmin(UserInterface $user = null)
    {
        if ($user === null) {
            // return current logged-in user
        }

        return $user->hasRole($this->roles['admin']);
    }
}
// Pass a user
$this->get('my.user')->isAdmin($this->getUser());

// Use current logged-in user
$this->get('my.user')->isAdmin();