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

时间:2012-04-15 22:29:57

标签: php 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'
    );
}

以及如何将其保存到数据库(以便了解模型中的函数如何工作)的示例(首先由另一位有帮助的人提供)

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

1 个答案:

答案 0 :(得分: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;
}