Php 使用composedObject和条令保存相关对象
我在尝试使用doctrine和composedobject保存相关数据时遇到问题 这些是我的实体 这是我的员工实体Php 使用composedObject和条令保存相关对象,php,doctrine-orm,zend-framework2,Php,Doctrine Orm,Zend Framework2,我在尝试使用doctrine和composedobject保存相关数据时遇到问题 这些是我的实体 这是我的员工实体 <?php namespace Nomina\Entity; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\Mapping as ORM; use Zend\Form\Annotation; use Nomina\Entity\EmpleadosDetalles; /** * Emp
<?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;
}
}
我没有收到任何错误,它保存在表中的员工,但是没有填充详细信息表。我遗漏了什么?可能是您的问题在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();
检查是否有效。
至少你知道问题是在表单验证/筛选中还是在实体定义的其他地方。你确定$empleado->getDetalles()不是空的吗?它是空的,这是我在print\r doctor\Common\Collections\ArrayCollection对象([\u elements:doctor\Common\Collections\ArrayCollection:private]=>数组中得到的结果( ) )
var_dump($request->getPost());
$empleado = new Empleado();
$hydrator = new DoctrineHydrator($entityManager,'Nomina\Entity\Empleado');
$data = $request->getPost()
$empleado = $hydrator($data, $empleado);
$entityManager->persist($empleado);
$entityManager->flush();