symfony: doctrine version of autocompleter widget from sfFormExtraPlugin

by prettyscripts on 2010-04-30 11:48:35

phpframeworksymfony

though it's not yet formally documented (as this post is written) from sfFormExtraPlugin's readme page, there 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:

Code:

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':

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':

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':

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.

Tags: autocomplete, doctrine, symfony

9 comments

Comment by Massimo @ 2010-05-07 01:45:55
**---
It doesn't work for me :(
Comment by prettyscripts @ 2010-05-07 01:56:02
which part is not working?
Comment by tsick @ 2010-06-18 13:17:31
****-
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?
Comment by Coder333 @ 2011-02-15 00:30:00
It doesn't work for me...for what symfony release is it written?
:(
Comment by Colin @ 2011-02-17 08:33:54
****-
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().
Comment by prettyscripts @ 2011-02-17 09:42:19
@Colin, thanks for spotting the typo. updated.
Comment by Orlando @ 2011-03-30 14:54:39
***--
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?
Comment by Fenix @ 2011-06-14 18:58:07
*****
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
));

Comment by christian @ 2011-07-07 00:14:26
*****
It works for me! It's i need

thanks.

Leave a comment


Your email address will not be revealed on this site.
PoorExcellent
note: all comments are moderated. do not spam and do not advertise. only comments relevant to the post will be published.
(Line breaks become <br />)
(For my next comment on this site)
(Allow users to contact me through a message form -- Your email will not be revealed!)