Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8371405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:09:13+00:00 2026-06-09T14:09:13+00:00

In my project I need to store role hierarchy in database and create new

  • 0

In my project I need to store role hierarchy in database and create new roles dynamically.
In Symfony2 role hierarchy is stored in security.yml by default.
What have I found:

There is a service security.role_hierarchy (Symfony\Component\Security\Core\Role\RoleHierarchy);
This service receives a roles array in constructor:

public function __construct(array $hierarchy)
{
    $this->hierarchy = $hierarchy;

    $this->buildRoleMap();
}

and the $hierarchy property is private.

This argument comes in constructor from \Symfony\Bundle\SecurityBundle\DependencyInjection\SecurityExtension::createRoleHierarchy()
which uses roles from config, as I understood:

$container->setParameter('security.role_hierarchy.roles', $config['role_hierarchy']);

It seems me that the best way is to compile an array of roles from database and set it as an argument for the service. But I haven’t yet understood how to do it.

The second way I see is to define my own RoleHierarchy class inherited from the base one. But since in the base RoleHierarchy class the $hierarchy property is defined as private, than I would have to redefine all the functions from the base RoleHierarchy class. But I don’t think it is a good OOP and Symfony way…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:09:15+00:00Added an answer on June 9, 2026 at 2:09 pm

    The solution was simple.
    First I created a Role entity.

    class Role
    {
        /**
         * @var integer $id
         *
         * @ORM\Column(name="id", type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        private $id;
    
        /**
         * @var string $name
         *
         * @ORM\Column(name="name", type="string", length=255)
         */
        private $name;
    
        /**
         * @ORM\ManyToOne(targetEntity="Role")
         * @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
         **/
        private $parent;
    
        ...
    }
    

    after that created a RoleHierarchy service, extended from the Symfony native one. I inherited the constructor, added an EntityManager there and provided an original constructor with a new roles array instead of the old one:

    class RoleHierarchy extends Symfony\Component\Security\Core\Role\RoleHierarchy
    {
        private $em;
    
        /**
         * @param array $hierarchy
         */
        public function __construct(array $hierarchy, EntityManager $em)
        {
            $this->em = $em;
            parent::__construct($this->buildRolesTree());
        }
    
        /**
         * Here we build an array with roles. It looks like a two-levelled tree - just 
         * like original Symfony roles are stored in security.yml
         * @return array
         */
        private function buildRolesTree()
        {
            $hierarchy = array();
            $roles = $this->em->createQuery('select r from UserBundle:Role r')->execute();
            foreach ($roles as $role) {
                /** @var $role Role */
                if ($role->getParent()) {
                    if (!isset($hierarchy[$role->getParent()->getName()])) {
                        $hierarchy[$role->getParent()->getName()] = array();
                    }
                    $hierarchy[$role->getParent()->getName()][] = $role->getName();
                } else {
                    if (!isset($hierarchy[$role->getName()])) {
                        $hierarchy[$role->getName()] = array();
                    }
                }
            }
            return $hierarchy;
        }
    }
    

    … and redefined it as a service:

    <services>
        <service id="security.role_hierarchy" class="Acme\UserBundle\Security\Role\RoleHierarchy" public="false">
            <argument>%security.role_hierarchy.roles%</argument>
            <argument type="service" id="doctrine.orm.default_entity_manager"/>
        </service>
    </services>
    

    That’s all.
    Maybe, there is something unnecessary in my code. Maybe it is possible to write better. But I think, that main idea is evident now.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are starting a new project where we need to store product and many
My project requires users to create some basic math functions, I need to store
In my project I need to store an image into a sqlite database and
In my project I need to store the values dynamically in a string and
for my current project i need to store a little database on disk, that
In my project I need to use third party code, stored in several Git
My android app project need to add a new function of saving the click
For a school project I need to create a program and it'd be nice
For a new project I need to load big XML files (200MB+) to a
Im my current project we need to interface with sharepoint to store and retrieve

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.