JGrinon
JGrinon

Reputation: 1453

Class for manage users groups

I'm creating a class to manage user groups. A group can contain either users or groups of users

I wonder if there is already a symfony class to implement that handle such relationships.

The best way that occurred to me is something like this:

class Group
{

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;

    /**
     * @ORM\ManyToOne(targetEntity="User")
     * @ORM\JoinColumn(name="coordinator_id", referencedColumnName="id", nullable=false)
     */
    private $coordinator;

    /**
     * @ORM\ManyToOne(targetEntity="Group")
     * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", nullable=true)
     */
    private $parent; 

    /**
     * @ORM\ManyToMany(targetEntity="User", inversedBy="users")
     * @ORM\JoinTable(name="groups_users")
     */
    private $users;       

}

Upvotes: 0

Views: 56

Answers (1)

Nicolai Fröhlich
Nicolai Fröhlich

Reputation: 52493

There is currently ( as of 2013-06-16 ) no bundle available which directly provides this special implementation.

FOSUserBundle introduces groups for roles ... but the implementation is pretty basic and needs manual hands-on to get it working fully. Maybe it's something you could look into for inspiration though.

Otherwise your approach looks okay to me for this special use-case.

You might be able to improve by using nested sets with Gedmo's Tree doctrine extension to handle the group-nesting.

Upvotes: 1

Related Questions