我希望使用一些内置库(例如表单构建器)可以做到这一点。我有以下三个实体。中间的那个几乎只是一个普通的连接表,但它有一个额外的列和一条额外的数据。
公式 --< 公式颜色 >-- 颜色
FormulaColor 具有以下字段:公式、颜色和百分比。
百分比字段是说颜色构成给定公式的百分比。一个非常简单的例子是一个公式可能是 77% 的红色和 33% 的蓝色。我的问题是我想为公式选择颜色,并使用表格手动给它们一个百分比。所以我会添加(或编辑)某个公式,并给出颜色紫色(20%)绿色(45%)和黄色(35%)。我不关心能否在公式添加/编辑视图中添加新颜色。我只想能够选择现有的颜色。我已经用集合和实体类型玩了几个小时,但没有运气。
对我有任何指示或提示吗?我必须在没有表单组件等的情况下手动完成吗?
谢谢。
公式形式类型
class FormulaAddEditType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('code', null, array(
'label' => 'Code'
))
->add('name', null, array(
'label' => 'Name'
))
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Prism\Portal\CommonBundle\Entity\Formula'
));
}
public function getName()
{
return 'prism_portal_adminbundle_formulaaddedittype';
}
}
公式实体
class Formula
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*
* @Serializer\Expose
*/
private $id;
/**
* @var string $code
*
* @ORM\Column(name="code", type="string", length=50, nullable=true)
*
* @Serializer\Expose
*/
private $code;
/**
* @var string $name
*
* @ORM\Column(name="name", type="string", length=50, nullable=true)
*/
private $name;
/**
* @var datetime $createdOn
*
* @Gedmo\Timestampable(on="create")
* @ORM\Column(name="createdOn", type="datetime", nullable=true)
*/
private $createdOn;
/**
* @var datetime $updatedOn
*
* @Gedmo\Timestampable(on="update")
* @ORM\Column(name="updatedOn", type="datetime", nullable=true)
*/
private $updatedOn;
/**
* @var formulaColors
*
* @ORM\OneToMany(targetEntity="FormulaColor", mappedBy="formula")
*/
private $formulaColors;
public function __construct()
{
$this->formulaColors = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set code
*
* @param string $code
*/
public function setCode($code)
{
$this->code = $code;
}
/**
* Get code
*
* @return string
*/
public function getCode()
{
return $this->code;
}
/**
* Set name
*
* @param string $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set createdOn
*
* @param datetime $createdOn
*/
public function setCreatedOn($createdOn)
{
$this->createdOn = $createdOn;
}
/**
* Get createdOn
*
* @return datetime
*/
public function getCreatedOn()
{
return $this->createdOn;
}
/**
* Set updatedOn
*
* @param datetime $updatedOn
*/
public function setUpdatedOn($updatedOn)
{
$this->updatedOn = $updatedOn;
}
/**
* Get updatedOn
*
* @return datetime
*/
public function getUpdatedOn()
{
return $this->updatedOn;
}
/**
* Add formulaColor
*
* @param FormulaColor $formulaColor
*/
public function addFormulaColor(FormulaColor $formulaColor)
{
$this->formulaColors[] = $formulaColor;
}
/**
* Get formulaColors
*
* @return Doctrine\Common\Collections\Collection
*/
public function getFormulaColors()
{
return $this->formulaColors;
}
}
公式颜色实体
/**
* Prism\Portal\CommonBundle\Entity\FormulaColor
*
* @ORM\Table(name="FormulaColor")
* @ORM\Entity
*
* @Serializer\ExclusionPolicy("all")
*/
class FormulaColor
{
/**
* @var integer $formula
*
* @ORM\ManyToOne(targetEntity="Formula", inversedBy="formulaColors")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="formulaId", referencedColumnName="id")
* })
* @ORM\Id
*/
private $formula;
/**
* @var integer color
*
* @ORM\ManyToOne(targetEntity="Color", inversedBy="formulaColors")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="colorId", referencedColumnName="id")
* })
* @ORM\Id
*/
private $color;
/**
* @var decimal percentage
*
* @ORM\Column(type="decimal", precision=5, scale=2, nullable=false)
*/
private $percentage;
/**
* Set percentage
*
* @param decimal $percentage
*/
public function setPercentage($percentage)
{
$this->percentage = $percentage;
}
/**
* Get percentage
*
* @return decimal
*/
public function getPercentage()
{
return $this->percentage;
}
/**
* Set formula
*
* @param Prism\Portal\CommonBundle\Entity\Formula $formula
*/
public function setFormula(\Prism\Portal\CommonBundle\Entity\Formula $formula)
{
$this->formula = $formula;
}
/**
* Get formula
*
* @return Prism\Portal\CommonBundle\Entity\Formula
*/
public function getFormula()
{
return $this->formula;
}
/**
* Set color
*
* @param Prism\Portal\CommonBundle\Entity\Color $color
*/
public function setColor(\Prism\Portal\CommonBundle\Entity\Color $color)
{
$this->color = $color;
}
/**
* Get color
*
* @return Prism\Portal\CommonBundle\Entity\Color
*/
public function getColor()
{
return $this->color;
}
}
颜色实体
/**
* Prism\Portal\CommonBundle\Entity\Color
*
* @ORM\Table(name="Color")
* @ORM\Entity
*
* @Serializer\ExclusionPolicy("all")
*/
class Color
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string $code
*
* @ORM\Column(name="code", type="string", length=50, nullable=true)
*/
private $code;
/**
* @var string $hexColor
*
* @ORM\Column(name="hex_color", type="string", length=50, nullable=true)
*/
private $hexColor;
/**
* @var string $name
*
* @ORM\Column(name="name", type="string", length=50, nullable=true)
*/
private $name;
/**
* @var integer $sortOrder
*
* @ORM\Column(name="sortOrder", type="integer", nullable=true)
*/
private $sortOrder;
/**
* @var datetime $createdOn
*
* @ORM\Column(name="createdOn", type="datetime", nullable=true)
*/
private $createdOn;
/**
* @var datetime $updatedOn
*
* @ORM\Column(name="updatedOn", type="datetime", nullable=true)
*/
private $updatedOn;
/**
* @var $formulaColors
*
* @ORM\OneToMany(targetEntity="FormulaColor", mappedBy="color")
*/
private $formulaColors;
public function __construct()
{
$this->formulaColors = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set code
*
* @param string $code
*/
public function setCode($code)
{
$this->code = $code;
}
/**
* Get code
*
* @return string
*/
public function getCode()
{
return $this->code;
}
/**
* Set name
*
* @param string $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set sortOrder
*
* @param integer $sortOrder
*/
public function setSortOrder($sortOrder)
{
$this->sortOrder = $sortOrder;
}
/**
* Get sortOrder
*
* @return integer
*/
public function getSortOrder()
{
return $this->sortOrder;
}
/**
* Set createdOn
*
* @param datetime $createdOn
*/
public function setCreatedOn($createdOn)
{
$this->createdOn = $createdOn;
}
/**
* Get createdOn
*
* @return datetime
*/
public function getCreatedOn()
{
return $this->createdOn;
}
/**
* Set updatedOn
*
* @param datetime $updatedOn
*/
public function setUpdatedOn($updatedOn)
{
$this->updatedOn = $updatedOn;
}
/**
* Get updatedOn
*
* @return datetime
*/
public function getUpdatedOn()
{
return $this->updatedOn;
}
/**
* Get formulaColors
*
* @return Doctrine\Common\Collections\Collection
*/
public function getFormulaColors()
{
return $this->formulaColors;
}
/**
* addFormulaColors
*
* @param \Prism\Portal\CommonBundle\Entity\FormulaColor $formulaColor
*/
public function addFormulaColor(\Prism\Portal\CommonBundle\Entity\FormulaColor $formulaColor)
{
$this->formulaColors[] = $formulaColor;
}
/**
* Remove formulaColors
*
* @param \Prism\Portal\CommonBundle\Entity\FormulaColor $formulaColors
*/
public function removeFormulaColor(\Prism\Portal\CommonBundle\Entity\FormulaColor $formulaColors)
{
$this->formulaColors->removeElement($formulaColors);
}
/**
* Set hexColor
*
* @param string $hexColor
* @return Color
*/
public function setHexColor($hexColor)
{
$this->hexColor = $hexColor;
return $this;
}
/**
* Get hexColor
*
* @return string
*/
public function getHexColor()
{
return $this->hexColor;
}
}
我也有一个 ColorAddEditType 表单
class ColorAddEditType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('code', null, array(
'label' => 'Code'
))
->add('name', null, array(
'label' => 'Name'
))
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Prism\Portal\CommonBundle\Entity\Color'
));
}
public function getName()
{
return 'prism_portal_adminbundle_coloraddedittype';
}
}
我还根据 Ryan 的回复更新了我的代码。
公式颜色类型
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('color', new ColorAddEditType());
$builder->add('percent', 'number');
}
公式添加编辑类型
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('code', null, array(
'label' => 'Code'
))
->add('name', null, array(
'label' => 'Name'
));
$builder->add('formulaColors', 'collection', array(
'type' => new FormulaColorType(),
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
));
}
现在我正在使用转换器/将 data_class 设置为空异常。当我将一个更改为空时,它说我可以将另一个更改为空。当我这样做时,它基本上说要改回来。我应该为这样的事情设置数据转换器是否正常?
这是我得到的当前错误:
表单的视图数据应该是 Prism\Portal\CommonBundle\Entity\Color 类的实例,但它是 Prism\Portal\CommonBundle\Entity\FormulaColor 类的实例。您可以通过将“data_class”选项设置为 null 或添加将 Prism\Portal\CommonBundle\Entity\FormulaColor 类的实例转换为 Prism\Portal\CommonBundle\Entity\Color 的实例的视图转换器来避免此错误。