7

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.

Dario
  • 618
  • 1
  • 11
  • 28

3 Answers3

26

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();
    },
])
Yoshi
  • 54,081
  • 14
  • 89
  • 103
3

Not sure if this works with Symfony 4 but it does work with symfony 5. This is more of an extension of Yoshis' answer than a new answer.

If you create a getFullname() function as suggested by Yoshi you can do the following.

Customer.php:

public function getFullname(): ?string {
    return $this->firstname . ' ' . $this->surname;
}

Then in your form:

->add('customer', EntityType::class, [
    'label' => 'Kunde: ',
    'class' => Customer::class,
    'choice_label' => 'fullname'
])

Adding the fullname function to the customer also allows you to use fullname within your twig files.

eg.

{{ customer.fullname }}
Syscall
  • 19,327
  • 10
  • 37
  • 52
Tim
  • 31
  • 2
2

Have you created a __toString() method in your customer entity.

/**
 * toString
 *
 * @return string
 */
public function __toString() {
    return $this->getFirstname().' '.$this->getSurname();
}

Then, something like this should be enough :

->add('customer')

If customer is related to your entityType, this should be enough

Preciel
  • 2,666
  • 3
  • 20
  • 45
  • You meant `CONCAT` right? Now I got this error: `Error: Expected StateFieldPathExpression | string | InputParameter | FunctionsReturningStrings | AggregateExpression, got '"'` – Dario Jan 29 '18 at 15:41
  • yeah, I meant `CONCAT` sorry... As for your current eror, we would need more details, I think you should edit and update your current question. – Preciel Jan 29 '18 at 15:47
  • I updated my question. What details do you need, the Entity, Controller, Stack Trace? – Dario Jan 29 '18 at 15:55
  • @Dario can you also add your user entity?! (just the parameters, no need of the getters/setters) – Preciel Jan 29 '18 at 18:20
  • I edited my question and added the `customer` entity. – Dario Jan 30 '18 at 08:32
  • @Dario Edited my answer... This one is more simple if there is a relation between the two entities – Preciel Jan 30 '18 at 11:35