简体   繁体   English

zf2学说odm集合水合

[英]zf2 doctrine odm collection hydration

Hi everybody Im using doctrine ODM and have trouble with hydrator. 大家好,我使用教义ODM,并且遇到水合器的问题。 I can't make extract on document with embed collection nor reference Class. 我无法使用嵌入集合或引用类对文档进行提取 the extract result for these class give me object and i really need to have them in array for rest module which is consumed by backbone implementation. 这些类的提取结果给了我对象,我确实需要将它们放在数组中以供其他模块使用,这些模块被骨干实现所消耗。 Here a example class : Analyse.php Document 这里是一个示例类: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 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;
            }
        }
    }

}

Here my getList Method 这是我的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);
    }

And obviously var_dump($data['elements']) return object Collection or proxy class Can You help me. 显然var_dump($ data ['elements'])返回对象Collection或代理类您可以帮我吗。 Anything will be appreciated it been 2 weeks i can't make it work. 任何事情将不胜感激,这已经有2周了,我无法正常工作。 Read about Hydrator Strategy out there but i don't knnow how to implement it. 了解有关Hydrator Strategy的信息,但我不知道如何实施。

Currently, the Doctrine ODM implementation does not provide recursion for embedded objects and references. 当前,Doctrine ODM实现不为嵌入式对象和引用提供递归。

If you use var_dump() on your $hydrator->extract($results) , you'll see that all your embeds/references are there in their original object format. 如果在$hydrator->extract($results) var_dump()上使用var_dump() ,则会看到所有嵌入/引用都以其原始对象格式存在。

What you can do here is to use Zend\\Stdlib\\Hydrator\\Strategy , and define your own logic for extraction/hydration. 您可以在这里使用Zend \\ Stdlib \\ Hydrator \\ Strategy ,并定义自己的提取/水合逻辑。 Doctrine extends Zend Framework 2's hydrators and strategies. 原则扩展了Zend Framework 2的水化器和策略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM