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 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。