Doctrine ODM / MongoDB: How to query for references within embedded documents?
-
12-03-2021 - |
Question
I'm new to Doctrine ODM and i'm totally stuck with a simple query :(
Let me start with the document structure:
Array
(
[_id] => 4ee1e4527f749c9411000012
[voteList] => Array
(
[_id] => 4ee1e4527f749c9411000013
[votes] => Array
(
... stripped ...
)
[latest] => Array
(
[_id] => 4ee1e4527f749c9411000014
[rating] => 1
[voter] => Array
(
[$ref] => Voter
[$id] => 4ee1e4527f749c941100000f
[$db] => x_test
)
)
)
... stripped ...
)
This document is called Voting.
My Question is, how to find Voting-documents by a particular voter (which is stored in voteList.latest.voter).
I tried it like this:
$builder
->field('voteList.latest.voter')->references($voter)
->getQuery()
->execute();
And this way also:
$result = $builder
->field('voteList.latest.voter.$id')->equals(new \MongoId($voter->getId()))
->getQuery()
->execute();
Both are leading to this exception:
Doctrine\ODM\MongoDB\MongoDBException: No mapping found for field 'voteList.latest.voter' in class 'App\BaseBundle\Document\Voting'.
Am i building the query incorrectly or might something be wrong with my document classes?
Thanks for reading, any advices appreciated.
EDIT: Documents attached
/**
* @ODM\Document(repositoryClass="App\BaseBundle\Document\VotingRepository")
*/
class Voting
{
/**
* @ODM\Id
* @var int
*/
protected $id;
/**
* @ODM\EmbedOne(targetDocument="App\BaseBundle\Document\VoteList")
* @var VoteList
*/
protected $voteList;
public function __construct()
{
if ($this->voteList === null) {
$this->voteList = new VoteList();
}
}
/**
* @return string
*/
public function getId()
{
return $this->id;
}
/**
* @return VoteList
*/
public function getVoteList()
{
return $this->voteList;
}
}
;
/**
* @ODM\EmbeddedDocument
*/
class VoteList implements \Countable, \ArrayAccess, \IteratorAggregate
{
/**
* @ODM\Id
*/
protected $id;
/**
* @ODM\EmbedMany(targetDocument="App\BaseBundle\Document\Vote")
* @var Vote[]
*/
protected $votes = array();
/**
* @ODM\EmbedOne(targetDocument="App\BaseBundle\Document\Vote")
* @var Vote
*/
protected $latest;
public function getId()
{
return $this->id;
}
/**
* @return Vote
*/
public function getLatest()
{
return $this->latest;
}
}
/**
* @ODM\EmbeddedDocument
*/
class Vote
{
/**
* @ODM\Id
*/
protected $id;
/**
* @ODM\ReferenceOne(targetDocument="App\BaseBundle\Document\Voter")
* @var Voter
*/
public $voter;
public function getId()
{
return $this->id;
}
public function getVoter()
{
return $this->voter;
}
public function setVoter(Voter $voter)
{
$this->voter = $voter;
}
}
Solution
Figured out it's not working due to a doctrine-odm bug.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow