2012-04-15 115 views
0

我正在尝试编写一个模型方法,以便将帖子附加到帖子中。数据库设置如下:使用CakePHP查找与帖子ID相关的所有主题

Post 
id 
title 

Topic 
id 
title 

Post_Topic 
id 
post_id 
topic_id 

和实例方法...

public function getPostTopics($postId) 
{ 
    $topics = $this->find('all', ... 
    return $topics; 
} 

我需要做的是找到在DB的关系,然后将它们存储在为以下格式返回例如tag1, tag2, tag3

谁能帮助我吗?

这里有关联:

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

    public $belongsTo = 'User'; 

    public $hasMany = array('Answer'); 

    // Has many topics that belong to topic post join table... jazz 
    public $hasAndBelongsToMany = array(
     'Topic' => array('with' => 'TopicPost') 
    ); 
} 

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

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

以及它是如何保存到数据库为例(为模型中的一个功能是如何工作的想法)摆在首位(另一个有用的礼貌人在SO)

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

     // Make it all lowercase for consistency of tag names 
     $topic = strtolower($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)); 

编辑:这是下面的Joep:

array(
    (int) 0 => array(
     'id' => '2', 
     'title' => 'amazing', 
     'TopicPost' => array(
      'id' => '2', 
      'topic_id' => '2', 
      'post_id' => '107' 
     ) 
    ), 
    (int) 1 => array(
     'id' => '1', 
     'title' => 'awesome', 
     'TopicPost' => array(
      'id' => '1', 
      'topic_id' => '1', 
      'post_id' => '107' 
     ) 
    ), 
    (int) 2 => array(
     'id' => '3', 
     'title' => 'jazz', 
     'TopicPost' => array(
      'id' => '3', 
      'topic_id' => '3', 
      'post_id' => '107' 
     ) 
    ) 
) 
+0

您可以发布相关模型之间的关联并指定您实际想要获取的内容吗?是“主题”,而不是“标签”,还是实际上是相同的?你可以发表你想要你的返回数组的样子的例子数组吗? – Joep 2012-04-16 05:21:27

+0

查看更新OP。我已经添加了很多代码来显示事情的工作方式。 – Cameron 2012-04-16 10:10:51

+0

至于回报。我希望以'tag1,tag2,tag3'的格式返回一个字符串,因为它将用于编辑视图上textarea的值。 – Cameron 2012-04-16 10:18:26

回答

1

我会假设这个函数在PostModel中。

function getTagsByPost($postId) { 
    $this->Behaviors->attach('Containable') //Remove this if you're already using ContainableBehavior 
    $result = $this->find(
     'first', 
     array(
      'conditions' => array(
       'Post.id' => $postId 
      ), 
      'contain' => array(
       'Topic' => array(
        'order' => 'Topic.title ASC' //Edit this value to change your sorting (or remove the array containing 'order' to disable sorting completely) 
       ) 
      ) 
     ) 
    ); 
    $returnString = ''; 
    foreach($result['Topic'] as $num => $topic) { 
     $returnString .= $topic['title']; 
     $nextNum = $num+1; 
     if(isset($result['Topic'][($nextNum])) { 
      $returnString .= ', '; 
     } 
    } 
    return $returnString; 
} 
+0

好吧,第一个似乎工作期望它输出'数组,数组,数组',对$ result进行调试['主题']显示内容在那里只是我们没有正确返回它。如果你检查我的OP,我已经添加了调试过的数组内容。还有删除最后一个逗号和空格的机会吗? – Cameron 2012-04-17 11:39:38

+0

查看更新的答案。 – Joep 2012-04-17 11:51:35

+0

繁荣:)非常感谢。对您的所有帮助非常感谢。 – Cameron 2012-04-17 11:59:47

相关问题