使用composedObject和doctrine保存相关对象

时间:2014-04-16 21:31:34

标签: php doctrine-orm zend-framework2

我在使用doctrine和composobject

尝试保存相关数据时遇到了麻烦

这些是我的实体

这是我的员工实体

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

use Doctrine\ORM\Mapping as ORM;
use Zend\Form\Annotation;
use Nomina\Entity\EmpleadosDetalles;


/**
 * Empleado
 *
 * @ORM\Table(name="empleado")
 * @ORM\Entity
 * @Annotation\Hydrator("Zend\Stdlib\Hydrator\ObjectProperty")
 * @Annotation\Name("EmpleadoForm")
 */
class Empleado
{
/**
 * @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=false)
 * @Annotation\Type("Zend\Form\Element\Text")
 * @Annotation\Required({"required":"true"})
 * @Annotation\Filter({"name":"StripTags"})
 * @Annotation\Options({"label":"Nombre"})
 * @Annotation\Attributes({"class":"form-control"})
 * @Annotation\Validator({"name":"NotEmpty","options":{"messages":{"isEmpty":"no debe estar vacio"}}})
 */
private $nombre;

/**
 * @var \EmpleadosDetalles $detalles
 * @ORM\OneToMany(targetEntity="Nomina\Entity\EmpleadosDetalles", mappedBy="empleado", cascade={"persist"})
 * @Annotation\ComposedObject("Nomina\Entity\EmpleadosDetalles")
 */
private $detalles;

/**
 * @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 getDetalles() {
    return $this->detalles;
}

/**
 * Set nombre
 *
 * @param string $nombre
 * @return Empleado
 */
public function setNombre($nombre)
{
    $this->nombre = $nombre;

    return $this;
}

/**
 * Get nombre
 *
 * @return string 
 */
public function getNombre()
{
    return $this->nombre;
}
}

这是我的详细信息实体,属于员工

<?php
namespace Nomina\Entity;

use Doctrine\ORM\Mapping as ORM;
use Zend\Form\Annotation;
/**
 * EmpleadosDetalles
 *
 * @ORM\Table(name="empleadosDetalles", indexes={@ORM\Index(name="fk_empleado", columns={"idEmpleado"})})
 * @ORM\Entity
 * @Annotation\Hydrator("Zend\Stdlib\Hydrator\ObjectProperty")
 * @Annotation\Name("EmpleadoDetallesForm")
 */
class EmpleadosDetalles
{
/**
 * @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="int", nullable=false)
 * @Annotation\Type("Zend\Form\Element\Text")
 * @Annotation\Required({"required":"true"})
 * @Annotation\Filter({"name":"StripTags"})
 * @Annotation\Options({"label":"Salario"})
 * @Annotation\Attributes({"class":"form-control"})
 * @Annotation\Validator({"name":"NotEmpty","options":{"messages":{"isEmpty":"no debe estar vacio"}}})
 */
private $salario = '0';

/**
 * @var string
 *
 * @ORM\Column(name="numero", type="string", length=20, nullable=true)
 * @Annotation\Type("Zend\Form\Element\Text")
 * @Annotation\Required({"required":"true"})
 * @Annotation\Filter({"name":"StripTags"})
 * @Annotation\Options({"label":"Numero"})
 * @Annotation\Attributes({"class":"form-control"})
 * @Annotation\Validator({"name":"NotEmpty","options":{"messages":{"isEmpty":"no debe estar vacio"}}})
 */
private $numero;


/**
* @ORM\Column(name="idEmpleado", type="integer", length=11, nullable=false)
*/
private $idEmpleado;

/**
 * @ORM\ManyToOne(targetEntity="Nomina\Entity\Empleado", inversedBy="detalles")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="idEmpleado", referencedColumnName="id")
 * })
 */
 protected $empleado;

/**
 * Set salario
 *
 * @param integer $salario
 * @return EmpleadosDetalles
 */
public function setSalario($salario)
{
    $this->salario = $salario;

    return $this;
}

/**
 * Get salario
 *
 * @return integer 
 */
public function getSalario()
{
    return $this->salario;
}

/**
 * Set numero
 *
 * @param string $numero
 * @return EmpleadosDetalles
 */
public function setNumero($numero)
{
    $this->numero = $numero;

    return $this;
}

/**
 * Get numero
 *
 * @return string 
 */
public function getNumero()
{
    return $this->numero;
}


public function setEmpleado(Empleado $empleado)
{
    $this->empleado = $empleado;
    return $this;
}

public function getEmpleado()
{
    return $this->empleado;
}

}

这是我的控制器动作

public function addAction() {
    $view = new ViewModel();
    $empleado = new Empleado();

    $entityManager = $this->entityManager();
    $builder = new DoctrineAnnotationBuilder($entityManager);
    $form = $builder->createForm($empleado);

    $request = $this->getRequest();
    if ($request->isPost()) {
        $form->bind($empleado);
        $form->setHydrator(new DoctrineHydrator($entityManager,'Nomina\Entity\Empleado'));
        $form->setValidationGroup('nombre');
        $form->setData($request->getPost());
        if ($form->isValid()) {
            $entityManager->persist($empleado);
            $entityManager->flush();
            $this->redirect()->toRoute('empleado');
        } else {
            $view->messages = $form->getMessages();
        }
    }
    $view->form = $form;
    return $view;
}

我没有收到任何错误,该员工已将其保存在表格中,但详细信息表格未填充。我错过了什么?

1 个答案:

答案 0 :(得分:0)

可能是你的问题在inputFilter / validator中。你的@annotation验证/过滤有问题,因此inputFilter正在过滤$detalles的所有数据,这意味着只有一个空数组可以保湿。

首先检查所有数据是否在您的帖子中:

var_dump($request->getPost());

然后尝试水合一次,而不使用表单inputFilter进行过滤/验证。 像这样:

$empleado = new Empleado();
$hydrator = new DoctrineHydrator($entityManager,'Nomina\Entity\Empleado');
$data = $request->getPost()
$empleado = $hydrator($data, $empleado);
$entityManager->persist($empleado);
$entityManager->flush();

检查是否有效。 至少你知道问题是在你的表单验证/过滤中还是在你的实体定义中的其他地方。

相关问题