问题描述:

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

网友答案:

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.

相关阅读:
Top