Symfony 2.0 在实体内部获取服务 [英] Symfony 2.0 getting service inside entity

查看:36
本文介绍了Symfony 2.0 在实体内部获取服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在搜索,找不到答案.我的应用程序中有数据库角色模型.用户可以有一个角色,但这个角色必须存储到数据库中.

但是用户需要从数据库中添加默认角色.所以我创建了一个服务:

em = $em;}公共函数 findAll(){返回 $this->em->getRepository(self::ENTITY_NAME)->findAll();}公共函数创建(用户 $user){//可能在这里验证$this->em->persist($user);$this->em->flush($user);}公共函数 addRole($name, $role) {如果 (($newrole = findRoleByRole($role)) != null)返回 $newrole;if (($newrole = findRoleByName($name)) != null)返回 $newrole;//没有现有角色$newrole = new Role();$newrole->setName($name);$newrole->setRole($role);$em->persist($newrole);$em->flush();返回 $newrole;}公共函数 getRoleByName($name) {return $this->em->getRepository(self::ENTITY_NAME)->findBy(array('name' => $name));}公共函数 getRoleByRole($role) {return $this->em->getRepository(self::ENTITY_NAME)->findBy(array('role' => $role));}}

我的 services.yml 是:

alef.role_service:类:AlefUserBundleServiceRoleService参数:[%doctrine.orm.entity_manager%]

现在我想在两个地方使用它:UserControllerUser 实体.我怎样才能让它们进入实体?至于控制器,我想我只需要:

$this->get('alef.role_service');

但是如何在实体内部获取服务?

解决方案

你没有.这是一个很常见的问题.实体应该只知道其他实体,而不是实体管理器或其他高级服务.过渡到这种开发方式可能有点挑战,但通常是值得的.

你要做的是在加载用户的时候加载角色.通常,您最终会得到一个执行此类操作的 UserProvider.您是否通读了有关安全性的部分?那应该是您的起点:

http://symfony.com/doc/current/book/security.html

Im seraching over and cannot find answer. I have database role model in my application. User can have a role but this role must be stored into database.

But then user needs to have default role added from database. So i created a service:

<?php

namespace AlefUserBundleService;

use AlefUserBundleEntityRole;

/**
 * Description of RoleService
 *
 * @author oracle
 */
class RoleService {

    const ENTITY_NAME = 'AlefUserBundle:Role';

    private $em;

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

    public function findAll()
    {
        return $this->em->getRepository(self::ENTITY_NAME)->findAll();
    }

    public function create(User $user)
    {
        // possibly validation here

        $this->em->persist($user);
        $this->em->flush($user);
    }

    public function addRole($name, $role) {
        if (($newrole = findRoleByRole($role)) != null)
            return $newrole;
        if (($newrole = findRoleByName($name)) != null)
            return $newrole;

        //there is no existing role
        $newrole = new Role();
        $newrole->setName($name);
        $newrole->setRole($role);

        $em->persist($newrole);
        $em->flush();

        return $newrole;
    }

    public function getRoleByName($name) {
        return $this->em->getRepository(self::ENTITY_NAME)->findBy(array('name' => $name));
    }

    public function getRoleByRole($role) {
        return $this->em->getRepository(self::ENTITY_NAME)->findBy(array('role' => $role));
    }

}

my services.yml is:

alef.role_service:
    class: AlefUserBundleServiceRoleService
    arguments: [%doctrine.orm.entity_manager%]

And now I want to use it in two places: UserController and User entity. How can i get them inside entity? As for controller i think i just need to:

$this->get('alef.role_service');

But how to get service inside entity?

解决方案

You don't. This is a very common question. Entities should only know about other entities and not about the entity manager or other high level services. It can be a bit of a challenge to make the transition to this way of developing but it's usually worth it.

What you want to do is to load the role when you load the user. Typically you will end up with a UserProvider which does this sort of thing. Have you read through the sections on security? That should be your starting point:

http://symfony.com/doc/current/book/security.html

这篇关于Symfony 2.0 在实体内部获取服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