Ask your Symfony questions! Pay money and get answers fast! (more info)

Name instead of the number, to show up for jquery autocomplete? Symfony


Symfony 1.4 and Doctrine.

I have a wine form with a foreign key relationship to the winery table, and I'm using sfWidgetFormJQueryAutocompleter so the user can start typing a winery name and the names of possibly wineries will be suggested. Everything works great except that when the page first loads, the user sees the numeric id of the winery instead of the name of the winery. Please see the attached screenshot and you'll see what I mean.

In the form class I have:

$url = url_for('winery/autocomplete');
$this->widgetSchema['winery_id'] = new sfWidgetFormJQueryAutocompleter(array(
'url' => $url,

And I have this action code, which works great:

public function executeAutocomplete(sfWebRequest $request)
$q = "%" . $request->getParameter('q') . "%";
$limit = $request->getParameter('limit');

// FIXME: use $limit
$dql = Doctrine_Query::create()
->from('Winery w')
->where(' LIKE ? ', array($q));
$this->rows = $dql->fetchArray();

$person = array();
foreach ($this->rows as $row) {
$persons[$row['id']] = (string) $row['name'];

return $this->renderText(json_encode($persons));

But how do I get it so that the form shows the name of the winery, not the id, when the page first loads?

Answers (2)


Marcos IbaƱez answers:

If you take a peek into sfWidgetFormJqueryAutocompleter.class.php you'll see that you have 2 options to specify the first value to be displayed by de form:

1. Specify a callback that produces such value in the form, in your case it'll be something like:

$this->widgetSchema['winery_id'] = new sfWidgetFormJQueryAutocompleter(array(
'url' => $url,
'value_callback' => 'thisFunctionShouldReturnTheNameOfTheWinery'

2. And if you have personalized templates and you are rendering the form "manually" (calling the render, renderError and renderLabel methods of each field in the template) I think the best would be to pass the value you want to display initially to the render method:

$form['winery_id']->render('winery_id', 'theNameOfTheFirstWineryToShow');

If you choose the second way, DON'T specify the value_callback because that takes precedence over manually specified values passed to the render method.


Loban Rahman answers:

Are you sure it shouldn't be:

$persons[$row['name']] = $row['id'];

'Cause that's how I remember it off the top of my head.