Symfony 2.3如何为类表单中的选择创建默认值

时间:2014-01-21 01:21:39

标签: forms symfony entity

我为客户注册创建了一个表单类。我现在想转向给定客户的数据修正案。特别地,每个客户被分配到从列表(一个实体)获取的区域。我想知道如何在select中显示数据库字段(简而言之,就像select =“selected”一样)。

我显示了我的类和控制器的代码

实体/ cliente.php

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

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

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

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

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

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

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

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



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

/**
 * Set nome
 *
 * @param string $nome
 * @return Cliente
 */
public function setNome($nome)
{
    $this->nome = $nome;

    return $this;
}

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

/**
 * Set cognome
 *
 * @param string $cognome
 * @return Cliente
 */
public function setCognome($cognome)
{
    $this->cognome = $cognome;

    return $this;
}

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

/**
 * Set indirizzo
 *
 * @param string $indirizzo
 * @return Cliente
 */
public function setIndirizzo($indirizzo)
{
    $this->indirizzo = $indirizzo;

    return $this;
}

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

/**
 * Set tel
 *
 * @param string $tel
 * @return Cliente
 */
public function setTel($tel)
{
    $this->tel = $tel;

    return $this;
}

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

/**
 * Set cel
 *
 * @param string $cel
 * @return Cliente
 */
public function setCel($cel)
{
    $this->cel = $cel;

    return $this;
}

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

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

    return $this;
}

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



/**
 * @ORM\OneToOne(targetEntity="Regione", inversedBy="clienti")
 * @ORM\JoinColumn(name="regione_id", referencedColumnName="id")
 */
protected $regione;

/**
 * Set regione
 *
 * @param \Management\ClientiBundle\Entity\Regione $regione
 * @return Cliente
 */
public function setRegione(\Management\ClientiBundle\Entity\Regione $regione = null)
{
    $this->regione = $regione;

    return $this;
}

/**
 * Get regione
 *
 * @return \Management\ClientiBundle\Entity\Regione 
 */
public function getRegione()
{
    return $this->regione;
}

/**
 * Set iva_cod_fisc
 *
 * @param string $ivaCodFisc
 * @return Cliente
 */
public function setIvaCodFisc($ivaCodFisc)
{
    $this->iva_cod_fisc = $ivaCodFisc;

    return $this;
}

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

 /**
 * @ORM\ManyToOne(targetEntity="Provincia", inversedBy="clienti")
 * @ORM\JoinColumn(name="provincia_id", referencedColumnName="id")
 */
protected $provincia;

/**
 * Set provincia
 *
 * @param \Management\ClientiBundle\Entity\Provincia $provincia
 * @return Cliente
 */
public function setProvincia(\Management\ClientiBundle\Entity\Provincia $provincia = null)
{
    $this->provincia = $provincia;

    return $this;
}

/**
 * Get provincia
 *
 * @return \Management\ClientiBundle\Entity\Provincia 
 */
public function getProvincia()
{
    return $this->provincia;
}

 /**
 * @ORM\ManyToOne(targetEntity="Comune", inversedBy="clienti")
 * @ORM\JoinColumn(name="comune_id", referencedColumnName="id")
 */
protected $comune;

/**
 * Set comune
 *
 * @param \Management\ClientiBundle\Entity\Comune $comune
 * @return Cliente
 */
public function setComune(\Management\ClientiBundle\Entity\Comune $comune = null)
{
    $this->comune = $comune;

    return $this;
}

/**
 * Get comune
 *
 * @return \Management\ClientiBundle\Entity\Comune 
 */
public function getComune()
{
    return $this->comune;
}

 /*
 * @ORM\OneToMany(targetEntity="Note", mappedBy="cliente")
 * 
 */
protected $note;


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

/**
 * Set url
 *
 * @param string $url
 * @return Cliente
 */
public function setUrl($url)
{
    $this->url = $url;

    return $this;
}

/**
 * Get url
 *
 * @return string 
 */
public function getUrl()
{
    return $this->url;
}
/**
 * Constructor
 */
public function __construct()
{
    $this->note = new \Doctrine\Common\Collections\ArrayCollection();
}

/**
 * Add note
 *
 * @param \Management\ClientiBundle\Entity\Note $note
 * @return Cliente
 */
