Symfony / Doctrine - 如何按相关模型中的属性过滤表单字段

发布于 2024-09-03 15:00:15 字数 815 浏览 2 评论 0原文

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

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

我想通过报表字段之一过滤来自此关系的报表对象,“ active”,这样只有 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 但我不清楚适当的用法。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云裳 2024-09-10 15:00:15

最快的方法是与现有的代码保持一致,但只需稍微调整一下即可。

在您的模型中,实现一个仅返回所需记录的查询对象的方法,但不对其进行 execute() 处理。基本上是您在 GetActiveReports() 方法中创建的查询对象(然后您可以重构此方法以使用新方法)。

然后,在您的表单类中:

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

小部件应该使用指定的查询对象来检索正确的过滤记录。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文