by prettyscripts on 2010-04-30 11:48:35
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.
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.
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.
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; | |
} |
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)); | |
} |
as mentioned, codes are not tested and are for references only.
Tags: autocomplete, doctrine, symfony