我有两个文件。我正在尝试查找与特定人员相关的所有论文。Person
文档保存在它们的集合中,并且正在从to创建引用Paper
,但不是相反。
/** @ODM\Document */
class Paper
{
/**
* @ODM\Id
*/
protected $id;
/** @ODM\ReferenceOne(targetDocument="Person", cascade={"all"}, mappedBy="papers") */
protected $person;
public function __get($property) {
return $this->$property;
}
public function __set($property, $value) {
$this->$property = $value;
}
public function toArray() {
return get_object_vars($this);
}
}
/** @ODM\Document */
class Person
{
/**
* @ODM\Id
*/
protected $id;
/** @ODM\ReferenceMany(targetDocument="Paper", cascade={"all"}, inversedBy="person") */
protected $papers;
public function __get($property) {
return $this->$property;
}
public function __set($property, $value) {
$this->$property = $value;
}
public function toArray() {
return get_object_vars($this);
}
}
创建一个新的双向参考
$person = $dm->getRespository('Person')->find($person_id);
$paper = new Paper();
$person->papers->add($paper);
$dm->persist($person);
$dm->flush();
在后面的代码中,这个查询返回 0 个结果;不应该是退回指定人写的论文吗?
$papers = $dm->createQueryBuilder('Paper')
->field('person.$id')->equals(new \MongoId($person_id_as_string))
->getQuery()->execute();