2016-04-12 116 views
0

創建窗體如何創建帶有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; 
    } 
} 

我想創建一個表單來輸入以下數據:

nombre(nombre在Cliente實體)

NOMBRE(NOMBRE在CONTACTO實體)

idcliente(idcliente在CONTACTO實體)(相同的存儲的值,所述客戶端實體)

+0

是否'$ idcliente'參考'Cliente'?如果是這樣,你需要這樣開始思考對象,而不是數據庫ID。所以'Contacto'有一個'Cliente',不只是一個「id」。你的例子有點不清楚你想要做什麼 - 也許圖表會有一些幫助? – iLikeBreakfast

+0

@iLikeBreakfast if的任何解決方案?謝謝! –

回答

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