2016-04-12 12 views
0

Wie ein Formular mit 2 Einheiten erstellen Ich habe 2 Einheiten:ein Formular mit 2 Einheiten in Symfony 2

Entity/Cliente.php

class Cliente 
{ 
    /** 
    * @var int 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @var int 
    * 
    * @ORM\Column(name="idcliente", type="integer") 
    */ 
    private $idcliente; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="nombre", type="string", length=100) 
    */ 
    private $nombre; 

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

    /** 
    * Set idcliente 
    * 
    * @param integer $idcliente 
    * 
    * @return Cliente 
    */ 
    public function setIdcliente($idcliente) 
    { 
     $this->idcliente = $idcliente; 

     return $this; 
    } 

    /** 
    * Get idcliente 
    * 
    * @return int 
    */ 
    public function getIdcliente() 
    { 
     return $this->idcliente; 
    } 

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

     return $this; 
    } 

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

Entity/Contacto.php

class Contacto 
{ 
    /** 
    * @var int 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @var int 
    * 
    * @ORM\Column(name="idcliente", type="integer") 
    */ 
    private $idcliente; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="nombre", type="string", length=50) 
    */ 
    private $nombre; 

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

    /** 
    * Set idcliente 
    * 
    * @param integer $idcliente 
    * 
    * @return Contacto 
    */ 
    public function setIdcliente($idcliente) 
    { 
     $this->idcliente = $idcliente; 

     return $this; 
    } 

    /** 
    * Get idcliente 
    * 
    * @return int 
    */ 
    public function getIdcliente() 
    { 
     return $this->idcliente; 
    } 

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

     return $this; 
    } 

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

ich möchte ein Formular erstellen folgende Daten eingeben:

nombre (nombre in Cliente Entität)

nombre (nombre in Contacto Entität)

idcliente (idcliente in Contacto Entity) (gleichen gespeicherten Wert, dass die Client-Entität)

+0

Hat '$ auf die' Cliente' idcliente' zu ​​beziehen? Wenn dies der Fall ist, müssen Sie also anfangen, an Objekte zu denken, nicht an Datenbank-IDs. So hat "Contacto" einen "Cliente", nicht einfach eine "ID". Es ist ein bisschen unklar von Ihrem Beispiel, was Sie zu tun versuchen - vielleicht würde ein Diagramm helfen? – iLikeBreakfast

+0

@iLikeBreakfast irgendwelche Lösungen für wenn? Vielen Dank! –

Antwort

0

Die idcliente ist einzigartig

Relationship

Mein Konzept in Zweig der Form ist so:

{{ form_start(form) }} 
<p>Nombre: {{ form_widget(form.nombre) }}</p> 
<p>Nombre Contacto: {{ form_widget(form.nombrecontacto) }}</p> 
{{ form_widget(form.Guardar) }} 
{{ form_end(form) }} 

in Controller Mein Konzept ist so:

public function inicioAction(Request $request) 
    { 
     $em = $this->getDoctrine()->getManager(); 

     $clientes = $em->createQuery("SELECT DISTINCT CLI.idcliente 
         FROM GeneralBundle:Cliente CLI 
         WHERE CLI.idcliente = CON.idcliente)->getResult(); 
     $numclientes = count($clientes); 
     $idnew = $numclientes+1; 
     $cliente = new Cliente(); 
     $contacto = new Contacto(); 

     $form = $this->createform(**¿¿ Here is the question ??**); 

     if ($form->isValid()) 
     { 
      $em = $this->getDoctrine()->getManager(); 
      $cliente->setIdcliente($idnew); 
      $em->persist($cliente); 
      $em->flush(); 

      $em = $this->getDoctrine()->getManager(); 
      $contacto->setIdcliente($idnuevo); 
      $em->persist($contacto); 
     } 
    }