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 8774865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:40:26+00:00 2026-06-13T18:40:26+00:00

I am converting my otherwise working Symfony2 application to use MongoDB through Doctrine-ODM. I

  • 0

I am converting my otherwise working Symfony2 application to use MongoDB through Doctrine-ODM. I have the vast majority of the system working, but I can’t get the user roles portion working. I can login, but then there are no roles attached to the user.

The relevant document classes are here with everything stripped out except what is relevant.

User

<?php

namespace XXXXX\UserBundle\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use Doctrine\Common\Collections\ArrayCollection;
use XXXXX\UserBundle\Interfaces\UserInterface;

/**
 * 
 * @MongoDB\Document( collection="user")
 * 
 */
class User implements UserInterface {

    /**
     * @MongoDB\Id
     */
    protected $id;

    /**
     * @MongoDB\ReferenceMany(targetDocument="Group")
     */
    protected $groups;

    /**
     * Constructor
     */
    public function __construct() {
        $this->groups = new ArrayCollection();
        $this->salt = base_convert(sha1(uniqid(mt_rand(), true)), 16, 36);
    }

    public function getRoles() {
        $array = array();
        //parse the roles down to an array
        foreach ($this->getGroups() as $group) {
            /* @var $group Group */
            foreach ($group->getRoles() as $role) {
                /* @var $role Role */
                if(!$role->getName())
                    throw new \Exception('Role must exist in group: '.$group->getName().' with ID: '.$group->getId().'.');
                $array[$role->getName()] = $role->getName();
            }
        }
        sort($array);
        return $array;
    }

    /**
     * Get groups
     *
     * @return Doctrine\Common\Collections\Collection 
     */
    public function getGroups() {
        return $this->groups;
    }

}

Group

<?php

namespace XXXXX\UserBundle\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use XXXXX\UserBundle\Interfaces\UserInterface;
use XXXXX\UserBundle\Interfaces\RoleInterface;
use XXXXX\UserBundle\Interfaces\GroupInterface;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @MongoDB\Document( collection="user_group" )
 */
class Group implements GroupInterface {

    /**
     * @MongoDB\Id
     */
    protected $id;

    /**
     * @MongoDB\String
     * @var string 
     */
    protected $name;

    /**
     * @MongoDB\ReferenceMany(targetDocument="User")
     */
    protected $users;

    /**
     * @MongoDB\ReferenceMany(targetDocument="Role", inversedBy="groups")
     */
    protected $roles;

    /**
     * Constructor
     */
    public function __construct() {
        $this->users = new ArrayCollection();
        $this->roles = new ArrayCollection();
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Get roles
     *
     * @return Doctrine\Common\Collections\Collection 
     */
    public function getRoles()
    {
        return $this->roles;
    }
}

Role

<?php

namespace XXXXX\UserBundle\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use XXXXX\UserBundle\Interfaces\UserInterface;
use XXXXX\UserBundle\Interfaces\GroupInterface;
use XXXXX\UserBundle\Interfaces\RoleInterface;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @MongoDB\Document( collection="user_role")
 */
class Role implements RoleInterface {

    /**
     * @MongoDB\Id
     */
    protected $id;

    /**
     * @MongoDB\String
     * @var string 
     */
    protected $name;

    /**
     * @MongoDB\String
     * @var string 
     */
    protected $description;

    /** 
     * @MongoDB\ReferenceMany(targetDocument="Group", mappedBy="roles") 
     */
    protected $groups;

    /**
     * Set name
     *
     * @param string $name
     * @return RoleInterface
     */
    public function setName($name) {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName() {
        return $this->name;
    }

    public function getId() {
        return $this->id;
    }

    public function getDescription() {
        return $this->description;
    }

    public function setDescription($description) {
        $this->description = $description;
    }

}

I use fixtures to load the data into the database, and the data in MongoDB is as follows. ( I stripped the additional data elements.)

User.

{ "_id" : ObjectId("5091a7241311fae01f00000d"), "groups" : [  DBRef("user_group", ObjectId("5091a7241311fae01f00000b")),      DBRef("user_group", ObjectId("5091a7241311fae01f00000c")) ] }

Groups that are referenced by the User. (This is from the query that is run by Symfony2)

db.user_group.find({ "_id": { "$in": { "5091a7241311fae01f00000b":ObjectId("5091a7241311fae01f00000b"), "5091a7241311fae01f00000c": ObjectId("5091a7241311fae01f00000c") } } }).sort([ ]);

{ "_id" : ObjectId("5091a7241311fae01f00000b"), "name" : "Base.Users", "roles" : [ DBRef("user_role", ObjectId("5091a7241311fae01f000009")) ] }
{ "_id" : ObjectId("5091a7241311fae01f00000c"), "name" : "AdminPortal.Base", "roles" : [ DBRef("user_role", ObjectId("5091a7241311fae01f000009")),        DBRef("user_role", ObjectId("5091a7241311fae01f00000a")) ] }

And finally, the roles referenced by the groups. (Also taken from the exact query being run by Symfony2)

db.user_role.find({ "_id": { "$in": { "5091a7241311fae01f000009": ObjectId("5091a7241311fae01f000009") } } }).sort([ ]);
{ "_id" : ObjectId("5091a7241311fae01f000009"), "name" : "ROLE_USER", "description" : "Role required for all system users." }

Further, the exception in the getRoles() function for the user is called and the following text is returned.

Role must exist in group: Base.Users with ID:
5091a7241311fae01f00000b.

The problem is that the roles are being queried from the database, but are not then being populated into the role object. I can verify that they are being loaded, as when I comment the exception, it will run and attempt to add the correct number of roles per group. The problem is that the name property of the role is set to NULL. The role object itself is a persisted and loaded object as when I do a print_r($role);exit; directly before the if statement, I will get the hugely recursive output that doctrine objects exhibit. The only thing that doesn’t happen is that the “name” (and other) properties are not loaded from the database.

Any insight into how I can solve this would be greatly appreciated. Thanks.

  • 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-13T18:40:27+00:00Added an answer on June 13, 2026 at 6:40 pm

    I was able to determine a work-around. Basically, using the convientent functions like find, findBy, findOneBy, etc do not seem to be setting the objects up for traversing. I was able to get the correct result by modifying the loading function to use a querybuilder instead of the convenient function “findOneBy”.

    My modified query is below. Hopefully this helps somebody in the future.

    /**
         * 
         * @param string $username
         * @return User|Null
         */
        public function findUserByUserName($username) {
            $qb = $this->createQueryBuilder();
            $qb->find($this->getClassName());
            $qb->field('username');
            $qb->equals($username);
            $query = $qb->getQuery();
            return $query->getSingleResult();
        }
    

    I suppose it could be more concise, but I had to break it apart to debug it, and am moving on with my life. 🙂

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

Sidebar

Related Questions

Have a legacy LAMP application that I'm in the process of converting over to
Converting to ODB.NET from System.Data.OracleClient and need help converting my connection string. Here is
I'm working on converting a human readable time to a datetime object . In
I am converting absolute file-system path to relative path using following code. public static
Currently working with converting SQLException error messages into messages that are more useful for
Below is a VBA function that I created for use in Excel for converting
I just started working coverting my iOS app to Android. But I'm having the
I have an input PDF file (usually, but not always generated by pdfTeX), which
I have a C# app I'm working on that loads it's code remotely, and
I have some inherited code for FlexLM that is converting an integer to a

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.