dongsicheng5262 2017-09-10 02:52
浏览 60
已采纳

使用与ZF3 MVC Response JSON的关系序列化对象

I have a Zend Framework 3 app. I added the ViewJsonStrategy to module.config.php. But i wants return a JSON Object with their relation objects ONE TO MANY in Array:

On my controller

public function getdirectoriojsonAction(){
$idraiz = $this->cfgGral->getIdDirectorioRaiz();
if ($idraiz <= 0) {
return $this->redirect()->toRoute('configuracion', ['action' => 'index']);
} else {
if ($this->params()->fromRoute('id') > 0) {
$idraiz = $this->params()->fromRoute('id');
}
$directorio = $this->em->find($this->rutaEntityDirectorio, $idraiz);
if ($directorio->getEstado() != 0) {
$directorio = $directorio->getPadre();
$directorio->getDirectoriosHijos();
$directorio->getArchivosHijos();
}
}
$hydrator = new Reflection;
return new JsonModel($hydrator->extract($directorio));
}

The Entity Directorio

<?php
namespace Directorios\Entity;

use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Zend\Form\Annotation as ZendAnnotation;
use Directorios\Model\ArchivoInterface;
use Doctrine\Common\Collections\ArrayCollection;


/**
 * @ORM\Entity
 * @ORM\Table (name="directorio")
 *
 */

class Directorio
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     * @ZendAnnotation\Exclude()
     * @var int|null
     */
    private $id;

    /**
     * @ORM\Column(type="string")
     * @var string
     * @Required
     * @ZendAnnotation\Filter({"name":"StringTrim"})
     * @ZendAnnotation\Validator({"name":"StringLength", "options":{"min":1, "max":60}})
     * @ZendAnnotation\Validator({"name":"Regex", "options":{"pattern":"/^[a-zA-Z][a-zA-Z0-9_-]{0,24}$/"}})
     * @ZendAnnotation\Attributes({"type":"text"})
     * @ZendAnnotation\Options({"label":"Nombre:"})
     */

    private $nombre;

    /**
     * @ORM\ManyToOne(targetEntity="Directorios\Entity\Directorio", inversedBy="directoriosHijos")
     * @ORM\JoinColumn(name="padre", referencedColumnName="id")
     */

    private $padre;

    /**
     * @ORM\OneToMany(targetEntity="Directorios\Entity\Directorio", mappedBy="padre",cascade={"persist", "remove"})
     */

    private $directoriosHijos;

    /**
     * @ORM\OneToMany(targetEntity="Directorios\Entity\Archivo", mappedBy="padre", cascade={"persist", "remove"})
     */

    private $archivosHijos;


    /**
     * @ORM\Column(type="datetime")
     * @var \DateTime
     */
    private $fechaCreacion;

    /**
     * @ORM\Column(type="datetime")
     * @var \DateTime
     */

    private $fechaModificacion;

    /**
     * @ORM\Column(name="ruta_real")
     * @ORM\Column(type="text")
     */
    private $ruta_real;

    /**
     * 
     * @ORM\Column(type="integer")
     * 
     */

    private $estado=0;

    /**
     *
     * @ORM\Column(type="integer")
     *
     */

    private $tipo;

//....... methods

    public function __construct(){
        $this->archivosHijos=new ArrayCollection();
        $this->directoriosHijos=new ArrayCollection();
    }

}

The JSON response:

id  2
nombre  "Nuevo directorio"
padre   Object <- Returns Objects
directoriosHijos    Object <- Returns Objects
archivosHijos   Object <- Returns Objects
fechaCreacion   
date    "2017-09-09 21:23:20.000000"
timezone_type   3
timezone    "Europe/Berlin"
fechaModificacion   
date    "2017-09-09 21:23:20.000000"
timezone_type   3
timezone    "Europe/Berlin"
ruta_real   "D:\\testDirectorioRaiz"
estado  0
tipo    0

The Objects relationated come like Object not like Array(). How i can do that relationated objects arrives like Json Array() too?

  • 写回答

2条回答 默认 最新

  • douxun3496 2017-09-10 13:05
    关注

    The Reflection Hydrator itself does not allow nested hydration/extraction. However Hydrator Aggregates do so, but you have to invest a bit more work into it then just simply instantiating it. If you choose this route I would invest a bit more time and injecting it into the controller in order to keep testability high

    Also consider using the Doctrine Hydrator provided by the doctrine/doctrine-module composer package. The project also has a short documentation on hydration

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 我想在一个软件里添加一个优惠弹窗,应该怎么写代码
  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流