Forms 一对多关系(symfony2)中的外键字段为空

Forms 一对多关系(symfony2)中的外键字段为空,forms,symfony,one-to-many,Forms,Symfony,One To Many,我有两个具有一对多关系的表。我的代码工作正常,表单使用交互模式添加详细信息,但是当我提交它时,除了外键id(即null)之外的所有内容都会被保存。因此,次表上的数据与主表不存在关系。问题在哪里 这是我的主表(只有一些属性): 一般来说,如果答案中包含对代码意图的解释,以及为什么在不引入其他代码的情况下解决问题,那么答案会更有帮助。(这篇文章至少有一个用户标记过,大概是因为他们认为应该删除没有解释的答案。)一般来说,如果答案中包含了对代码意图的解释,以及在不介绍其他人的情况下解决问题的原因,那么答

我有两个具有一对多关系的表。我的代码工作正常,表单使用交互模式添加详细信息,但是当我提交它时,除了
外键id
(即
null
)之外的所有内容都会被保存。因此,次表上的数据与主表不存在关系。问题在哪里

这是我的主表(只有一些属性):


一般来说,如果答案中包含对代码意图的解释,以及为什么在不引入其他代码的情况下解决问题,那么答案会更有帮助。(这篇文章至少有一个用户标记过,大概是因为他们认为应该删除没有解释的答案。)一般来说,如果答案中包含了对代码意图的解释,以及在不介绍其他人的情况下解决问题的原因,那么答案会更有帮助。(这篇文章至少有一个用户标记过,大概是因为他们认为应该删除没有解释的答案。)一般来说,如果答案中包含了对代码意图的解释,以及在不介绍其他人的情况下解决问题的原因,那么答案会更有帮助。(这篇文章至少有一个用户标记过,大概是因为他们认为应该删除没有解释的答案。)一般来说,如果答案中包含了对代码意图的解释,以及在不介绍其他人的情况下解决问题的原因,那么答案会更有帮助。(这篇文章至少有一个用户标记过,大概是因为他们认为应该删除没有解释的答案。)
    <?php

namespace Alg\NormasBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Requisitos
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Alg\NormasBundle\Entity\RequisitosRepository")
 */
class Requisitos
{
    /**
     * @var integer
     *
     * @ORM\Column(name="idrequisito", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;



    /**
     * @ORM\OneToMany(targetEntity="RequisitosDetalle", mappedBy="requisito", cascade={"persist", "remove"})
     */
    private $detalle;

    public function __construct() {
        $this->detalle = new \Doctrine\Common\Collections\ArrayCollection();
    }

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


    /**
     * Add detalle
     *
     * @param \Alg\NormasBundle\Entity\RequisitosDetalle $detalle
     * @return Requisitos
     */
    public function addDetalle(\Alg\NormasBundle\Entity\RequisitosDetalle $detalle)
    {
        $this->detalle[] = $detalle;

        return $this;
    }

    /**
     * Remove detalle
     *
     * @param \Alg\NormasBundle\Entity\RequisitosDetalle $detalle
     */
    public function removeDetalle(\Alg\NormasBundle\Entity\RequisitosDetalle $detalle)
    {
        $this->detalle->removeElement($detalle);
    }

    /**
     * Get detalle
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getDetalle()
    {
        return $this->detalle;
    }
}
    <?php

namespace Alg\NormasBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * RequisitosDetalle
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Alg\NormasBundle\Entity\RequisitosDetalleRepository")
 */
class RequisitosDetalle
{
    /**
     * @var integer
     *
     * @ORM\Column(name="iddetalle", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="Requisitos", inversedBy="detalle")
     * @ORM\JoinColumn(name="idrequisito", referencedColumnName="idrequisito")
     */
    private $requisito;

    /**
     * @var string
     *
     * @ORM\Column(name="tema", type="text")
     */
    private $tema;

    /**
     * @var string
     *
     * @ORM\Column(name="disposicion", type="text")
     */
    private $disposicion;

    /**
     * @var string
     *
     * @ORM\Column(name="articulos", type="text")
     */
    private $articulos;

    /**
     * @var string
     *
     * @ORM\Column(name="aplicacion", type="text", nullable=true)
     */
    private $aplicacion;

    /**
     * @var string
     *
     * @ORM\Column(name="evidencia", type="text", nullable=true)
     */
    private $evidencia;

    /**
     * @ORM\ManyToOne(targetEntity="Cargos")
     * @ORM\JoinColumn(name="idcargo", referencedColumnName="idcargo")
     */
    private $cargo;



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

    /**
     * Set tema
     *
     * @param string $tema
     * @return Requisitos
     */
    public function setTema($tema)
    {
        $this->tema = $tema;

        return $this;
    }

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

    /**
     * Set disposicion
     *
     * @param string $disposicion
     * @return Requisitos
     */
    public function setDisposicion($disposicion)
    {
        $this->disposicion = $disposicion;

        return $this;
    }

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

