php - zf2 doctrine odm collection hydration -
hi im using doctrine odm , have trouble hydrator. can't make extract on document embed collection nor reference class. extract result these class give me object , need have them in array rest module consumed backbone implementation. here example class : analyse.php document
<?php namespace application\document; use doctrine\odm\mongodb\mapping\annotations odm; use doctrine\common\collections\collection; /** * application\document\analyse * * @odm\document(collection="analyse") */ class analyse extends basedocument { /** * @odm\id */ protected $id; /** * @odm\field(type="string") */ protected $nom; /** * @odm\field(type="string") * @odm\index */ protected $alias; /** * @odm\embedmany(targetdocument="elementsanalyse") */ protected $elements = array(); public function __construct() { parent::__construct(); $this->elements = new \doctrine\common\collections\arraycollection(); } public function getid() { return $this->id; } public function setnom($nom) { $this->nom = $nom; return $this; } public function getnom() { return $this->nom; } public function setalias($alias) { $this->alias = $alias; return $this; } public function getalias() { return $this->alias; } public function addelements(collection $elements) { foreach ($elements $element) { $this->elements->add($element); } } public function removeelements(collection $elements) { foreach ($elements $item) { $this->elements->removeelement($item); } } public function getelements() { return $this->elements; } }
elementanalyse.php collection
<?php namespace application\document; use doctrine\odm\mongodb\mapping\annotations odm; use doctrine\common\collections\collection; /** * application\document\valeurnormales * * @odm\embeddeddocument * */ class elementsanalyse { /** * @odm\field(type="string") */ protected $nom; /** * @odm\field(type="string") */ protected $unite; /** * @odm\embedmany(targetdocument="valeurnormales") */ protected $valeurnormales = array(); /** * @odm\field(type="string") */ protected $type; public function __construct() { $this->valeurnormales = new \doctrine\common\collections\arraycollection(); } /** * set nom */ public function setnom($nom) { $this->nom = $nom; return $this; } /** * nom */ public function getnom() { return $this->nom; } /** * set unite */ public function setunite($unite) { $this->unite = $unite; return $this; } /** * unite * * @return string */ public function getunite() { return $this->unite; } /** * add valeurnormales */ public function addvaleurnormales(collection $vn) { foreach ($vn $item) { $this->valeurnormales->add($item); } } public function removevaleurnormales(collection $vn) { foreach ($vn $item) { $this->valeurnormales->removeelement($item); } } /** * valeurnormales */ public function getvaleurnormales() { return $this->valeurnormales; } /** * set type * * @param $type * @return analyse */ public function settype($type) { $this->type = $type; return $this; } /** * type * * @return type */ public function gettype() { return $this->type; } /** * toarray function */ public function toarray() { return get_object_vars($this); } /** * fromarray function * */ public function fromarray(array $array) { $objects = $this->toarray(); foreach($array $item => $value) { if(array_key_exists($item, $objects)) { $this->$item = $value; } } } }
here getlist method
public function getlist() { $hydrator = new doctrinehydrator($entitymanager, 'application\document\analyse'); $service = $this->getanalyseservice(); $results = $service->findall(); $data = $hydrator->extract($results); return new jsonmodel($data); }
and var_dump($data['elements']) return object collection or proxy class can me. appreciated been 2 weeks can't make work. read hydrator strategy out there don't knnow how implement it.
currently, doctrine odm implementation not provide recursion embedded objects , references.
if use var_dump()
on $hydrator->extract($results)
, you'll see embeds/references there in original object format.
what can here use zend\stdlib\hydrator\strategy, , define own logic extraction/hydration. doctrine extends zend framework 2's hydrators , strategies.
Comments
Post a Comment