Lets consider sfWidgetFormDoctrineChoice form select object with multiple elements:
$this->setWidgets(array( 'books' => new sfWidgetFormDoctrineChoice(array('model' => 'Book', 'expanded' => true, 'multiple' => true)), )); |
So it will obviously take all records from book table and show it as multiple list of checkboxes.
But what if you don’t need to show all values for selection (e.g. you want to show only new books or perform any other kind of filtering). sfWidgetFormDoctrineChoice object provides useful methods which help to handle such situations, e.g.
$this->setWidgets(array( 'books' => new sfWidgetFormDoctrineChoice(array('model' => 'Book', 'table_method' => 'getBooks', 'expanded' => true, 'multiple' => true)), )); |
So we just specified which records should be taken using getBooks method.
Now we should add this method to (attention!) autogenerated table class in lib/model folder:
BookTable.class.php
public function getBooks() { return Doctrine_Query::create() ->select('*') ->from('Books') ->andWhere('created_at = ? ', date('Y-m-d')) ->execute(); } |
Worth to mention that sfWidgetFormDoctrineChoice provides other methods eg to change key method, change string method, etc.
3 replies on “Custom values in sfWidgetFormDoctrineChoice”
It’s also worth to mention that using ‘query’ option instead of ‘table_method’ is likely a more elegant solution.
Massimiliano, thanks for your comment. Both post and your comment was very helpful for me !
[…] From: http://www.symfonylab.com/custom-values-in-sfwidgetformdoctrinechoice/ […]