douwu5009 2018-01-29 15:08
浏览 39
已采纳

Symfony4 Forms - 带有两个choice_label的EntityType

I'm pretty new to symfony and symfony forms.

I have a form with an EntityType, that looks like this:

->add('customer', EntityType::class, [
    'label' => 'Kunde: ',
    'class' => Customer::class,
    'choice_label' => 'Name',
    'query_builder' => function(EntityRepository $er) {
        return $er->createQueryBuilder('c')
            ->select('CONCAT(c.firstname, " ", c.surname) AS Name');
    }
])

But I now get an Error/Warning:

Warning: spl_object_hash() expects parameter 1 to be object, string given

enter image description here


Customer Entity:

/**
 * @ORM\Entity(repositoryClass="App\Repository\CustomerRepository")
 */
class Customer
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
     private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="firstname", type="string", length=50, nullable=false)
     */
    private $firstname;

    /**
     * @var string
     *
     * @ORM\Column(name="surname", type="string", length=50, nullable=false)
     */
    private $surname;

    ...

Thank you very much for your time and help.

  • 写回答

2条回答 默认 最新

  • doushi3202 2018-01-30 10:01
    关注

    You could also simply use a callback for the choice_label

    E.g.:

    ->add('customer', EntityType::class, [
        'label' => 'Kunde: ',
        'class' => Customer::class,
        'choice_label' => function (Customer $customer) {
            return $customer->getFirstname() . ' ' . $customer->getSurname();
    
            // or better, move this logic to Customer, and return:
            // return $customer->getFullname();
        },
    ])
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化