2012-07-19 58 views
1

我有這樣的場景:我是否需要在symfony2中將「Role」類聲明爲array或arrayCollection?

  • User意志屬於Groups
  • Groups會有一定Roles

現在我想知道,我確實需要Group.php

宣佈此

這是Group.php,這是Role.php

$this->roles = new \Doctrine\Common\Collections\ArrayCollection(); 

$this->roles = new array(); 

我很困惑如何與symfony的安全性的工作。我的意思是symfony安全想要在arraycollection或array中扮演角色的格式。

回答

3

的Symfony 2所預計和RolearraygetRoles()方法返回值。由於User可以有很多Group和每個組可能有很多Role,我會這樣做:

/** 
* @ORM\Entity 
* @ORM\Table(name="user") 
*/ 
class User 
{ 
    /* @ORM\ManyToMany(targetEntity="Group", inversedBy="users") */ 
    private $groups; 

    public function __construct() 
    { 
     $this->groups = new \Doctrine\Common\Collections\ArrayCollection(); 
    } 

    public function getRoles() 
    { 
     $roles = array(); 

     foreach($this->groups as $group) : 
      $roles = array_merge($roles, $group->getRoles()->toArray()); 
     endforeach; 

     return $roles; 
    } 
} 
1

那麼,你的問題是ArrayArrayCollection

使用ArrayCollection,因爲它在ORM的上下文中做得更好,並且更容易擴展/操作(請參閱:Doctrine Collections)。

有關在Doctrine中設置集合的更多信息,請參閱文檔中的Initializing Collections


FOSUserBundle

您也可以考慮使用優秀Friends of Symfony (FOS)UserBundle。這完全支持GroupsRoles

訪問控制列表(ACL)

您可能還需要檢查的ACL:

在複雜的應用程序,你會經常遇到訪問決策的問題不能隻立足於人(令牌)誰正在請求訪問,但也涉及訪問被請求的域對象。這是ACL系統進來。

Symfony2 - Access Control Lists

相關問題