public function addNote(\Management\ClientiBundle\Entity\Note $note)
{
    $this->note[] = $note;

    return $this;
}

/**
 * Remove note
 *
 * @param \Management\ClientiBundle\Entity\Note $note
 */
public function removeNote(\Management\ClientiBundle\Entity\Note $note)
{
    $this->note->removeElement($note);
}

/**
 * Get note
 *
 * @return \Doctrine\Common\Collections\Collection 
 */
public function getNote()
{
    return $this->note;
}

实体/ regione.php

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

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


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

/**
 * Set nome
 *
 * @param string $nome
 * @return Regione
 */
public function setNome($nome)
{
    $this->nome = $nome;

    return $this;
}

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

/**
 * @ORM\OneToMany(targetEntity="Cliente", mappedBy="regione")
 */
protected $clienti;

public function __construct()
{
    $this->clienti = new ArrayCollection();
}

/**
 * Add clienti
 *
 * @param \Management\ClientiBundle\Entity\Cliente $clienti
 * @return Regione
 */
public function addClienti(\Management\ClientiBundle\Entity\Cliente $clienti)
{
    $this->clienti[] = $clienti;

    return $this;
}

/**
 * Remove clienti
 *
 * @param \Management\ClientiBundle\Entity\Cliente $clienti
 */
public function removeClienti(\Management\ClientiBundle\Entity\Cliente $clienti)
{
    $this->clienti->removeElement($clienti);
}

/**
 * Get clienti
 *
 * @return \Doctrine\Common\Collections\Collection 
 */
public function getClienti()
{
    return $this->clienti;
}

型/ clienteType.php

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('nome','text')
            ->add('cognome','text',array('max_length'=>10))
            ->add('regione', 'entity', array('class' => 'ManagementClientiBundle:Regione', 'property' => 'nome','empty_value' => 'Scegliere la tua regione'))
            ->add('provincia', 'entity', array('class' => 'ManagementClientiBundle:Provincia', 'property' => 'sigla','empty_value' => 'Scegliere la provincia'))
            ->add('comune', 'entity', array('class' => 'ManagementClientiBundle:Comune', 'property' => 'nome','empty_value' => 'Scegliere il comune' ))
            ->add('indirizzo','text')
            ->add('tel','text')
            ->add('cel','text')
            ->add('iva_cod_fisc','text',array('required'=>false))
            ->add('email','text')
            ->add('Salva','submit');
}

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

defaultController.php

    public function modificaClienteAction(Request $request,$dettaglio_cliente)
    {
       $id = $this->idCliente($dettaglio_cliente);
       $dettaglio = $this->getDoctrine()->getRepository('ManagementClientiBundle:Cliente')->findById($id);

        $form   =   $this->createForm('cliente',$dettaglio);
        $form->handleRequest($request);

        if($form->isValid()){
            $em = $this->getDoctrine()->getManager();
            $cliente = $em->getRepository('ManagementClientiBundle:Cliente')->find($id);
           if($_POST['form']['nome'] && $_POST['form']['cognome'] && $_POST['form']['indirizzo'] && $_POST['form']['regione'] && $_POST['form']['comune'] && $_POST['form']['provincia'] && $_POST['form']['cel'] && $_POST['form']['email']){
            $clienti->setNome($_POST['form']['nome']);
            $clienti->setCognome($_POST['form']['cognome']);
            $clienti->setIndirizzo($_POST['form']['indirizzo']);
            $clienti->setCel($_POST['form']['tel']);
            $clienti->setCel($_POST['form']['cel']);
            $clienti->setIvaCodFisc($_POST['form']['iva_cod_fisc']);
            $clienti->setEmail($_POST['form']['email']);

            $em->flush();

            $notifica  = "MODIFICA AVVENUTA";
            }else $notifica = "Controlla che tutti i campi obbligatori non siano vuoti!!";
        }

          return $this->render('ManagementClientiBundle:Default:modifica.html.twig',
                               array('form'=> $form->createView(),
                                     'cliente'=>$dettaglio,
                                     'id'=>$id
                                     )
                              );
    }
}

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

首先,您需要使用Doctrine正确实现实体关系。 $cliente应与区域实体建立关系。

然后你真的需要重新阅读:http://symfony.com/doc/current/book/forms.html#building-the-form

它显示了如何正确处理提交的数据并将其绑定回$clienti实体并使用实体类型设置表单。