大家好我正在使用学说ODM并且遇到水化器问题。我不能使用embed集合或引用类对文档进行提取。这些类的提取结果给了我对象,我真的需要将它们放在数组中,用于由骨干实现消耗的休息模块。 这是一个示例类: Analyse.php文件
<?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 Collection
<?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($ data ['elements'])返回对象Collection或代理类 你能帮助我吗。任何事情将被赞赏它已经2周我无法使它工作。 阅读有关Hydrator Strategy的内容,但我不知道如何实现它。
答案 0 :(得分:0)
目前,Doctrine ODM实现不提供嵌入对象和引用的递归。
如果您在var_dump()
上使用$hydrator->extract($results)
,则会看到所有嵌入/引用都以原始对象格式存在。
这里你可以做的是使用Zend\Stdlib\Hydrator\Strategy,并定义你自己的提取/水合逻辑。 Doctrine扩展了Zend Framework 2的水分和策略。