简体   繁体   English

使用数据库+ Symfony 2和Doctrine中的值填写表单中的选择列表

[英]Fill select list in form with values from database + Symfony 2 & Doctrine

I want to create a form with to begin only a select list wtih values selected from a database. 我想创建一个表单,只开始从数据库中选择的值的选择列表。
This is the entity Region and I would like to fill the dropdown with regions. 这是实体区域,我想用区域填写下拉列表。

<?php

namespace Reuzze\ReuzzeBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Regions
 *
 * @ORM\Table(name="regions")
 * @ORM\Entity
*/
class Regions
{
    /**
     * @var integer
     *
     * @ORM\Column(name="region_id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    protected $regionId;

    /**
     * @var string
     *
     * @ORM\Column(name="region_name", type="string", length=45, nullable=false)
     */
     protected $regionName;



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

    /**
     * Set regionName
     *
     * @param string $regionName
     * @return Regions
     */
    public function setRegionName($regionName)
    {
        $this->regionName = $regionName;

        return $this;
    }

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

This is my form: 这是我的表格:

class RegionType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('regionName'   , 'text', array(
            'label' => 'Region Name',
            'attr' => array('placeholder' => 'Region Name')
        ));
    }

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

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Reuzze\ReuzzeBundle\Entity\Regions',
        ));
    }

}

But now I would like to show the regions in a select list instead of giving in the name in a textbox. 但现在我想在选择列表中显示区域,而不是在文本框中给出名称。 Does anybody know how I can do this in my form? 有谁知道我怎么能以我的形式做到这一点? And how I show it in my view? 我如何在我看来展示它?

This is clearly documented in the Symfony docs - Choice Field 这在Symfony docs - Choice Field中有明确记载

$builder->add('regions', 'entity', array(
    'class' => 'ReuzzeReuzzeBundle:Regions',
    'property' => 'regionName',
    'expanded' => false,
    'multiple' => false
));

Something like this should get you going. 这样的事情会让你前进。

NOTE This code would be placed within a form that will have the select box. 注意此代码将放在将具有选择框的表单中。 You probably don't want to render only a select box with nothing else. 您可能不希望仅渲染没有其他内容的选择框。

UPDATE UPDATE

The docs also show you how to render a form in a template . 文档还向您展示了如何在模板中呈现表单

In your controller: 在你的控制器中:

$region = new Region();
$form = $this->createForm(new RegionType(), $region ); //or create a different form

// ....

return $this->render('ReuzzeReuzzeBundle:Default:index.html.twig', array(
        'form' => $form->createView(),
));

And in a twig template index.html.twig : 并在树枝模板index.html.twig

{{ form_start(form) }}
    {{ form_errors(form) }}

    {{ form_row(form.regions) }
{{ form_end(form) }}

Updated answer for Symfony 3+: Symfony 3+的更新答案:

You now need to use the EntityType class and the choice_label option. 您现在需要使用EntityType类和choice_label选项。

See docs here: http://symfony.com/doc/current/reference/forms/types/entity.html 请参阅此处的文档: http//symfony.com/doc/current/reference/forms/types/entity.html

use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Reuzze\ReuzzeBundle\Entity\Regions;

class RegionType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add(
            'regionName',
            EntityType::class,
            [
                'class' => Regions::class,
                'choice_label' => 'regionName',
                'expanded' => false,
                'multiple' => false
            ]
        );

        $builder->setMethod('POST');

        return $builder;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM