Display values ​​in a folded object using form annotations and DoctrineORM in ZF2

Well, I have a problem using structObject, where with regard to OneToMany the edit form does not display the values ​​of my object, I assume that this is because my other class returns a collection

This is my first class containing my folded object

<?php 
 namespace Nomina\Entity\Empleado;
use Doctrine\Common\Collections\ArrayCollection;

use Doctrine\ORM\Mapping as ORM;
use Zend\Form\Annotation;


/**
 * Empleado2
 *
 * @ORM\Table(name="empleado2")
 * @ORM\Entity
 * @Annotation\Hydrator("Zend\Stdlib\Hydrator\ObjectProperty")
 * @Annotation\Name("EmpleadoForm")
 */
class Empleado2
{
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer", nullable=false)
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="nombre", type="string", length=255, nullable=true)
 * @Annotation\Type("Zend\Form\Element\Text")
 * @Annotation\Required({"required":"true"})
 * @Annotation\Filter({"name":"StripTags"})
 * @Annotation\Options({"label":"Numero de empleado"})
 * @Annotation\Attributes({"class":"form-control"})
 */
private $nombre;

/**
 * @ORM\OneToMany(targetEntity="Nomina\Entity\Empleado\EmpleadoDetalles2", mappedBy="empleado")
 * @Annotation\ComposedObject("Nomina\Entity\Empleado\EmpleadoDetalles2",is_collection=true,options={"count":1})
 */
private $detalles;

/**
  * Annotation\ComposedObject("Nomina\Entity\Empleado\EmpleadoDetalles2")
  */
private $detalle;

/**
 * @Annotation\Type("Zend\Form\Element\Submit")
 * @Annotation\Attributes({"value":"Procesar"})
 * @Annotation\Attributes({"class":"btn btn-primary"})
 */
public $submit;

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

public function addDetalles(Collection $detalles)
{
    foreach ($detalles as $detalle) {
        $detalle->setEmpleado($this);
        $this->detalles->add($detalle);
    }
}

public function removeDetalles(Collection $detalles)
{
    foreach ($detalles as $detalle) {
        $detalle->setEmpleado(null);
        $this->detalles->removeElement($detalle);
    }
}

public function getDetalles()
{
    return $this->detalles;
}
.....

And this is my class, which can be many

<?php
 namespace Nomina\Entity\Empleado;
 use Nomina\Entity\Empleado\Empleado2;

 use Doctrine\ORM\Mapping as ORM;
 use Zend\Form\Annotation;
 /**
  * EmpleadoDetalles
 *
 * @ORM\Table(name="empleadoDetalles2")
 * @ORM\Entity
 * @Annotation\Hydrator("Zend\Stdlib\Hydrator\ObjectProperty")
 * @Annotation\Name("EmpleadoDetallesForm")
 */
 class EmpleadoDetalles2
 { 
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer", nullable=false)
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
 private $id;

/**
 * @var string
 * 
 * @ORM\Column(name="salario", type="decimal", precision=10, scale=2, nullable=false)
 * @Annotation\Type("Zend\Form\Element\Text")
 * @Annotation\Required({"required":"true"})
 * @Annotation\Filter({"name":"StripTags"})
 * @Annotation\Options({"label":"Salario Diario"})
 * @Annotation\Attributes({"class":"form-control"})
 */
private $salario = '0.00';

/**
 * @var string
 *
 * @ORM\Column(name="banco", type="string", length=255, nullable=true)
 * @Annotation\Type("Zend\Form\Element\Text")
 * @Annotation\Required({"required":"true"})
 * @Annotation\Filter({"name":"StripTags"})
 * @Annotation\Options({"label":"Banco"})
 * @Annotation\Attributes({"class":"form-control"})
 */
private $banco;

/**
 * @ORM\Column(name="idEmpleado", type="integer", length=11, nullable=false)
 * Annotation\Type ("Zend\Form\Element\Hidden")
 */
private $idEmpleado;

/**
 * @ORM\ManyToOne(targetEntity="Nomina\Entity\Empleado\Empleado2", inversedBy="detalles")
 * @ORM\JoinColumn(name="idEmpleado", referencedColumnName="id")
 * Annotation\Type ("Zend\Form\Element\Hidden")
 */
private $empleado;




/**
 * Allow null to remove association
 *
 * @param Empleado2 $empleado
 */
public function setEmpleado(Empleado2 $empleado = null)
{
    $this->empleado = $empleado;
}

/**
 * @return Empleado2
 */
public function getEmpleado()
{
    return $this->empleado;
}

I have it in my opinion

echo $this->formRow($form->get('nombre'));
echo $this->formRow($form->get('detalles')->get('salario'));
echo $this->formRow($form->get('detalles')->get('banco'));

I correctly get the field from the database, but I don’t get anything in the folded object, which in my case is "detalles", I think this is because detalles can be many objects of the same type, m not sure how to make it work.

+4
1

.

:

/**
  * @ORM\OneToMany(targetEntity="Nomina\Entity\Empleado\EmpleadoDetalles2", mappedBy="empleado")
  * @Annotation\ComposedObject("Nomina\Entity\EmpleadoEmpleadoDetalles2",is_collection=true,options={"count":1})
  */

/**
  * @ORM\OneToMany(targetEntity="Nomina\Entity\Empleado\EmpleadoDetalles2", mappedBy="empleado")
  * @Annotation\ComposedObject({"target_object":"Nomina\Entity\EmpleadoEmpleadoDetalles2",
  *                             "is_collection":"true",
  *                             "options":{"count":1} })
  */

"@Annotations\ComposedObject" , , .

"count": 1 "count" , "": "-" , , .

, , , ....

+1

Source: https://habr.com/ru/post/1537553/


All Articles