php - 以 Symfony 形式添加 EntityType 的 CollectionType
问题描述
我有一个 Symfony 4 项目,我想在我的表单中创建一个 EntityType 的 CollectionType,这可能吗?
通常我知道如何做一个 CollectionType 但这种情况很特殊..
我的安装实体有:
/**
* @ORM\OneToMany(targetEntity="Option", mappedBy="installation", cascade={"persist"})
*
* @Assert\Valid()
*
* @var ArrayCollection|Option[]
*/
protected $options;
期权实体:
<?php
declare(strict_types = 1);
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity
*/
class Option
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer", options={"unsigned": true})
*
* @var integer|null
*/
protected $id;
/**
* @ORM\Column(type="string", length=255)
*
* @Assert\Type(type="string", groups={"etape_installation"})
* @Assert\Length(max="255", groups={"etape_installation"})
* @Assert\NotBlank(groups={"etape_installation"})
*
* @var string|null
*/
protected $libelle;
/**
* @ORM\Column(type="integer", options={"unsigned": true})
*
* @Assert\Type(type="int")
* @Assert\Range(min="1")
* @Assert\NotBlank()
*
* @var integer|null
*/
protected $prix;
/**
* @ORM\ManyToOne(targetEntity="Installation", inversedBy="options")
*
* @Assert\Valid()
*
* @var Installation|null
*/
protected $installation;
public function getLibelle(): ?string
{
return $this->libelle;
}
public function setLibelle(?string $libelle): void
{
$this->libelle = $libelle;
}
public function getPrix(): ?int
{
return $this->prix;
}
public function setPrix(?int $prix): void
{
$this->prix = $prix;
}
public function getInstallation(): ?Installation
{
return $this->installation;
}
public function setInstallation(?Installation $installation): void
{
$this->installation = $installation;
}
public function __toString(): ?string
{
return $this->getLibelle() . ' +' . $this->getPrix() . '€';
}
}
安装类型:
->add('options', CollectionType::class, [
'entry_type' => OptionType::class,
'entry_options' => [
'label' => 'Options supplémentaires',
],
'allow_add' => true,
'by_reference' => false,
'allow_delete' => true,
'prototype' => true,
])
我被困在我的 OptionType 中。请问我该怎么办?
先感谢您
解决方案
我建议在这里阅读 symfony 文档: https ://symfony.com/doc/current/form/form_collections.html
推荐阅读
- node.js - 无法使用在端口 3000 上运行的 Nginx 连接到 Node.js 应用程序
- node.js - Nodejs:如何从 REST API 下载 pdf 文件?
- java - Tesseract 加载语言失败 (Tess4j / Java / Netbeans)
- javascript - 如何将 ace 编辑器集成到不同语言的角度?
- odoo - Odoo12 域条件与 OR 和 AND 逻辑
- react-native - 如何在 React Native React Navigation 5 中为标题后退按钮的颜色设置动画?
- c++ - 如何在 D3D11 中设置 MSAA?
- javascript - React 不更新 HTML 或状态不同步
- react-native - react-native-modal 使背景平面列表可点击
- javascript - 在 D3 中转换 SVG 行时,会立即出现新行