2012-04-15 47 views
0

數據庫表我有以下設置爲我的CakePHP應用程序:保存標籤到CakePHP中

Posts 
id 
title 
content 

Topics 
id 
title 

Topic_Posts 
id 
topic_id 
post_id 

所以基本上我的主題(標籤)的表都是獨一無二的,並有一個ID。然後可以使用Topic_Posts連接表附加它們以發佈。當用戶創建新帖子時,他們將填入主題,方法是將用逗號分隔的textarea輸入,然後將它們保存到主題表中(如果它們尚不存在),然後將引用保存到Topic_posts表中。我已經建立了像這樣的模式:

Post模型:

class Post extends AppModel 
{ 
    public $name = 'Post'; 

    public $hasAndBelongsToMany = array(
     'Topic' => array('with' => 'TopicPost') 
    ); 
} 

主題模型:

class Topic extends AppModel 
{ 
    public $hasMany = array(
     'TopicPost' 
    ); 
} 

TopicPost型號:

class TopicPost extends AppModel { 
    public $belongsTo = array(
     'Topic', 'Post' 
    ); 
} 

併爲新的POST方法我有這個到目前爲止:

public function add() 
{ 
    if ($this->request->is('post')) 
    { 
     //$this->Post->create(); 

     if ($this->Post->saveAll($this->request->data)) 
     { 

      // Redirect the user to the newly created post (pass the slug for performance) 
      $this->redirect(array('controller'=>'posts','action'=>'view','id'=>$this->Post->id)); 
     } 
     else 
     { 
      $this->Session->setFlash('Server broke!'); 
     } 
    } 
} 

正如你所看到的我已經使用saveAll但我該如何處理主題數據?

我見過類似的事情:http://bakery.cakephp.org/articles/dooltaz/2007/05/02/simple-tagging-behavior但我要做到這一點非常簡單並且更現代化(即文章日期2007年),我還使用CakePHP的2.1

回答

3

我會實現的方法這樣的主題(型號):

/** 
* This methods saves the topics coming from a post save and associates them with the right post. 
* 
* @param string $postId The post id to save associations to. 
* @param string $topics A comma seperated list of topics to save. 
* @param bool Save of all topics succeeded (true) or not (false). 
*/ 
    public function savePostTopics($postId, $topics){ 
     // Explode the topics by comma, so we have an array to run through 
     $topics = explode(',', $topics); 
     // Array for collecting all the data 
     $collection = array(); 

     foreach($topics as $topic){ 
      // Trim it so remove unwanted white spaces in the beginning and the end. 
      $topic = trim($topic); 

      // Check if we already have a topic like this 
      $controlFind = $this->find(
       'first', 
       array(
        'conditions' => array(
         'title' => $topic 
        ), 
        'recursive' => -1 
       ) 
      ); 

      // No record found 
      if(!$controlFind){ 
       $this->create(); 
       if(
        !$this->save(
         array(
          'title' => $topic 
         ) 
        ) 
       ){ 
        // If only one saving fails we stop the whole loop and method. 
        return false; 
       } 
       else{ 
        $temp = array(
         'TopicPost' => array(
          'topic_id' => $this->id, 
          'post_id' => $postId 
         ) 
        ) 
       } 
      } 
      else{ 
       $temp = array(
        'TopicPost' => array(
         'topic_id' => $controlFind['Topic']['id'], 
         'post_id' => $postId 
        ) 
       ) 
      } 
      $collection[] = $temp; 
     } 

     return $this->TopicPost->saveMany($collection, array('validate' => false)); 
    } 

我沒有測試它,但它應該工作。

你可以調用這個AFTER保存帖子本身,爲它提供帖子ID和數據數組中的主題。確保你處理該方法的返回。如果主題保存失敗,這是刪除整篇文章的原因?由於在cakephp中沒有很好的實現回滾API,如果你願意的話,你可能不得不從數據庫中刪除這個帖子。或者,您是否只向撰寫帖子並記錄錯誤的用戶提供了成功信息?

順便說一句:繼CakePHP的約定模型和關聯表也被命名爲PostTopicpost_topic。按字母順序;)您可能需要在項目的早期階段更改它。

問候 func0der

+0

真棒,工作正常。我沒有改變任何我的表名或模型名稱?謝謝;) – Cameron 2012-04-15 14:48:49

+0

不,你不需要改變它。但通過cakephp約定,您應該;) 通讀本:http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasandbelongstomany-habtm(示例下方的信息框) – func0der 2012-04-16 10:20:55