2
假設我有兩個簡單的文檔,其中一個人可以有多個文檔,但一個文檔只能屬於一個人。ODM:兩個文檔上都沒有創建引用
namespace Dashboard\Document;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
/** @ODM\Document(db="testing", collection="person")
* @ODM\InheritanceType("COLLECTION_PER_CLASS")
*/
class Person
{
/**
* @ODM\Id
*/
protected $id;
/** @ODM\Field(type="string") */
protected $slug;
/** @ODM\Field(type="string") */
protected $name;
/** @ODM\ReferenceMany(targetDocument="Paper", cascade={"all"}) */
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);
}
}
namespace Dashboard\Document;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
/** @ODM\Document(db="testing", collection="paper")
* @ODM\InheritanceType("COLLECTION_PER_CLASS")
*/
class Paper
{
/**
* @ODM\Id
*/
protected $id;
/** @ODM\Field(type="string") */
protected $name;
/** @ODM\ReferenceOne(targetDocument="Person", cascade={"all"}) */
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自動創建一個雙方的引用爲您服務。因此,如果我執行以下聲明,我將看到來自Paper文檔的人員引用,以及引用Person文檔中的Paper(s)的引用。
//For demo sake; $person already contains a Person document
try {
$paper = $dm->getRepository('\Dashboard\Document\Paper')
->find($paperId);
} catch (\Doctrine\ODM\MongoDB\MongoDBException $e) {
$this->setStatusFailure($e->getMessage());
$this->sendResponse();
}
$paper->person = $person;
$dm->persist($paper);
$dm->flush();
當我這樣做,並檢查mongodb時,從紙 - >人的參考在那裏。但是我沒有看到參考人員 - >在db中顯示的論文。我認爲級聯註釋對此有所幫助,但顯然我錯過了一些東西。
如何確保引用包含在兩端,以便我可以運行查詢以查看屬於單個人的所有論文?這是必須手動完成,還是我可以有教條爲我處理?
UPDATE
此頁面上的第一款讓我認爲這是可能的。