user973783
user973783

Reputation: 111

Render values in a composedObject using form annotations and DoctrineORM in ZF2

OK, I'm having a problem using a composedObject where in a OneToMany relation, the edit form is not rendering the values of my entity, I asume that it's because my other class is returning a collection

This is my first class that contains my composed 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 this in my view

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

The I am getting the field from the database correctly, but not I am getting nothing in the composedObject, which in my case is "detalles", I think it's because detalles can be many objects of the same type, but I'm not sure how to make it work

Upvotes: 4

Views: 887

Answers (1)

tigs001
tigs001

Reputation: 146

Only a partial answer so far.

The syntax of your entry:

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

Needs to be

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

The "@Annotations\ComposedObject" annotation can either take a string, for the target class name, or it takes an array as I've shown above.

I could not get the "count":1 or any "count" to work, but if you put "label":"something" in your options sub-array, you can see that it is being read.

Now once I successfully specified my Composed Object was a collection, I started receiving other errors....

Upvotes: 1

Related Questions