【发布时间】:2011-08-04 19:40:36
【问题描述】:
假设我有一个网站,我可以在其中创建文章并编辑现有文章。创建新文章的模板页面与编辑现有文章的模板页面相同。唯一的区别是用于编辑的表单已经添加了默认值/现有值。
我试图弄清楚如何对两条路线使用相同的操作。这是我的路线:
article_new
url: /article/new/
class: sfDoctrineRoute
options:
model: MyArticle
type: object
param:
module: article
action: edit
article_edit
url: /article/edit/:id/
class: sfDoctrineRoute
options:
model: MyArticle
type: object
param:
module: article
action: edit
requirements:
id: /d+
所以两篇文章都指向以下行动:
public function executeEdit(sfWebRequest $request)
{
$article = $this->getRoute()->getObject();
$this->form = new MyForm( $article );
// Binding and validation stuff here
// .....
}
当您想要编辑文章时,这非常有用。 getRoute()->getObject() 自动从id slug 中为您获取正确的文章,并将这些值作为默认值传递到表单中。完美。
但是使用article_new 路由,它就不能很好地工作了。 getRoute()->getObject() 返回我的数据库表中的第一篇文章,即使我没有为 url 提供任何类型的 id 参数。因此,数据库中第一篇文章的信息作为默认值传递到表单中。我显然不想要这个。
我还尝试从 article_new 路由中删除 class: sfDoctrineRoute 东西,然后是我正在使用的 getRoute()->getObject() fails because it's no longer ansfRoute` 对象。
这里最好的方法是什么?我想一次性完成所有操作,以节省编程时间和未来的维护时间。
我还发现一个解决方案是我可以检查$request->getParameter('id') 是否为null,如果是,则$article = array(),否则使用getRoute()->getObject() 检索文章。这似乎工作正常,但我认为可能会有更少的 hacky 解决方案。
【问题讨论】:
标签: php symfony1 symfony-forms