遍历Symfony2中的Doctrine2 ODM / MongoDB对象图无法填充第二级对象中的属性

时间:2012-10-31 23:15:10

标签: php mongodb symfony doctrine-orm doctrine-odm

我正在将我正在运行的Symfony2应用程序转换为通过Doctrine-ODM使用MongoDB。我有绝大多数系统工作,但我不能让用户角色部分工作。我可以登录,但是没有任何角色附加到用户。

除了相关的内容之外,相关的文档类都已删除。

用户

<?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;
    }

}

<?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;
    }
}

作用

<?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;
    }

}

我使用fixture来将数据加载到数据库中,MongoDB中的数据如下所示。 (我删除了其他数据元素。)

用户。

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

用户引用的组。 (这来自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")) ] }

最后,小组引用的角色。 (也取自Symfony2运行的确切查询)

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

此外,调用用户的getRoles()函数中的异常并返回以下文本。

  

角色必须存在于组中:具有ID的Base.Users:   5091a7241311fae01f00000b。

问题是从数据库中查询角色,但不会将其填充到角色对象中。我可以验证它们是否正在加载,因为当我评论异常时,它将运行并尝试为每个组添加正确数量的角色。问题是角色的name属性设置为NULL。角色对象本身是一个持久化和加载的对象,就像我执行print_r($ role); exit;直接在if语句之前,我将得到教义对象展示的巨大递归输出。唯一没有发生的事情是“名称”(和其他)属性不会从数据库中加载。

任何有关如何解决此问题的见解将不胜感激。感谢。

1 个答案:

答案 0 :(得分:0)

我能够确定一种解决方法。基本上,使用find,findBy,findOneBy等方便的函数似乎并没有设置对象进行遍历。通过修改加载函数来使用querybuilder而不是方便的函数“findOneBy”,我能够得到正确的结果。

我的修改后的查询如下。希望这有助于将来的某些人。

/**
     * 
     * @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();
    }

我想它可能会更简洁,但我不得不把它分开来调试它,并继续我的生活。 :)