当列表字段是Sonata项目Symfony中使用的many_to_one类型时,使列表字段可编辑



My entity

/**
* @ORMManyToOne(targetEntity="Estat", inversedBy="temes")
*/
private $estat;

public function setEstat(NcdForumBundleEntityEstat $estat = null)
{
$this->estat = $estat;
return $this;
}

我的管理员

protected function configureListFields(ListMapper $listMapper)
{
//$estats=$this->getEstatsPossibles()->toArray();
$estats=array();
foreach($this->getEstatsPossibles() as $estat)
{
$estats[$estat->getId()]=$estat->getNom();
}
$listMapper
->add('estat', 'choice',['editable' => true,'choices'=> $estats])

我想在列表网格中编辑 estat 字段。以这种方式执行此操作,我使其可编辑,出现一个组合框,但是当我选择一个选项时,我得到一个异常,因为我实体的 setEstat 函数没有接收 Estat 实体,而是一个字符串(数组的键(。

正在尝试

->add('estat', 'many_to_one',['editable' => true,'choices'=> $estats])

仅显示指向实体的链接,没有任何更改的可能性。

可能吗?

等待更好,更干净的解决方案 按照以下答案的解决方案,我在我的实体中注入了一个实体管理器: 获取实体内的实体管理器

然后,在我的实体中,我更改了 setEstat 函数:

public function setEstat( $estat = null)
{
if (is_object($estat) &&   get_class($estat)=='NcdForumBundleEntityEstat')
{
$this->estat=$estat;
} else {
$estat_o=$this->em->getRepository('NcdForumBundleEntityEstat')->find((int)$estat);
$this->estat = $estat_o;
}

return $this;
}

最新更新