使用 CakePHP 查找与帖子 ID 相关的所有主题


Find all topics related to a post id using CakePHP

我正在尝试编写一个模型方法,该方法将提取附加到帖子的主题。数据库设置如下:

Post
id
title
Topic
id
title
Post_Topic
id
post_id
topic_id

和示例方法...

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

我需要做的是在数据库中找到关系,然后以以下格式存储它们以返回,例如 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));

编辑:这是针对下面的乔普:

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

我将假设这个函数在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;
}