【发布时间】:2014-03-17 20:14:11
【问题描述】:
想象你有一个物品实体。在这个项目实体中,有一种方法可以获取该项目的价格。价格以 3 种不同的格式保存:欧元、美元和英镑。
实体看起来像这样:
实体 WebshopItem.php
class WebshopItem
{
/**
* @var integer
*/
private $id;
/**
* @Gedmo\Translatable
* @var string
*/
private $title;
......
/**
* @var \Doctrine\Common\Collections\Collection
*/
private $prices;
}
实体 WebshopItemPrice.php
class WebshopItemPrice
{
/**
* @var integer
*/
private $id;
/**
* @var string
*/
private $currency;
/**
* @var string
*/
private $price;
/**
* @var \WebshopItem
*/
private $webshopItem;
}
现在我想创建一个表单,其中正好包含 3 个输入字段。为此,我认为最好使用货币字段。所以我正在创建这样的表单:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title')
....
->add('prices', new WebshopPricesType());
}
webshopPricesType 如下所示:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('eur', 'money', array('currency' => 'EUR', 'data_class' => 'bundlePath\Entity\WebshopItemPrice'))
->add('usd', 'money', array('currency' => 'USD', 'data_class' => 'bundlePath\Entity\WebshopItemPrice'))
->add('gbp', 'money', array('currency' => 'GBP', 'data_class' => 'bundlePath\Entity\WebshopItemPrice'));
}
现在呈现 3 个正确的字段。我只需要在编辑时填写它们,保存时,我必须确保它们被保存。我正在考虑使用数据转换器来查找正确的实体,但这不起作用。
如何确保在编辑时正确预填所有 3 个字段,并在单击保存时保存 3 个价格?
或者我应该以完全不同的方式来做吗?
谢谢!
【问题讨论】:
标签: php forms symfony currency