    /**
     * Set articulos
     *
     * @param string $articulos
     * @return Requisitos
     */
    public function setArticulos($articulos)
    {
        $this->articulos = $articulos;

        return $this;
    }

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

    /**
     * Set aplicacion
     *
     * @param string $aplicacion
     * @return Requisitos
     */
    public function setAplicacion($aplicacion)
    {
        $this->aplicacion = $aplicacion;

        return $this;
    }

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

    /**
     * Set evidencia
     *
     * @param string $evidencia
     * @return Requisitos
     */
    public function setEvidencia($evidencia)
    {
        $this->evidencia = $evidencia;

        return $this;
    }

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

    public function addRequisito(\Alg\NormasBundle\Entity\Requisitos $requisito)
    {
        if (!$this->requisito->contains($requisito)) {
            $this->requisito->add($requisito);
        }
    }

    /**
     * Set requisito
     *
     * @param \Alg\NormasBundle\Entity\Requisitos $requisito
     * @return Requisitos
     */
    public function setRequisito(\Alg\NormasBundle\Entity\Requisitos $requisito = null)
    {
        $this->requisito = $requisito;

        return $this;
    }

    /**
     * Get requisito
     *
     * @return \Alg\NormasBundle\Entity\Requisitos 
     */
    public function getRequisito()
    {
        return $this->requisito;
    }



    /**
     * Set cargo
     *
     * @param \Alg\NormasBundle\Entity\Cargos $cargo
     * @return Requisitos
     */
    public function setCargo(\Alg\NormasBundle\Entity\Cargos $cargo = null)
    {
        $this->cargo = $cargo;

        return $this;
    }

    /**
     * Get cargo
     *
     * @return \Alg\NormasBundle\Entity\Cargos 
     */
    public function getCargo()
    {
        return $this->cargo;
    }
}
    <?php

namespace Alg\NormasBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
//use Doctrine\ORM\EntityRepository;

class abmRequisitoDetalleType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {

        $builder->add('tema', null, array('label' => 'Tema:'))
                ->add('disposicion', null, array('label' => 'Disposición:'))
                ->add('articulos', null, array('label' => 'Artículos:'));
                //->add('aplicacion', null, array('label' => 'Aplicación:'))
                //->add('evidencia', null, array('label' => 'Evidencia:'));
        //  ->add('cargo', 'entity', array('label' => 'Disposición:'));

    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Alg\NormasBundle\Entity\RequisitosDetalle'
        ));
    }

    public function getName()
    {
        return 'fdetalle';
    }
}
    <?php

namespace Alg\NormasBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Alg\NormasBundle\Form\Type\abmRequisitoDetalleType;

class abmRequisitoType extends AbstractType
{


    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('fecha', null, array('label' => 'Fecha Redacción:',
                                                'widget' => 'single_text'))
                ->add('titulo', null, array('label' => 'Título Documento:'));

        $builder->add('detalle', 'collection', array(
                                'type'           => new abmRequisitoDetalleType(),
                                'label'          => 'Detalle de Requisitos Legales:',
                                'by_reference'   => false,
                                'allow_delete'   => true,
                                'allow_add'      => true,
                                'attr'           => array('class' => 'elDetalle')
                    )   );

    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Alg\NormasBundle\Entity\Requisitos',
            'cascade_validation' => true
        ));
    }

    public function getName()
    {
        return 'frequisito';
    }
}
public function newAction()
{
    $request = $this->getRequest();
    $repository = $this->getDoctrine()->getManager();

        //nuevo
        $requisito = new Requisitos();
        $requisito->setFecha(new \DateTime('now'));
    $form = $this->createForm(new abmRequisitoType(), $requisito);
    //-- En caso de que el request haya sido invocado por POST procesaremos el formulario
    if($request->getMethod() == 'POST')
    {
        //-- Pasamos el request al método bindRequest() del objeto formulario el cual obtiene los datos del formulario
        // y los carga dentro del objeto fhallazgo1 que está contenido también dentro del objeto Type
        $form->bindRequest($request);
        //-- Con esto nuestro formulario ya es capaz de decirnos si los datos son válidos o no 
        if($form->isValid())
        {
            $requisito->setEstado('1');
            $repository->persist($requisito);
            $repository->flush();

            return $this->redirect($this->generateURL('alg_normas_requisitoelab'));
        }
    }
    // Ingresamos al formulario
    return $this->render('AlgNormasBundle:Requisitos:new.html.twig', 
                            array('form' => $form->createView(), 
                                'requisito' => $requisito ));
}
public function addDetalle(\Alg\NormasBundle\Entity\RequisitosDetalle $detalle)
{
    $this->detalle[] = $detalle;

    $detalle->setRequisitos($this); // *** Add this

    return $this;
}
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/

public function processPre()
{
    foreach ($this->detalle as $key => $obj) {
        if ($obj->getRequisitos() === null) {
            $obj->setRequisitos($this);
            $this->detalle[$key] = $obj;
        }
    }
}