Ver Mensaje Individual
  #5 (permalink)  
Antiguo 11/04/2016, 13:52
manupucelano
 
Fecha de Ingreso: abril-2016
Mensajes: 3
Antigüedad: 8 años
Puntos: 0
Respuesta: Formulario para manejar dos entidades

Si hasta hay todo bien!
Pero el problema es creas un nuevo formulario o como lo haces?
Mi idea del tema sería algo así?

Entidad Cliente
Código:
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;
    }
}
Entidad Contacto
Código:
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;
    }

}
Formulario Nuevo
Código:
namespace GeneralBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class NuevoFormType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('idcliente', 'hidden')
            ->add('nombre', 'text')
            ->add('nombrecontacto', 'text')
            ->add('Guardar','submit')
        ;
    }
    
    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'GeneralBundle\Entity\Contacto'
        ));
    }
}
CONTROLLER
Código:
public function inicioAction(Request $request)
{
	$em = $this->getDoctrine()->getManager();

	$cliente = new Cliente();
	$contacto = new Contacto();
	$form = $this->createform(new NuevoFormType(),$cliente);
}
Twig
Código:
{{ form_start(form) }}
<p>Nombre del Cliente: {{ form_widget(form.nombre) }}</p>
<p>Nombre del Contacto: {{ form_widget(form.nombrecontacto) }}</p>
{{ form_widget(form.Guardar) }}
{{ form_end(form) }}
El problema y hay la duda, es como estableces el Form para relacionar las 2 entidades, y a la vez, a través del controlador guarde los nombres(Entidad Cliente y Contacto) y el ID de cliente, que también se guardará en Contacto, como IDCliente.

Última edición por manupucelano; 11/04/2016 a las 13:58