Doctrine ODM / MongoDB: how to request links in embedded documents?

I am new to Doctrine ODM and I am completely stuck in a simple query :(

Let me start with the structure of the document:

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 voice documents by a specific voter (which is stored in the voteList.latest.voter element) .

I tried like this:

 $builder ->field('voteList.latest.voter')->references($voter) ->getQuery() ->execute(); 

And this method also:

 $result = $builder ->field('voteList.latest.voter.$id')->equals(new \MongoId($voter->getId())) ->getQuery() ->execute(); 

Both lead to this exception:

 Doctrine\ODM\MongoDB\MongoDBException: No mapping found for field 'voteList.latest.voter' in class 'App\BaseBundle\Document\Voting'. 

Am I creating a query incorrectly or is there something wrong with my document classes?

Thanks for reading, any advice is appreciated.

EDIT: attached documents

  /** * @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; } } 
+4
source share
1 answer

Found out that it does not work due to the error of the odm doctrine.

https://github.com/doctrine/mongodb-odm/pull/207

+1
source

Source: https://habr.com/ru/post/1385512/


All Articles