doctrine - ODM: References not being created on both documents -
say have 2 simple documents this, person can have many papers, paper can belong 1 person.
namespace dashboard\document; use doctrine\odm\mongodb\mapping\classmetadata; use doctrine\odm\mongodb\mapping\annotations 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 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); } } i thought read somewhere when create reference on 1 end, doctrine odm auto create references on both sides you. if execute statement below, see reference person paper document, references paper(s) in person document.
//for demo sake; $person contains 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(); when that, , check mongodb, reference paper-->person there. see no reference person-->paper shown in db. thought cascade annotations helped this, i'm missing something.
how can ensure reference contained on both ends, can run queries see papers belong single person? have done manually, or can have doctrine handle me?
update
the first paragraph on page made me think possible.
turns out should have read whole page. if use mappedby & inversedby, , persist document has inversedby in it's annotation, bi-directional relationship
/** @odm\referenceone(targetdocument="person", cascade={"all"}, inversedby="papers") */ protected $person; //will give me relationship can query on both sides $person->papers->add($paper);
Comments
Post a Comment