我无法实现仅包含固定字段的表单集合,即allow_add和allow_delete为false。
我有一个董事会实体,该实体由许多属性组成,其中包括“组件”属性,该属性与“组件”实体具有多对多关系。进入董事会的表格将有一个“组件”部分,但这不是用户可以添加他们喜欢的任何组件的情况。相反,它们将显示一个固定的组件类型列表,然后可以从下拉列表中选择组件。例如:
我有部分工作。我能够在BoardType中添加组件字段,并使用所需条件生成下拉菜单。我可以提交表单,并保留相应的数据库字段。一切都很好。当我去编辑一个现有的板子...不同的故事。发生了两种不希望发生的事情:
现在的代码(为简洁起见,简称:)
董事会实体
/**
* Board
*
* @ORM\Table(name="Board", indexes={@ORM\Index(name="type_id", columns={"type_id"})})
* @ORM\Entity
*/
class Board extends Baseentity
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string|null
*
* @ORM\Column(name="customer_board_name", type="string", length=128, nullable=true)
*/
private $customerBoardName;
/**
* @ORM\ManyToMany(targetEntity="Component")
* @ORM\JoinTable(name="BoardComponentJoin",
* joinColumns={@ORM\JoinColumn(name="board_id")},
* inverseJoinColumns={@ORM\JoinColumn(unique=true)})
*/
private $components;
public function __construct()
{
$this->components = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getCustomerBoardName(): ?string
{
return $this->customerBoardName;
}
public function setCustomerBoardName(?string $customerBoardName): self
{
$this->customerBoardName = $customerBoardName;
return $this;
}
public function getComponents()
{
return $this->components;
}
public function addComponent(Component $component)
{
$this->components->add($component);
}
public function removeComponent(Component $component)
{
$this->components->removeElement($component);
}
组件实体
/**
* Component
*
* @ORM\Table(name="Component", indexes={@ORM\Index(name="type_id", columns={"type_id"}), @ORM\Index(name="company_id", columns={"company_id"})})
* @ORM\Entity
*/
class Component extends Baseentity
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=128, nullable=false)
*/
private $name;
/**
* @var string|null
*
* @ORM\Column(name="description", type="text", length=65535, nullable=true)
*/
private $description;
/**
* @var \ComponentDevicetype
*
* @ORM\ManyToOne(targetEntity="ComponentDevicetype")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="type_id", referencedColumnName="id")
* })
*/
private $type;
/**
* @var \Company
*
* @ORM\ManyToOne(targetEntity="Company", cascade={"persist"})
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="company_id", referencedColumnName="id")
* })
*/
private $company;
BoardType
class BoardType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('customerBoardName', TextType::class, [
'label' => 'Name (Customer)'])
;
$builder
->add('components', CollectionType::class, [
'entry_type' => EmbedComponentType::class,
'allow_add' => false,
'allow_delete' => false])
;
$builder->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event) {
$form = $event->getForm();
$form->get('components')
->add('ddr', EntityType::class, [
'class' => Component::class,
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('comp')
->where('comp.type = 1')
->orderBy('comp.name', 'ASC');},
'choice_label' => 'name',
'placeholder' => 'Choose Component...',
'label' => 'DDR',
'required' => false])
->add('flash', EntityType::class, [
'class' => Component::class,
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('comp')
->where('comp.type = 6')
->orderBy('comp.name', 'ASC');},
'choice_label' => 'name',
'placeholder' => 'Choose Component...',
'label' => 'Flash',
'required' => false]);
});
}
EmbedComponentType
class EmbedComponentType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', EntityType::class, [
'class' => Component::class,
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('comp')
->orderBy('comp.name', 'ASC');},
'choice_label' => 'name',
'placeholder' => 'Choose Component...',]);
}
}
我的问题:
感谢您的帮助。