symfony 2动态下拉菜单

时间:2015-06-19 13:28:05

标签: php symfony drop-down-menu

如何使用symfony 2创建动态下拉列表 我有类似的东西,

$builder->add('gender', 'choice', array(
         'choices'  => array(1 => 'campus1', 2 => 'campus2'),
         'required' => false,
));

我不想硬编码任何东西,我不希望它在树枝或前端,我想在控制器中有一些东西。 这是需要加载到下拉列表中的实体

<?php

namespace Test\Bundle\TestBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

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

/**
 * @ORM\ManyToOne(targetEntity="University", inversedBy="campus")
 * @ORM\JoinColumn(name="university_id", referencedColumnName="id")
 */
private $university;

/**
 * @var string
 *
 * @ORM\Column(name="name", type="string", length=255)
 * @Assert\NotBlank
 * @Assert\Length(
 *                  min=3,
 *                  max=35,
 *                  minMessage= "Name Field should contains at least 3 characters",
 *                  maxMessage = "Name Field Cannot contain more than 35 characters"
 *               )
 * @Assert\Regex(pattern="/[^a-z\s-]/i", match=false , message="Name can only contain letters")
 */
private $name;

/**
 * @var string
 * @ORM\Column(name="address", type="string", length=255)
 * @Assert\NotBlank()
 * @Assert\Length(
 *                 min = 10,
 *                 max = 80,
 *                 minMessage = "Address must be more specific",
 *                 maxMessage = "80 characters limit exceeded"
 *              )
 */
private $address;


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

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

    return $this;
}

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

/**
 * Set address
 *
 * @param string $address
 * @return Campus
 */
public function setAddress($address)
{
    $this->address = $address;

    return $this;
}

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

/**
 * Set university
 *
 * @param \Test\Bundle\TestBundle\Entity\University $university
 * @return Campus
 */
public function setUniversity(\Test\Bundle\TestBundle\Entity\University $university = null)
{
    $this->university = $university;

    return $this;
}

/**
 * Get university
 *
 * @return \Test\Bundle\TestBundle\Entity\University 
 */
public function getUniversity()
{
    return $this->university;
}

}

当选择下拉列表并提交表单时,我将使用实体中的其他字段,因此在提交表单时我需要整个对象。

那么如何使用symfony2功能从数据库中填充数据呢?

2 个答案:

答案 0 :(得分:1)

这就像构建一个数组并传递它一样简单

$camps=$em->getRepository('TestTestBundle:Campus')->findAll();
$choices=array();
foreach ($camps as $campus) {
    $choices[$campus->getId()] = $campus->getName();
}
$builder->add('gender', 'choice', array(
         'choices'  => $choices,
         'required' => false,
));

您可以通过ID

找到实体

答案 1 :(得分:1)

使用Entity表单字段类型,这是一种特殊类型的选择字段,用于从Doctrine实体加载选项。 e.g:

$builder->add('campus', 'entity', array(
    'class' => 'TestBundleTestBundle:Campus',
    'property' => 'name',
    'query_builder' => function (EntityRepository $er) {
        return $er->createQueryBuilder('c')
            ->orderBy('c.name', 'ASC');
    },
));

提交表单后,“校园”字段的表单数据将成为Campus类的实例。如果您的表单数据是实体而不是数组,那么该实体将需要具有相应的“校园”字段,该字段表示与Campus实体的关系。