在Symfony 2中创建一个包含2个实体的表单


Create a Form with 2 entities in Symfony 2

如何创建具有2个实体的表单我有两个实体:

实体/Client.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;
    }
}

实体/联系人.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)

nombre(Contacto实体中的nombre)

idcliente(Contacto实体中的idcliente)(与客户端实体相同的存储值)

idcliente是唯一的

关系

我对树枝形式的概念是这样的:

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