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条)

报告相同问题?