假设我有一个实体。
由于域逻辑
,此实体有两个字段只能通过一个函数进行更改如何使用一个方法调用使表单框架设置值。
我读到的关于data transformers
的内容,让我觉得它无法用于此提议。
接下来是form events
,这是可用的事件
PRE_BIND
BIND
POST_BIND
PRE_SET_DATA
POST_SET_DATA
BIND_CLIENT_DATA
BIND_NORM_DATA
SET_DATA
但关于此的文档非常缺乏。
这是一个示例实体
<?php
namespace X3\TestBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Test
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="X3\TestBundle\Entity\TestRepository")
*/
class Test
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="firstValue", type="string", length=255)
*/
private $firstValue;
/**
* @var string
*
* @ORM\Column(name="secondValue", type="string", length=255)
*/
private $secondValue;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Test
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set firstValue
*
* @param string $firstValue
* @return Test
*/
protected function setFirstValue($firstValue)
{
$this->firstValue = $firstValue;
return $this;
}
/**
* Get firstValue
*
* @return string
*/
public function getFirstValue()
{
return $this->firstValue;
}
/**
* Set secondValue
*
* @param string $secondValue
* @return Test
*/
protected function setSecondValue($secondValue)
{
$this->secondValue = $secondValue;
return $this;
}
/**
* Get secondValue
*
* @return string
*/
public function getSecondValue()
{
return $this->secondValue;
}
//
// The objective here is that the form use this function
// to set the two values in one call
//
protected function setValues($firstValue, $secondValue)
{
$this->firstValue = $firstValue;
$this->secondValue = $secondValue;
return $this;
}
}
请注意setFirstValue($firstValue)
和setSecondValue($secondValue)
为protected
,应使用setValues($firstValue, $secondValue)
是否有我可以使用的事件,检索firstValue
和secondValue
并使用setValues($firstValue, $secondValue)
进行设置,并避免form component
抱怨Method "setFirstValue()" is not public in class...
?
一些代码或链接到它,将是一个奖励。
答案 0 :(得分:0)
即使使用某些表单事件,扩展表单框架也不容易。
一种可能的解决方案是在实体中创建values
属性,该属性未映射到任何数据库字段及其setter和getter。然后,setValues
函数可以执行设置firstValue
和secondValue
所需的任何操作。
答案 1 :(得分:0)
您可能想要使用DataTransformer。它允许您控制两个方向的数据转换。无论如何,您需要将values
字段添加到表单中,该字段将转换为两个字段并保存为一个字段。