views:

454

answers:

1

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

+1  A: 

Quickest way to do it would be keeping with the existing code you have but just tweaking it slightly.

In your model, implement a method that just returns the query object for the required records, but without execute()'ing it. Basically the query object you're creating in your GetActiveReports() method (you can then refactor this method to use the new method).

Then, in your form class:

$queryObject = Doctrine::getTable("Report")->GetActiveReportsQuery();
$this->setWidget('report_id',
  new sfWidgetFormDoctrineChoice(array(
    'model' => $this->getRelatedModelName('Report'),
    'query' => $queryObject)
  )
);

The widget should then use the specified query object to retrieve the right filtered records.

richsage
Thanks this worked great! Couple follow ups, since I'm still learning the fundamentals...1. How does the query work with the model? Before I guess it figured out the query behind the scenes based on the related model? And now since I'm defining a query, it overrides it? Any tips on a doc I can look at that explains how the model and query properties work?2. Still wondering how to use FormFilters, since that seems like it would be the most elegant solution. Any tips on docs with an explanation on form filters and how they're used?Thanks!
Dan
Glad it worked :-) quick answers: 1) By default, Doctrine just retrieves all records for the given model if you don't specify a query. If you do, it uses that query to retrieve it. 2) The filter class I've only ever used in the admin generator module and not elsewhere - docs are a bit sparse unfortunately though :-(
richsage