【发布时间】:2012-01-16 16:31:46
【问题描述】:
我是 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 ...
)
此文档称为Voting。
我的问题是,如何查找特定 voter 的 Voting 文档(存储在 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;
}
}
【问题讨论】:
-
您应该发布两个文档的代码,以便我们更完整地了解。
-
@elnur 感谢您的评论。编辑:添加文档,精简为引用/嵌入内容
标签: mongodb doctrine symfony doctrine-orm doctrine-odm