2013-11-01 61 views
0

我有兩個表格,one to many關係。我的代碼工作正常,表單使用交互模式添加詳細信息,但是當我提交時,除foreign key idnull)之外的所有內容均已保存。因此,輔助表上的數據與主表不存在關係。哪裏有問題?外鍵字段在一對多關係中爲null(symfony2)

這是我的主表(僅一些ATTR):

<?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)); 
} 

回答

2
public function addDetalle(\Alg\NormasBundle\Entity\RequisitosDetalle $detalle) 
{ 
    $this->detalle[] = $detalle; 

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

    return $this; 
} 
+0

的工作,謝謝! – vixstor

1
/** 
* @ORM\PrePersist() 
* @ORM\PreUpdate() 
*/ 

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

通常情況下,如果答案包含解釋代碼意圖做什麼的解釋,以及爲什麼解決問題而不介紹其他問題,答案會更有幫助。 (這篇文章被至少一個用戶標記,大概是因爲他們認爲沒有解釋的答案應該被刪除。) –

相關問題