我有选择字段的symfony表单。我想从我的实体类静态方法加载选择。我可以使用数据还是CallbackChoiceLoader?什么是最佳做法?
这是我的领域:
class CarType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('type', ChoiceType::class, [
// This choices I would like to load from:
// 'choices' => $car::getTypes(),
'choices' => [
'Critical' => 'critical',
'Medium' => 'medium',
'Info' => 'info',
],
'label' => 'Type'
])
->add('name', TextType::class, [
'label' => 'Name'
])
->add('save', SubmitType::class, [
'label' => 'Save'
])
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => 'AppBundle\Entity\Car'
]);
}
public function getName()
{
return 'car';
}
}
这是我的实体:
/**
* @ORM\Entity
* @ORM\Table(name="car")
*/
class Car
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string $type
*
* @Assert\NotBlank()
* @ORM\Column(name="type")
*/
private $type;
/**
* @var string $name
*
* @Assert\NotBlank()
* @ORM\Column(name="name")
*/
private $name;
/**
* @var \DateTime $created
*
* @ORM\Column(name="created", type="datetime")
*/
private $created;
private static $types = ['main', 'custom'];
public function __construct()
{
$this->created = new \DateTime('now', new \DateTimeZone('Europe/Ljubljana'));
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Get types
*
* @return array
*/
public function getTypes()
{
return self::$types;
}
/**
* Set type
*
* @param string $type
*
* @return Car
*/
public function setType($type)
{
if (in_array($type, $this->getTypes())) {
$this->type = $type;
}
return $this;
}
/**
* Get type
*
* @return string
*/
public function getType()
{
return $this->type;
}
/**
* Set name
*
* @param string $name
*
* @return Car
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set created
*
* @param \DateTime $created
*
* @return Car
*/
public function setCreated($created)
{
$this->created = $created;
return $this;
}
/**
* Get created
*
* @return \DateTime
*/
public function getCreated()
{
return $this->created;
}
如何以及我可以使用什么来加载来自$ car :: getTypes()方法的选择,就像我在表单中注释一样,以便根据getTypes实体方法中的值动态加载选项?
答案 0 :(得分:1)
编辑:这是选项1.下面的选项2更直接地回答了问题。
从实体创建选择表单字段的首选方法是使用EntityType字段。以下是需要种族领域的应用程序的示例。下面是种族实体。
表单字段:
->add('ethnicity', EntityType::class, array(
'label' => 'Ethnicity:',
'class' => 'AppBundle:Ethnicity',
'choice_label' => 'abbreviation',
'expanded' => false,
'placeholder' => 'Select ethnicity',
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('e')
->orderBy('e.abbreviation', 'ASC')
;
},
))
种族实体:
/**
* Ethnicity.
*
* @ORM\Table(name="ethnicity")
* @ORM\Entity
*/
class Ethnicity
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* @var string
*
* @ORM\Column(name="ethnicity", type="string", length=45, nullable=true)
*/
protected $ethnicity;
/**
* @var string
*
* @ORM\Column(name="abbr", type="string", length=45, nullable=true)
*/
protected $abbreviation;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\OneToMany(targetEntity="AppBundle\Entity\Member", mappedBy="ethnicity", cascade={"persist"})
*/
protected $members;
/**
* Constructor.
*/
public function __construct()
{
$this->members = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id.
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set ethnicity.
*
* @param string $ethnicity
*
* @return Ethnicity
*/
public function setEthnicity($ethnicity)
{
$this->ethnicity = $ethnicity;
return $this;
}
/**
* Get ethnicity.
*
* @return string
*/
public function getEthnicity()
{
return $this->ethnicity;
}
/**
* Set abbreviation.
*
* @param string $abbreviation
*
* @return Ethnicity
*/
public function setAbbreviation($abbreviation)
{
$this->abbreviation = $abbreviation;
return $this;
}
/**
* Get abbreviation.
*
* @return string
*/
public function getAbbreviation()
{
return $this->abbreviation;
}
/**
* Add members.
*
* @param \AppBundle\Entity\Member $members
*
* @return Ethnicity
*/
public function addMember(\AppBundle\Entity\Member $members)
{
$this->members[] = $members;
return $this;
}
/**
* Remove members.
*
* @param \AppBundle\Entity\Member $members
*/
public function removeMember(\Truckee\ProjectmanaBundle\Entity\Member $members)
{
$this->members->removeElement($members);
}
/**
* Get members.
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getMembers()
{
return $this->members;
}
/**
* @var bool
*
* @ORM\Column(name="enabled", type="boolean", nullable=true)
*/
protected $enabled;
/**
* Set enabled.
*
* @param bool $enabled
*
* @return enabled
*/
public function setEnabled($enabled)
{
$this->enabled = $enabled;
return $this;
}
/**
* Get enabled.
*
* @return bool
*/
public function getEnabled()
{
return $this->enabled;
}
}
选项2: 如果你真的想这样做,那么这就是一种方法:
$types
,以便选项是数组中的值,例如$types = array(1 => 'main', 2 => 'custom')
在控制器操作中:
$car = new Car();
$types = $car->getTypes;
使用Passing data to buildForm() in Symfony 2.8/3.0的答案来了解如何将$types
传递给您的表单。