2013-04-05 4 views
0

안녕하세요 여러분, 저는 ODM 교리를 사용하고 수화기에 문제가 있습니다. 을 포함 컬렉션 또는 참조 클래스가있는 문서에서 추출 할 수 없습니다. 이 클래스에 대한 추출 결과는 객체를 제공하고 백본 구현에 의해 소비되는 나머지 모듈을 배열에 실제로 포함해야합니다. 여기 예를 들어 클래스 : Analyse.php 문서zf2 교리 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 ($ 데이터 [ '요소' ]) return object 컬렉션 또는 프록시 클래스 나를 도와 줄 수 있습니까? 아무 것도 그것을 2 주 나는 그것을 작동하게 만들 수 없습니다 인정 될 것입니다. Hydrator Strategy에 대해 알아보십시오.하지만 구현 방법을 알지 못합니다.

답변

0

현재 Doctrine ODM 구현은 포함 된 객체 및 참조에 대한 재귀를 제공하지 않습니다.

$hydrator->extract($results)var_dump()을 사용하면 모든 퍼가기/참조가 원래의 개체 형식으로 표시됩니다.

여기서 할 수있는 것은 Zend\Stdlib\Hydrator\Strategy을 사용하고 추출/수화를위한 논리를 정의하는 것입니다. Doctrine은 젠드 프레임 워크 2의 해수정과 전략을 확장합니다.

+1

OP는 Hydrator 전략을 구현하는 방법에 대한 제안을 물었습니다. (그것이 무엇이든간에) 구현 방법을 알고 있다면 간단한 예제를 공유 할 수 있습니까? –