我希望使用一些内置库(例如表单构建器)可以实现这一点。我有以下三个实体。中间的那个几乎只是一个常规的连接表,但它有一个额外的列,带有额外的数据。
公式 - < FormulaColor> - 颜色
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;
}
}
FormulaColor实体
/**
* 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的回复更新了我的代码。
FormulaColorType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('color', new ColorAddEditType());
$builder->add('percent', 'number');
}
FormulaAddEditType
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,
));
}
现在我正在使用变换器/ set data_class来使异常为空。当我将一个更改为null时,它表示我可以将另一个更改为null。当我这样做时,它基本上会说要改回来。我应该为这样的事情设置数据转换器是正常的吗?
以下是我遇到的当前错误:
表单的视图数据应该是类的实例 Prism \ Portal \ CommonBundle \ Entity \ Color,但是是类的实例 棱镜\门户\ CommonBundle \实体\ FormulaColor。你可以避免这种情况 将“data_class”选项设置为null或添加视图时出错 转换类实例的转换器 Prism \ Portal \ CommonBundle \ Entity \ FormulaColor为一个实例 棱镜\门户\ CommonBundle \实体\颜色。
答案 0 :(得分:1)
你应该可以做这样的事情。这是一种相当普遍的模式。棘手的一点是Javascript,但它不会太糟糕。您可能需要使用Javascript将数字添加到100。
<强> FormulaType 强>
public function buildForm(FormBuilder $builder, array $options) {
$builder->add('formulaColors', 'collection', array(
'type' => new FormularColorType(),
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
));
}
<强> FormulaColorType 强>
public function buildForm(FormBuilder $builder, array $options) {
$builder->add('color', new ColorType()); // Or similar
$builder->add('percent', 'number');
}