Symfony2 entity field type alternatives to “property” or “__toString()”?

这一生的挚爱 提交于 2019-11-28 17:36:11

I found this really helpful, and I wound a really easy way to do this with your code so here is the solution

$builder->add('customers', 'entity', array(
'multiple' => true,
'class'    => 'AcmeHelloBundle:Customer',
'property' => 'label',
));

And in the class Customer (the Entity)

public function getLabel()
{
    return $this->lastname .', '. $this->firstname .' ('. $this->email .')';
}

eh voila :D the property get its String from the Entity not the Database.

Passing a closure does not work yet, but will be added to Symfony soon: https://github.com/symfony/symfony/issues/4067

It seems this can be achievable by adding following block after elseif ($this->labelPath) block in ObjectChoiceList.php.

elseif (is_callable($this->labelPath)) {
  $labels[$i] = call_user_func($this->labelPath, $choice);
}

Haven't tried it though :).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!