elitalon
elitalon

Reputation: 9447

Custom values in sfWidgetFormDoctrineChoice

I have a User model with name and surname columns. I'm trying to create a selector in a form using sfWidgetFormDoctrineChoice, which by default only display the name values.

How can I modify the widget to display both the name and the surname? I know you can pass a query option when initializing the widget, but I cannot make it work:

$this->setWidget('user_id', new sfWidgetFormDoctrineChoice(array(
    'model' => $this->getRelatedModelName('User'),
    'query' => Doctrine_Query::create()->select('u.name, u.surname')->from('User u'),
    'add_empty' => false)
));

Thanks!

Upvotes: 1

Views: 7384

Answers (1)

Blair McMillan
Blair McMillan

Reputation: 5349

Use the method option and create a public function of whatever method name that you want in your User model. An example might be:

$this->setWidget('user_id', new sfWidgetFormDoctrineChoice(array(
    'model' => $this->getRelatedModelName('User'),
    'method' => 'getFullName',
    'add_empty' => false)
));

And then in your lib/model/Doctrine/User.class.php file

public function getFullName() {
    return "{$this->getName()} {$this->getSurname()}";
}

Upvotes: 9

Related Questions