如何将表单组合框的默认值设置为Symfony2中数据库中的值?

时间:2014-11-27 11:51:10

标签: database symfony combobox field default-value

我想知道如何将表单Combobox字段的默认值设置为Symfony2中数据库中的值。解释如下:

这是我正在处理的实体的代码:

    <?php

namespace Ikproj\HomeBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * User
 *
 * @ORM\Table(name="user")
 * @ORM\Entity(repositoryClass="Ikproj\HomeBundle\Entity\UserRepository")
 */
class User
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id_user", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

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

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

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

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

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

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="dateanniv", type="date")
     */
    private $dateanniv;


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

    /**
     * Set name
     *
     * @param string $name
     * @return User
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * Set pseudo
     *
     * @param string $pseudo
     * @return User
     */
    public function setPseudo($pseudo)
    {
        $this->pseudo = $pseudo;

        return $this;
    }

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

    /**
     * Set passWD
     *
     * @param string $passWD
     * @return User
     */
    public function setPassWD($passWD)
    {
        $this->passWD = $passWD;

        return $this;
    }

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

    /**
     * Set sexeuser
     *
     * @param string $sexeuser
     * @return User
     */
    public function setSexeuser($sexeuser)
    {
        $this->sexeuser = $sexeuser;

        return $this;
    }

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

    /**
     * Set dateanniv
     *
     * @param \DateTime $dateanniv
     * @return User
     */
    public function setDateanniv($dateanniv)
    {
        $this->dateanniv = $dateanniv;

        return $this;
    }

    /**
     * Get dateanniv
     *
     * @return \DateTime 
     */
    public function getDateanniv()
    {
        return $this->dateanniv;
    }
}

这是属于上述实体的表格的代码:

    <?php

namespace Ikproj\HomeBundle\Form;

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

class UserprofilechangeType extends AbstractType
{
     /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name','text')
                ->add('sexeuser', 'choice', array('choices' => array('Homme' => 'Homme', 'Femme' => 'Femme')))
                ->add('dateanniv','date', array('input' => 'datetime','format' => 'yyyy-MM-dd','years' => range(1900, date("Y"))));
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Ikproj\HomeBundle\Entity\User'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'ikproj_homebundle_user';
    }
}

这是视图html \ twig的代码:

 <html>
    <head>
        <link rel="stylesheet" type="text/css" href="{{asset('bundles/ikprojhome/css2/css10.css')}}"/>
        <script src='{{asset('bundles/ikprojhome/lib/jquery.min.js')}}'></script>
    </head>
    <body>
        <center>
            <div id="container">
                <div id="header">
                </div>
                <div id="content">
                    <table width="100%" height="100%" align="center">
                        <tr>
                            <td>   
                                <form action="{{path('ikproj_home_profilechange',{id:id})}}" method="POST" {{ form_enctype(form) }} onsubmit="javascript:parent.jQuery.fancybox.close();">
                                    {{ form_errors(form) }}
                                    <table align="center">
                                        <tr>
                                            <td class="separation">
                                                <label>Nom:</label>
                                            </td>
                                            <td>
                                                {{ form_widget(form.name) }}
                                            </td>
                                        </tr>
                                        <tr height="20px"></tr>
                                        <tr>
                                            <td class="separation">
                                                <label>Sexe:</label>
                                            </td>
                                            <td>
                                                {{ form_widget(form.sexeuser) }}
                                            </td>
                                        </tr>
                                        <tr height="20px"></tr>
                                        <tr>
                                            <td class="separation">
                                                <label>Date de naissance:</label>
                                            </td>
                                            <td>
                                                {{ form_widget(form.dateanniv) }}
                                            </td>
                                        </tr>
                                        <tr>
                                            <td colspan="2" align="center" id="button" valign="bottom">
                                                <input class="button" type="submit" value="" id="update"/>
                                            </td>
                                        </tr>
                                    </table>
                                    {{form_widget(form._token)}}
                                </form>
                            </td>
                        </tr>
                    </table> 
                </div>
            </div>
        </center>
    </body>
</html>

实际上,数据库中字段“sexeuser”的值(与上面实体的代码中的变量$sexeuser相关)是“Femme”,而当视图的页面是html \ twig时显示Combobox字段中显示的默认值为“Homme”。所以,我的问题是:如何将Combobox字段的默认值设置为数据库中的确切值? (因此,默认值为“Femme”而非“Homme”)。

修改

这是控制器中的相关代码:

public function ProfileChangeAction($id, Request $request) {
    $em = $this->getDoctrine()->getManager();
    $profile1 = $em->getRepository('IkprojHomeBundle:User')->find($id);
    $form = $this->createForm(new UserprofilechangeType(), $profile1);
    $profile2 = $form->getData();
    $form->handleRequest($request);
    if ($request->isMethod('POST')) {
        if ($form->isValid()) {
            $em->persist($profile2);
            $em->flush();
            return $this->render('IkprojHomeBundle:configuration:profilechange.html.twig');
        }
    } else {
        return $this->render('IkprojHomeBundle:configuration:profilechange.html.twig', array(
                    'id' => $id,
                    'form' => $form->createView()));
    }
}

1 个答案:

答案 0 :(得分:0)

最后,我解决了它,我刚刚在表单类中更改了以下行:

->add('sexeuser', 'choice', array('choices' => array('Homme' => 'Homme', 'Femme' => 'Femme')))

到这一个:

->add('sexeuser', 'choice', array('choices' => array('Homme' => 'Homme', 'Femme' => 'Femme'),'empty_value' => null))

实际上,正如您在上面所看到的,我添加了这部分代码:'empty_value' => null