2016-04-12 13 views
0

で2つのエンティティとフォームを作成、私は2つの実体があります2つのエンティティ でフォームを作成する方法のSymfony 2

エンティティ/ 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; 
    } 
} 

エンティティ/ 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; 
    } 
} 

私は、次のデータを入力するために、1つのフォームを作成したいと思います:

ノンブル(ノンブルをClienteエンティティ内)

ノンブル(ノンブル連絡先ヘルプエンティティ内)

idcliente(連絡先ヘルプエンティティ内idcliente)(同じ格納された値クライアントエンティティこと)

+0

は、$は 'Cliente'を参照してくださいidcliente''していますか?そうであれば、データベースIDではなく、オブジェクトの観点から考える必要があります。したがって、 'Contacto'は単なる" id "ではなく、' Cliente'を持っています。あなたの例から、あなたがやろうとしていることは少し不明です。おそらく、ダイアグラムは助けになるでしょうか? – iLikeBreakfast

+0

@iLikeBreakfast if for any solutions?ありがとう! –

答えて

0

idclienteが一意

Relationship

あります

私の枝での私のコンセプトはそうです:

{{ 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) }} 
コントローラで

私の考え方はそうです:

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