简体   繁体   中英

Create a Form with 2 entities in Symfony 2

How to create a form with 2 entities I have 2 entities:

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

I would like to create one form to enter the following data:

nombre (nombre in Cliente entity)

nombre (nombre in Contacto entity)

idcliente (idcliente in Contacto entity)(same stored value that the client entity)

The idcliente is unique

Relationship

My concept of form in twig is 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) }}

My concept in controller is 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);
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM