Doctrine ODM / PHP / MongoDB - Can't query Referenced Elements
I have an Event, that has a Privilege and this Privilege references a User. I'm trying to get all the events associated with a User, but I can't seem to get doctrine working as I'd like.
Here is my Mongo Schema
Array
(
[_id] => 4e63903cbc3470a2cd000002
[date] => 2011-10-09
[name] => Event1
[privileges] => Array
(
[0] => Array
(
[user] => Array
(
[$ref] => users
[$id] => 4e63611cbc347053a2000001
[$db] => eventdb
)
开发者_开发知识库 [role] => admin
)
)
[url] => Event1
)
The doctrine Event Entity:
class Event {
/**
* @Id
*/
protected $id;
/**
* @String
*/
protected $name;
/**
* @EmbedMany(targetDocument="\Event\Privilege")
*/
protected $privileges = array();
}
The Privilege Entity:
class Privilege {
/**
* @ReferenceOne(targetDocument="\User", cascade={"persist"})
*/
protected $user;
/**
* @String
*/
protected $role;
}
And the User entity:
class User {
/**
* @Id
*/
protected $id;
/**
* @String
*/
private $firstname;
/**
* @String
*/
private $lastname;
/**
* @String
*/
protected $username;
}
I tried the following with no success
Example 1:
$privilege_repository = $dm->getRepository('\Event\Privilege');
$qb1 = $privilege_repository->createQueryBuilder('\Event\Privilege') ->field('user.$id')->equals('4e63611cbc347053a2000001');
$query1 = $qb1->getQuery();
$result1 = $query1->execute();
$result1->count() :0
Example 2:
$privilege_repository = $dm->getRepository('\Event\Privilege');
$qb2 = $privilege_repository->createQueryBuilder('\Event\Privilege') ->field('user.$id')->equals(new \MongoId('4e63611cbc347053a2000001'));
$query2 = $qb2->getQuery();
$result2 = $query2->execute();
$result2->count() :0
Example 3:
$privilege_repository = $dm->getRepository('\Event\Privilege');
$qb3 = $privilege_repository->createQueryBuilder('\Event\Privilege') ->field('user')->equals(new \MongoId('4e63611cbc347053a2000001'));
$query3 = $qb3->getQuery();
$result3 = $query2->execute();
$result3->count() :0
I have also tried using the Event class but it doesn't get more success... I'm surely missing something but I have no clue what! Any help is welcome.
Thanks.
** One night later ** Finally got it working!
Example 2:
$privilege_repository = $dm->getRepository('\Event');
$qb2 = $privilege_repository->createQueryBuilder('\Event') ->field('privileges.user.$id')->equals(new \MongoId('4e63611cbc347053a2000001'));
$query2 = $qb2->getQuery();
$result2 = $query2->execute();
$result2->count() :3
You really need to use the new MongoId() otherwise it always returns empty!
you can try this:
$user = ... The User Document with _id 4e63611cbc347053a2000001 ...
$dm = $this->getDocumentManager();
$queryBuilder = $dm->createQueryBuilder('\Event')->field('privileges.user')->references($user)
strangely it's not documented here http://doctrine-mongodb-odm.readthedocs.org/en/latest/reference/query-builder-api.html, but worked for me.
What the result of user.id?
$qb2 = $privilege_repository->createQueryBuilder('\Event\Privilege') ->field('user.id')->equals(new \MongoId('4e63611cbc347053a2000001'));
quick note while the method works you won't be using an index when using the .$id reference.
It is best to use the db reference.
$privilege_repository = $dm->getRepository('\Event');
$qb2 = $privilege_repository->createQueryBuilder('\Event') ->field('privileges.user')->equals( \MongoDBRef::create("user", new \MongoId('4e63611cbc347053a2000001'),'database_name') );
$query2 = $qb2->getQuery();
$result2 = $query2->execute();
$result2->count() :3
I assumed user is the name of the collection above and database_name as database name. You can also create the index by :
db.collection.ensureIndex({'privileges.user':1};
When you have very large collections with multiple shards and replica sets the index will come in handy.
精彩评论