Symfony / Doctrine - How to filter form field by property in related model

Posted by Dan on Stack Overflow See other posts from Stack Overflow or by Dan
Published on 2010-05-29T23:14:43Z Indexed on 2010/05/29 23:22 UTC
Read the original article Hit count: 570

Filed under:
|
|

I have a UserForm class which has a select list populated from a related model (specified by a foreign relationship in the yml) like so:

$this->setWidget('report_id', new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Report'))));

I'd like to filter the Report objects that come from this relation by one of the Report fields, "active" such that only Reports with active=1 appear in the form.

I have a method, ReportTable::GetActiveReports() that performs the appropriate query and returns the filtered reports. So one option is to populate the Widget with the results of that function. Any tips on the syntax to do that?

It seems to me the cleaner way is to use the UserFormFilter class to filter the reports by active=1 there. Unfortunately I couldn't find any documentation on how to use form filters (or really what they are), so maybe this is not the right solution. Is a Form Filter the appropriate tool for this job? It seems I should use the Doctrine_Record_Filter_Standard class as defined here: http://www.doctrine-project.org/api/orm/1.2/doctrine/doctrine_record_filter_standard.html But it's not clear to me the appropriate usage.

Any guidance would be helpful. Thanks! Dan

© Stack Overflow or respective owner

Related posts about symfony

Related posts about doctrine