Symfony2 entity field type alternatives to “property” or “__toString()”?

后端 未结 3 1635
后悔当初
后悔当初 2020-12-13 07:05

Using Symfony2 entity field type one should specify property option:

$builder->add(\'customers\', \'entity\', array(
    \'multiple\' =>          


        
相关标签:
3条回答
  • 2020-12-13 07:56

    Passing a closure does not work yet, but will be added to Symfony soon: https://github.com/symfony/symfony/issues/4067

    0 讨论(0)
  • 2020-12-13 07:56

    It seems this can be achievable by adding following block after elseif ($this->labelPath) block in ObjectChoiceList.php.

    elseif (is_callable($this->labelPath)) {
      $labels[$i] = call_user_func($this->labelPath, $choice);
    }
    

    Haven't tried it though :).

    0 讨论(0)
  • 2020-12-13 08:07

    I found this really helpful, and I wound a really easy way to do this with your code so here is the solution

    $builder->add('customers', 'entity', array(
    'multiple' => true,
    'class'    => 'AcmeHelloBundle:Customer',
    'property' => 'label',
    ));
    

    And in the class Customer (the Entity)

    public function getLabel()
    {
        return $this->lastname .', '. $this->firstname .' ('. $this->email .')';
    }
    

    eh voila :D the property get its String from the Entity not the Database.

    0 讨论(0)
提交回复
热议问题