Search code examples
formssymfonyentitydql

Fill my form with the data that I pick up from a table - Symfony2


My problem is this : I have an entity that contains user data , such as:

  • age
  • name
  • surname
  • address
  • phone
  • ...

My goal is to make a form where using a query can bring all this data and fill my form with the data , so that the user can modify it if you ever change the cell for example, or changing the direction of his house.

The company and I have made the entity​​ and the form, and consulting the table will do. My problem is how to fill the form fields with the data that I get from the table.

My entity (DatosEntity):

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

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

private $personales;

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

private $email;

/**
 * Get id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}

/**
 * Set empresa
 *
 * @param string $empresa
 * @return Datos
 */
public function setEmpresa($empresa)
{
    $this->empresa = $empresa;

    return $this;
}

/**
 * Get empresa
 *
 * @return string 
 */
public function getEmpresa()
{
    return $this->empresa;
}

/**
 * Set cuit
 *
 * @param integer $cuit
 * @return Datos
 */
public function setCuit($cuit)
{
    $this->cuit = $cuit;

    return $this;
}

/**
 * Get cuit
 *
 * @return integer 
 */
public function getCuit()
{
    return $this->cuit;
}

/**
 * Set localidad
 *
 * @param string $localidad
 * @return Datos
 */
public function setLocalidad($localidad)
{
    $this->localidad = $localidad;

    return $this;
}

/**
 * Get localidad
 *
 * @return string 
 */
public function getLocalidad()
{
    return $this->localidad;
}

/**
 * Set calle
 *
 * @param string $calle
 * @return Datos
 */
public function setCalle($calle)
{
    $this->calle = $calle;

    return $this;
}

/**
 * Get calle
 *
 * @return string 
 */
public function getCalle()
{
    return $this->calle;
}

/**
 * Set altura
 *
 * @param integer $altura
 * @return Datos
 */
public function setAltura($altura)
{
    $this->altura = $altura;

    return $this;
}

/**
 * Get altura
 *
 * @return integer 
 */
public function getAltura()
{
    return $this->altura;
}

/**
 * Set telefono
 *
 * @param integer $telefono
 * @return Datos
 */
public function setTelefono($telefono)
{
    $this->telefono = $telefono;

    return $this;
}

/**
 * Get telefono
 *
 * @return integer 
 */
public function getTelefono()
{
    return $this->telefono;
}

/**
 * Set area
 *
 * @param integer $area
 * @return Datos
 */
public function setAreaTel($areaTel)
{
    $this->areaTel = $areaTel;

    return $this;
}

/**
 * Get area
 *
 * @return integer 
 */
public function getAreaTel()
{
    return $this->areaTel;
}

/**
 * Set celular
 *
 * @param integer $celular
 * @return Datos
 */
public function setCelular($celular)
{
    $this->celular = $celular;

    return $this;
}

/**
 * Get celular
 *
 * @return integer 
 */
public function getCelular()
{
    return $this->celular;
}

/**
 * Set areaCel
 *
 * @param integer $areaCel
 * @return Datos
 */
public function setAreaCel($areaCel)
{
    $this->areaCel = $areaCel;

    return $this;
}

/**
 * Get areaCel
 *
 * @return integer 
 */
public function getAreaCel()
{
    return $this->areaCel;
}

/**
 * Set email
 *
 * @param integer $email
 * @return Datos
 */
public function setEmail($email)
{
    $this->email = $email;

    return $this;
}

/**
 * Get email
 *
 * @return integer 
 */
public function getEmail()
{
    return $this->email;
}

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


}

My DatosType:

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

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

}

The consultation on the controller but did not know how to do it. Now , do not like taking the data I receive and dump them in the form.

Thank you very much and sorry for the trouble


Solution

  • I think what you want to do can be done by following these steps :

    1. Start by collecting your User data in a variable we will call $user
    2. Then, give this $userto be hydrated by your form, like this :

      $form = $this->createForm(new UserType(), $user);

    3. Treat your form like you would normaly do. The values found in the entity should be automaticaly filled in your form.

    Hope this helps.