Php zf2学说odm集合
大家好,我用的是ODM,水合器有问题。我无法对包含嵌入集合或引用类的文档进行提取。这些类的提取结果给了我对象,我真的需要将它们放在rest模块的数组中,rest模块由主干实现使用。 下面是一个示例类: analysis.php文档Php zf2学说odm集合,php,zend-framework2,doctrine-odm,Php,Zend Framework2,Doctrine Odm,大家好,我用的是ODM,水合器有问题。我无法对包含嵌入集合或引用类的文档进行提取。这些类的提取结果给了我对象,我真的需要将它们放在rest模块的数组中,rest模块由主干实现使用。 下面是一个示例类: analysis.php文档 <?php namespace Application\Document; use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; use Doctrine\Common\Collections\Collect
<?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;
}
}
显然,var_dump($data['elements'])返回对象集合或代理类
你能帮助我吗。任何事情都会很感激的。已经两周了,我不能让它工作了。
阅读关于Hydroor策略的文章,但我不知道如何实现它。目前,Doctrine ODM实现没有为嵌入式对象和引用提供递归 如果在
$hydrator->extract($results)
上使用var\u dump()
,您将看到所有嵌入/引用都以其原始对象格式存在
您可以在这里使用,并定义您自己的提取/水合逻辑。条令扩展了Zend Framework 2的水合器和策略。OP询问了关于如何实施水合器策略(无论是什么)的建议,如果您知道如何实施水合器策略,您可以分享一个简单的例子吗?
<?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;
}
}
}
}
public function getList()
{
$hydrator = new DoctrineHydrator($entityManager, 'Application\Document\Analyse');
$service = $this->getAnalyseService();
$results = $service->findAll();
$data = $hydrator->extract($results);
return new JsonModel($data);
}