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

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

问题描述

我可以扫描并找不到答案。
我的应用程序中有数据库角色模型。用户可以有一个角色,但此角色必须存储到数据库中。

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 Alef\UserBundle\Service;

use Alef\UserBundle\Entity\Role;

/**
 * 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));
    }

}

我的 services.yml 是:

alef.role_service:
    class: Alef\UserBundle\Service\RoleService
    arguments: [%doctrine.orm.entity_manager%]

现在我想在两个地方使用它:
UserController 用户实体。我如何让他们进入实体?
至于控制器我想我只需要:

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.

你想做什么是在你加载角色时加载用户。通常你会得到一个UserProvider,它做这样的事情。你是否阅读了有关安全性的部分?这应该是您的出发点:

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天全站免登陆