Symfony: Doctrine version of autocompleter widget from sfFormExtraPlugin

Though it’s not yet formally documented (as this post is written) from sfFormExtraPlugin‘s readme page, there is doctrine version for autocomplete field called sfWidgetFormDoctrineJQueryAutocompleter included with the latest version of the plugin (v 1.1.1).

There’s a simple tutorial but it’s about how to use the propel version of the widget (scroll down to section titled Autocomplete). I can’t seem to find anything about for doctrine version anywhere (yet).

I’m trying to document about the doctrine version based on the tutorial. Note that the code is not tested but is just documented as reference. Feel free to comment if anything missing or if you find any error.

Pre-setup (optional)

The plugin comes with a set of jQuery files. However they may not be up-to-dated. you may wish to download the latest version and upload to web/[subdirectory of your choice]/js (not web/js!) and settings.yml config as follows:

all:
    jquery_web_dir:       /[subdirectory]
    jquery_core:          jquery-1.4.2.min.js
    jquery_sortable:      jquery-ui-1.8.custom.min.js
    jquery_autocomplete:  jquery.autocomplete.min.js

Important: the files should not be directly under web/js directory and must be under web/[subdirectory]/js for this to work.

Setup the form

Edit lib/form/doctrine/ArticleForm.class.php:

$this->widgetSchema['author_id']->setOption(
    'renderer_class', 
    'sfWidgetFormDoctrineJQueryAutocompleter'
);
$this->widgetSchema['author_id']->setOption('renderer_options', array(
  'model' => 'Author',
  'url'   => 'module/ajax', // example only
));

Note line 7: url can be defined as $this->getOption(‘url’) and this option must be passed to the form in executeEdit action.

Setup the model

Edit lib/model/doctrine/AuthorTable.class.php:

function retrieveForSelect($q, $limit) {
    $q = Doctrine_Query::create()
        ->from('Author')
        ->andWhere('name like ?', '%' . $q . '%')
        ->addOrderBy('name')
        ->limit($limit);
    $authors = array();
    foreach ($q->execute() as $author) {
        $authors[$author->getId()] = (string) $author;
    }
    return $authors;
}

Setup module action

Edit /path/to/your/module/actions/actions.class.php:

public function executeAjax($request)
{
    $this->getResponse()->setContentType('application/json');
 
    $authors = Doctrine::getTable('Author')->retrieveForSelect(
                $request->getParameter('q'), 
                $request->getParameter('limit')
    );
 
    return $this->renderText(json_encode($authors));
}

Disclaimer

As mentioned, codes are not tested and are for references only.

11 thoughts on “Symfony: Doctrine version of autocompleter widget from sfFormExtraPlugin

  1. tsick Reply

    Works fine for me, although the problem I’m having is when you want to add a new value and not select from the auto completed values.. the new value doesnt bind to the form..

    author_id is a hidden field the value is only set when a value is selected from auto complete.

    Any ideas?

  2. Coder333 Reply

    It doesn’t work for me…for what symfony release is it written?
    🙁

  3. Colin Reply

    In the section “setup the model”, their is a typo on line 7 of the code for “lib/model/doctrine/AuthorTable.class.php”.

    It should be array() not aray().

  4. Orlando Reply

    the autocomplete works fine, but, when I try to insert or edit a record the following message appears:

    “500 | Internal Server Error | Doctrine_Table_Exception”

    “Invalid expression found: Id”

    Some idea?

  5. Fenix Reply

    Change/ Add method_for_query:

    $this->widgetSchema[‘your_key’]->setOption(‘renderer_options’, array(
    ‘model’ => ‘userShop’,
    ‘url’ => ‘/ajax’,
    ‘method_for_query’ => ‘findOneBy your_key’// example only
    ));

  6. talles Reply

    0
    down vote
    I every body.

    Please can some body help me by giving me step by step how to implement this autocomplete in symfony 1.4 .

    Please

    Thanx in advance

Leave a Reply

Your email address will not be published. Required fields are marked *