簡體   English   中英

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

[英]Doctrine ODM / MongoDB: How to query for references within embedded documents?

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

該文件稱為投票

我的問題是, 如何找到一個特定的選民 投票 -documents(存儲在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;
        }
    }

由於一個doctrine-odm錯誤,它找不到它。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM