我有以下代码:
/**
* Callback transform array of checkboxes to string and store it in database
*
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function onPreUpdate()
{
if($this->id == 13)
{
$data = implode('-', $this->value);
$this->setValue($data);
}
}
/**
* Callback retrieved data from database transform to string to fill our field
*
* @ORM\PostLoad()
*/
public function onPostLoad()
{
if($this->id == 13)
{
$data = (strstr($this->value, '-') !== false) ? explode('-', $this->value) : array($this->value);
$this->setValue($data);
}
}
当我添加/更新数据时,我必须这样做以转换字符串中的复选框数组,反之亦然,当我从数据库中检索数据以将它们放入复选框小部件中时。但现在是静态的,即 id,我需要检查我的输入类型,如果它是一个复选框,请运行上面的代码......我做不到:
$this->getDoctrine()->getRepository();
在我的实体类中,因为它不起作用并且对于数据映射器来说不是一个好主意......所以我该怎么做呢?
感谢所有回复,对不起我的英语不好