Doctrine ODM / MongoDB:如何在嵌入式文档中查询引用?

时间:2011-12-09 11:02:33

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

我是Doctrine ODM的新手,我完全坚持一个简单的查询:(

让我先从文档结构开始:

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 ...
)

此文档名为投票

我的问题是,如何找到特定选民的投票文档(存储在 voteList.latest.voter )

我试过这样:

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

这样也是:

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

两者都导致了这个例外:

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

我是否错误地构建了查询,或者我的文档类可能出错?

感谢阅读,任何建议都表示赞赏。

编辑:附上文件

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

1 个答案:

答案 0 :(得分:1)

由于一个doctrine-odm错误,它找不到它。

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