2011-04-28 56 views
0

比方說,我們有一個文章模型和一個評論模型。重新使用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個cruds每個模型。現在,在文章的展示動作中,我想展示一篇文章,它是相關的評論和一個添加評論的表單。

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(); 

    } 
} 

的問題是如何重新註釋/新評論/張貼在文章/ show動作時評論創建操作?這是組織代碼的正確方法嗎?

+0

你是指什麼評論/新和評論/創建? – Tom 2011-04-29 21:39:19

+0

它是模塊/動作 – Dziamid 2011-04-30 12:19:55

+0

我明白了。至少,將評論創建代碼移到評論模型中,以便它可以在任何地方訪問:$ comment-> saveThisCommentThroughMyModel($ data)。根據你上面發佈的內容,不知道還有什麼要說的。 – Tom 2011-04-30 17:11:59

回答

1

如果您想重新使用操作,可能需要一個組件。 組件類似於partials,但是當你必須添加一些邏輯(比如你在comment/new或comment/create的action中使用的代碼)時,你使用了一個組件。

組件就像一個動作,除了 它更快。 組件的邏輯被保存在繼承自sfComponents的類 中,其位於 位於actions/components.class.php 文件中。其演示文稿保存在 部分。

check here the docs of Symfony

的文檔是Symfony的1.2,我用在Symfony的1.4沒有問題

我真的相信一個組件是你在找什麼。