Symfony,从另一个表中加载角色,来自用户

时间:2015-07-21 09:15:09

标签: php symfony doctrine

好的,我将从我的db布局开始,我的access_levels_id表中有一个Users,我希望根据每个用户加载我的角色。

现在这是我所拥有的,但我没有让它工作,

我的ORM,分析器没有任何问题,

Users Tables :

    manyToOne:
    accesslevel:
        targetEntity: AccessLevels
        inversedBy: user
        joinColumn:
            name: access_levels_id
            referencedColumnName: id

访问表:

    oneToMany:
    user:
        targetEntity: Users
        mappedBy: accesslevel

这个剂量缺乏工作,我做了User

public function BlogAction() {
     $user = $this->getUser();

     var_dump( $user ); //see below to see what this returns

     return $this->render('xxxxxxxx.html.twig');
}

var dump

private 'accesslevel' => 
 object(Proxies\__CG__\xx\Entity\AccessLevels)[236]
  public '__initializer__' => 
    object(Closure)[234]
      public 'static' => 
        array (size=2)
          ...
      public 'this' => 
        object(Doctrine\ORM\Proxy\ProxyFactory)[132]
          ...
      public 'parameter' => 
        array (size=1)
          ...
  public '__cloner__' => 
    object(Closure)[235]
      public 'static' => 
        array (size=2)
          ...
      public 'this' => 
        object(Doctrine\ORM\Proxy\ProxyFactory)[132]
          ...
      public 'parameter' => 
        array (size=1)
          ...
  public '__isInitialized__' => boolean false
  private 'id' (xx\Entity\AccessLevels) => int 3
  private 'level' (xx\Entity\AccessLevels) => null
  private 'leveltitle' (xx\Entity\AccessLevels) => null

那么为什么,鉴于它在AccessLevel上获得了正确的ID,如果我更改它,它会更新,那么为什么levelleveltitle为空?

我还在我的Access Levels实体中尝试了剂量工具RoleInterface,我一直在Users实体中执行此操作,

public function getRoles()
{
    $UserLevel = $this->getAccessLevelsId();
    return array('ROLE_ADMIN', new AccessLevels($UserLevel));
}

在我的Access Level实体中,我这样做,

private $UsersLevel;

public function __construct($UsersLevel) {

    $this->UsersLevel = $UsersLevel;
}

public function getRole()
{
    $UsersRoleID =  $this->UsersLevel;

    return 'ROLE_TEST'.$UsersRoleID; <- this has the ID
    #return 'ROLE_TEST88'; 
             ^this works, and my user has admin and test88 roles
}

那么我如何使用我的getter getLeveltitle根据我的users表中access_level_id的ID返回正确的角色。

从我所看到的,我将不得不使用getDoctine并查询我的数据库以使这个方法起作用,现在我想我可以通过服务/助手来做到这一点?但我不确定这是否是正确的方法。

或者我这样做错了吗?

谢谢,

0 个答案:

没有答案