2013-04-05 101 views
0

嗨大家我使用原則ODM和hydator有問題。我不能使用嵌入集合或參考類對文檔進行摘錄。這些類的提取結果給我的對象,我真的需要讓他們在數組中休息模塊,這是由骨幹實施消耗。 這裏一個例子類: Analyse.php文檔zf2 doctrine odm收集水合

<?php 
namespace Application\Document; 

use Doctrine\ODM\MongoDB\Mapping\Annotations as 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 as $element) { 
      $this->elements->add($element); 
     } 

    } 

    public function removeElements(Collection $elements) 
    { 
     foreach ($elements as $item) { 
      $this->elements->removeElement($item); 
     } 
    } 

    public function getElements() 
    { 
     return $this->elements; 
    } 

} 

ElementAnalyse.php收集

<?php 

namespace Application\Document; 

use Doctrine\ODM\MongoDB\Mapping\Annotations as 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; 
    } 

    /** 
    * Get nom 
    */ 
    public function getNom() 
    { 
     return $this->nom; 
    } 

    /** 
    * Set unite 
    */ 
    public function setUnite($unite) 
    { 
     $this->unite = $unite; 
     return $this; 
    } 

    /** 
    * Get unite 
    * 
    * @return string 
    */ 
    public function getUnite() 
    { 
     return $this->unite; 
    } 

    /** 
    * add valeurnormales 
    */ 
    public function addValeurnormales(Collection $vn) 
    { 
     foreach ($vn as $item) { 
      $this->valeurnormales->add($item); 
     } 
    } 

    public function removeValeurnormales(Collection $vn) 
    { 
     foreach ($vn as $item) { 
      $this->valeurnormales->removeElement($item); 
     } 
    } 

    /** 
    * Get valeurnormales 
    */ 
    public function getValeurnormales() 
    { 
     return $this->valeurnormales; 
    } 

    /** 
    * Set type 
    * 
    * @param $type 
    * @return Analyse 
    */ 
    public function setType($type) 
    { 
     $this->type = $type; 
     return $this; 
    } 

    /** 
    * Get 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 as $item => $value) 
     { 
      if(array_key_exists($item, $objects)) 
      { 
       $this->$item = $value; 
      } 
     } 
    } 

} 

這裏我的GetList方法

public function getList() 
    { 
     $hydrator = new DoctrineHydrator($entityManager, 'Application\Document\Analyse'); 

     $service = $this->getAnalyseService(); 
     $results = $service->findAll(); 

     $data = $hydrator->extract($results); 
     return new JsonModel($data); 
    } 

而且很明顯的var_dump($數據[ '元素' ])返回對象集合或代理類 你能幫助我嗎?任何東西將不勝感激它已經2個星期,我不能讓它工作。 閱讀Hydrator Strategy,但我不知道如何實現它。

回答

0

目前,Doctrine ODM實現不提供嵌入對象和引用的遞歸。

如果在$hydrator->extract($results)上使用var_dump(),則會看到所有嵌入/引用都以其原始對象格式存在。

你可以在這裏做的是使用Zend\Stdlib\Hydrator\Strategy,並定義你自己的提取/水合的邏輯。 Doctrine擴展了Zend Framework 2的水化器和策略。

+1

OP詢問有關如何實施Hydrator策略(無論是什麼)的建議,如果您知道如何實施Hydrator策略,您是否可以分享一個快速示例? – 2014-03-31 00:14:22