重用 symfony 中的动作
假设我们有一个文章模型和一个评论模型。
Article:
columns:
body: text
Comment:
columns:
article_id: integer
message: text
relations:
Article:
local: article_id
foreign: id
foreignAlias: Comments
我们根据“文章”和“评论”路线集合生成 2 个模型:
article:
class: sfDoctrineRouteCollection
options:
module: article
model: Article
comment:
class: sfDoctrineRouteCollection
options:
module: comment
model: Comment
因此,每个模型基本上都有 2 个 CRUD。现在,在文章的显示操作中,我想显示一篇文章、它的相关评论和用于添加评论的表单。
class articleActions extends sfActions
{
public function executeShow(sfWebRequest $request)
{
$this->article = $this->getRoute()->getObject();
$this->comments = Doctrine::getTable('Comment')->findAllByArticleId ($this->article->getId());
$this->form = new CommentForm();
}
}
问题是在文章/显示操作中发布评论时如何重用评论/新建和评论/创建操作?这是组织代码的正确方法吗?
Let's say we have an Article model and a Comment model.
Article:
columns:
body: text
Comment:
columns:
article_id: integer
message: text
relations:
Article:
local: article_id
foreign: id
foreignAlias: Comments
And we generate 2 models based on "article" and "comment" route collections:
article:
class: sfDoctrineRouteCollection
options:
module: article
model: Article
comment:
class: sfDoctrineRouteCollection
options:
module: comment
model: Comment
So, we basically have 2 cruds for each model. Now, in article's show action I would like to display an article, it's related comments and a form to add a comment.
class articleActions extends sfActions
{
public function executeShow(sfWebRequest $request)
{
$this->article = $this->getRoute()->getObject();
$this->comments = Doctrine::getTable('Comment')->findAllByArticleId ($this->article->getId());
$this->form = new CommentForm();
}
}
The question is how can I reuse comment/new and comment/create actions when posting comments in article/show action? Is this the right way to organise the code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想重用操作,您可能需要一个组件。
组件与部分类似,但是当您必须向组件添加一些逻辑时(例如在注释/新建或注释/创建操作中使用的代码),您可以使用组件。
在这里查看 Symfony 的文档:
文档适用于 Symfony 1.2,我在 Symfony 1.4 中使用它没有任何问题
,我确信组件就是您正在寻找的。
If you want to reuse actions, it could be possible that you need a component.
Component are similar to partials, but you use a component when you have to add some logic to it (like the code you use in the action of comment/new or comment/create).
check here the docs of Symfony :
the docs are for Symfony 1.2, I use that in Symfony 1.4 without problems
I am really sure that a Component is what you are looking for.