Doctrine-ODM を介して MongoDB を使用するように、別の方法で動作している Symfony2 アプリケーションを変換しています。システムの大部分は動作していますが、ユーザー ロールの部分を動作させることができません。ログインできますが、ユーザーにロールが関連付けられていません。
関連するドキュメント クラスはここにあり、関連するもの以外はすべて削除されています。
ユーザー
<?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;
}
}
フィクスチャを使用してデータをデータベースにロードすると、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() 関数で例外が呼び出され、次のテキストが返されます。
ロールはグループに存在する必要があります: Base.Users ID: 5091a7241311fae01f00000b。
問題は、ロールがデータベースからクエリされているが、ロール オブジェクトに入力されていないことです。例外にコメントすると、例外が実行され、グループごとに正しい数のロールを追加しようとするため、それらがロードされていることを確認できます。問題は、ロールの name プロパティが NULL に設定されていることです。ロール オブジェクト自体は、print_r($role);exit; を実行したときと同様に、永続化されて読み込まれたオブジェクトです。if ステートメントの直前に、doctrine オブジェクトが示す非常に再帰的な出力を取得します。起こらない唯一のことは、「名前」(およびその他の) プロパティがデータベースからロードされないことです。
これをどのように解決できるかについての洞察は大歓迎です。ありがとう。