3

我有一个 UserForm 类,它有一个从相关模型(由 yml 中的外部关系指定)填充的选择列表,如下所示:

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

我想通过“活动”的“报告”字段之一过滤来自此关系的报告对象,以便只有具有 active=1 的报告出现在表单中。

我有一个方法 ReportTable::GetActiveReports() 执行适当的查询并返回过滤后的报告。因此,一种选择是使用该函数的结果填充小部件。有关执行此操作的语法的任何提示吗?

在我看来,更简洁的方法是使用 UserFormFilter 类通过 active=1 过滤报告。不幸的是,我找不到任何关于如何使用表单过滤器(或者它们是什么)的文档,所以这可能不是正确的解决方案。表单过滤器是否适合这项工作?看来我应该使用此处定义的 Doctrine_Record_Filter_Standard 类:http: //www.doctrine-project.org/api/orm/1.2/doctrine/doctrine_record_filter_standard.html 但我不清楚适当的用法。

任何指导都会有所帮助。谢谢!担

4

1 回答 1

1

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.

于 2010-05-30T12:20:32.527 回答