嵌入表单,然后保留在表中

Embed forms and then persist in the tables

几天前,我一直在为这个问题苦苦挣扎,不知道如何解决!!!

控制器

public function crearUsuarioDatosAction()
    {
        $usuario = new User();
        $datos = new Datos();
        $datos->setPersonales($usuario);
        $request = $this->getRequest();
        $form = $this->createForm(new UserType(), $usuario);
        $form->add('datos', new DatosType());


        if($request->getMethod() == 'POST')
            {
                        $form->bind($request);

                if($form->isValid())
                    {
                        $em = $this->getDoctrine()->getEntityManager();
                        $em->persist($form);
                        $em->flush();



                    }

                return $this->redirect($this->generateUrl('home'));

            }

            return $this->render('AtajoBundle:IngresarValores:crearUsuarioDatos.html.twig', array('form' => $form->createView()));

    }

我的实体用户

    <?php

namespace Proyecto\LavocBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * User
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Proyecto\LavocBundle\Entity\UserRepository")
 */
class User {

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="usuario", type="string", length=30)
     */
    private $usuario;

    /**
     * @var string
     *
     * @ORM\Column(name="pass", type="string", length=20)
     */
    private $pass;

/**
     *  @ORM\OneToOne(targetEntity="Datos", mappedBy="personales")
     */

    private $datos;

/**
     * Get datos
     *
     * @return string
     */

    public function getDatos()
    {
        return $this->datos;
    }

    /**
     * Set datos
     *
     * @param string $datos
     * @return User
     */

    public function setDatos($datos)
    {
        $this->datos = $datos;
        $datos->setPersonales($this);
        return $this;
    }

现在,我的实体数据:

    <?php

namespace Proyecto\LavocBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Datos
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Proyecto\LavocBundle\Entity\DatosRepository")
 */
class Datos
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="empresa", type="string", length=50)
     */
    private $empresa;

    /**
     * @var integer
     *
     * @ORM\Column(name="cuit", type="integer")
     */
    private $cuit;

    /**
     * @var string
     *
     * @ORM\Column(name="localidad", type="string", length=50)
     */

    private $localidad;

    /**
     * @var string
     *
     * @ORM\Column(name="calle", type="string", length=40)
     */
    private $calle;

    /**
     * @var integer
     *
     * @ORM\Column(name="altura", type="integer")
     */
    private $altura;

    /**
     * @var integer
     *
     * @ORM\Column(name="areaTel", type="integer")
     */
    private $areaTel;

    /**
     * @var integer
     *
     * @ORM\Column(name="telefono", type="integer")
     */
    private $telefono;

    /**
     * @var integer
     *
     * @ORM\Column(name="areaCel", type="integer")
     */
    private $areaCel;

    /**
     * @var integer
     *
     * @ORM\Column(name="celular", type="integer")
     */
    private $celular;

    /**
     * @var string
     *
     * @ORM\Column(name="email", type="string")
     */

    private $email;

    /**
    * @ORM\OneToOne(targetEntity="User", inversedBy="datos")
    * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
    */

    private $personales;

    /**
     * Get id
     *
     * @return integer 
     */

/**
     * Set personales
     *
     * @param string $personales
     * @return Datos
     */

    public function setPersonales($personales)
    {
        $this->personales = $personales;
        return $this;

    }

    /**
     * Get personales
     *
     * @return string
     */

    public function getPersonales()
    {
        return $this->personales;
    }

用户类型

<?php

namespace Proyecto\LavocBundle\Form;

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

class UserType extends AbstractType 
{
    public function buildForm(FormBuilderInterface $builder, array $options) 
    {
        $builder->add('usuario');
        $builder->add('pass');
    }

    public function getName() 
    {
        return 'user_form';
    }
}

数据类型:

    <?php

namespace Proyecto\LavocBundle\Form;

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

class DatosType extends AbstractType {

    public function buildForm(FormBuilderInterface $builder, array $options) 
    {
        $builder->add('empresa');
        $builder->add('cuit');
        $builder->add('localidad');
        $builder->add('calle');
        $builder->add('altura');
        $builder->add('areaTel');
        $builder->add('telefono');
        $builder->add('areaCel');
        $builder->add('celular');
        $builder->add('email');
    }

    public function getName()
    {
    return 'datos_form';
    }

}

前几天尝试嵌入表单,之前他做过一个嵌入表单的类型文件,但在我工作时,直接嵌套在控制器上。如果有人可以帮助我,我将不胜感激,有些日子我正在处理这个问题。 我的角色是显示表单,当用户发送时,数据会保留在各自的实体中。谢谢

尝试保留实体而不是形式

$em->persist($form->getData());

而不是

$em->persist($form);

一定要看看the docs