Search code examples
symfonyormentitysonata-adminsonata

Symfony, how show normal name of object not enetity number


I have problem with name of objects. In symfony 4, sonata admin. When create field autocomplete with result from other enitity i got number of this object enitity, why i dont get normal name witch is saved in database ?

                    ->add('child', ModelAutocompleteType::class, [ 
                        'property' => 'name',
                        'multiple' => 'true',
                    ])

And there : Edite App\Entity\Invest..... want there normal name. Can anyone help ?

enter image description here


Solution

  • Try implementing "magic" method __toString() in you object class:

    http://php.net/manual/en/language.oop5.magic.php#object.tostring

    and inside it generate name of you object. So, add it like:

    public function __toString()
    {
        return $this->foo;
    }
    

    Your function just has to return a string. Use variables you have in your object to generate that string.