开发者

Reusing actions in symfony

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?


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).

A component is like an action, except it's much faster. The logic of a component is kept in a class inheriting from sfComponents, located in an actions/components.class.php file. Its presentation is kept in a partial.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