CakePHP 2.x从HABTM表中获取数据

时间:2016-03-20 11:19:54

标签: php cakephp has-and-belongs-to-many

我的CakePHP应用程序中有3个模型,它们遵循CakePHP命名约定与HABTM关联链接:posts - post_tag_links - tags

在我的前端postController.php文件中,我想查询所有帖子及其相关标签。通过我的查询,我可以从PostTagLink模型的Post中获取信息,但我现在不知道如何获得Tag模型的信息,因此我可以获得标签标题和标签slugs。这是我的尝试:

$this->Post->virtualFields = array(
    'date_published' => 'DATE_FORMAT(Post.published, "%d/%m/%Y")',
    'time_published' => 'DATE_FORMAT(Post.published, "%ku%i")',
    'day' => 'DATE_FORMAT(Post.published, "%d")',
    'month' => 'DATE_FORMAT(Post.published, "%m")',
    'year' => 'DATE_FORMAT(Post.published, "%Y")',
    'hours' => 'DATE_FORMAT(Post.published, "%k")',
    'minutes' => 'DATE_FORMAT(Post.published, "%i")'
);

$this->Paginator->settings = array(
    'fields' => array(
        'Post.id',
        'Post.title',
        'Post.content',
        'Post.published',
        'Post.slug',
        'Post.date_published',
        'Post.time_published',
        'Post.day',
        'Post.month',
        'Post.year'
    ),
    'conditions' => array(
        'Post.published <' => date('Y-m-d H:i:s'),
        'Post.show' => 'Y',
        'Post.deleted' => null
    ),
    'order' => array(
        'Post.published' => 'DESC',
        'Post.id' => 'DESC',
    ),
    'limit' => 10
);

$posts = $this->Paginator->paginate(
    'Post'
);

此代码的输出已接近但不够接近:

array(
    (int) 0 => array(
        'Post' => array(
            'id' => '1',
            'title' => 'Eerste post',
            'content' => 'Content 1',
            'published' => '2016-03-16 18:56:00',
            'slug' => 'eerste-post',
            'date_published' => '16/03/2016',
            'time_published' => '18u56',
            'day' => '16',
            'month' => '03',
            'year' => '2016'
        ),
        'PostTagLink' => array(
            (int) 0 => array(
                'id' => '30',
                'post_id' => '1',
                'tag_id' => '1',
                'created' => '2016-03-05 14:05:48',
                'modified' => '2016-03-05 14:05:48',
                'deleted' => null
            ),
            (int) 1 => array(
                'id' => '31',
                'post_id' => '1',
                'tag_id' => '3',
                'created' => '2016-03-05 15:10:53',
                'modified' => '2016-03-05 15:10:53',
                'deleted' => null
            ),
            (int) 2 => array(
                'id' => '32',
                'post_id' => '1',
                'tag_id' => '2',
                'created' => '2016-03-05 15:10:53',
                'modified' => '2016-03-05 15:10:53',
                'deleted' => null
            ),
            (int) 3 => array(
                'id' => '36',
                'post_id' => '1',
                'tag_id' => '5',
                'created' => '2016-03-20 01:59:41',
                'modified' => '2016-03-20 01:59:41',
                'deleted' => null
            )
        )
    ),
    (int) 1 => array(
        'Post' => array(
            'id' => '2',
            'title' => 'Tweede post',
            'content' => 'Content 2',
            'published' => '2016-02-29 18:59:00',
            'slug' => 'tweede-post',
            'date_published' => '29/02/2016',
            'time_published' => '18u59',
            'day' => '29',
            'month' => '02',
            'year' => '2016'
        ),
        'PostTagLink' => array(
            (int) 0 => array(
                'id' => '37',
                'post_id' => '2',
                'tag_id' => '6',
                'created' => '2016-03-20 01:59:56',
                'modified' => '2016-03-20 01:59:56',
                'deleted' => null
            )
        )
    ),
    (int) 2 => array(
        'Post' => array(
            'id' => '3',
            'title' => 'Derde post',
            'content' => 'Content 3',
            'published' => '2016-01-22 19:00:00',
            'slug' => 'derde-post',
            'date_published' => '22/01/2016',
            'time_published' => '19u00',
            'day' => '22',
            'month' => '01',
            'year' => '2016'
        ),
        'PostTagLink' => array(
            (int) 0 => array(
                'id' => '26',
                'post_id' => '3',
                'tag_id' => '4',
                'created' => '2016-01-23 14:12:52',
                'modified' => '2016-01-23 14:12:52',
                'deleted' => null
            ),
            (int) 1 => array(
                'id' => '34',
                'post_id' => '3',
                'tag_id' => '1',
                'created' => '2016-03-09 22:24:33',
                'modified' => '2016-03-09 22:24:33',
                'deleted' => null
            )
        )
    ),
    (int) 3 => array(
        'Post' => array(
            'id' => '4',
            'title' => 'Vierde post',
            'content' => 'Content 4',
            'published' => '2016-01-11 19:00:00',
            'slug' => 'vierde-post',
            'date_published' => '11/01/2016',
            'time_published' => '19u00',
            'day' => '11',
            'month' => '01',
            'year' => '2016'
        ),
        'PostTagLink' => array(
            (int) 0 => array(
                'id' => '33',
                'post_id' => '4',
                'tag_id' => '1',
                'created' => '2016-03-09 22:24:25',
                'modified' => '2016-03-09 22:24:25',
                'deleted' => null
            )
        )
    ),
    (int) 4 => array(
        'Post' => array(
            'id' => '5',
            'title' => 'Vijfde post',
            'content' => 'Content 5',
            'published' => '2015-05-23 09:54:00',
            'slug' => 'vijfde-post',
            'date_published' => '23/05/2015',
            'time_published' => '9u54',
            'day' => '23',
            'month' => '05',
            'year' => '2015'
        ),
        'PostTagLink' => array(
            (int) 0 => array(
                'id' => '25',
                'post_id' => '5',
                'tag_id' => '2',
                'created' => '2016-01-23 14:11:22',
                'modified' => '2016-01-23 14:11:22',
                'deleted' => null
            ),
            (int) 1 => array(
                'id' => '27',
                'post_id' => '5',
                'tag_id' => '4',
                'created' => '2016-01-23 14:14:11',
                'modified' => '2016-01-23 14:14:11',
                'deleted' => null
            ),
            (int) 2 => array(
                'id' => '29',
                'post_id' => '5',
                'tag_id' => '1',
                'created' => '2016-02-27 17:02:02',
                'modified' => '2016-02-27 17:02:02',
                'deleted' => null
            ),
            (int) 3 => array(
                'id' => '38',
                'post_id' => '5',
                'tag_id' => '5',
                'created' => '2016-03-20 02:02:14',
                'modified' => '2016-03-20 02:02:14',
                'deleted' => null
            ),
            (int) 4 => array(
                'id' => '39',
                'post_id' => '5',
                'tag_id' => '7',
                'created' => '2016-03-20 02:20:34',
                'modified' => '2016-03-20 02:20:34',
                'deleted' => null
            )
        )
    )
)

如您所见,没有其他标签信息可用:我只获得了链接表信息。我的一个尝试是将连接添加到查询中:

$this->Post->virtualFields = array(
    'date_published' => 'DATE_FORMAT(Post.published, "%d/%m/%Y")',
    'time_published' => 'DATE_FORMAT(Post.published, "%ku%i")',
    'day' => 'DATE_FORMAT(Post.published, "%d")',
    'month' => 'DATE_FORMAT(Post.published, "%m")',
    'year' => 'DATE_FORMAT(Post.published, "%Y")',
    'hours' => 'DATE_FORMAT(Post.published, "%k")',
    'minutes' => 'DATE_FORMAT(Post.published, "%i")'
);

$this->Paginator->settings = array(
    'fields' => array(
        'Post.id',
        'Post.title',
        'Post.content',
        'Post.published',
        'Post.slug',
        'Post.date_published',
        'Post.time_published',
        'Post.day',
        'Post.month',
        'Post.year'
    ),
    'joins' => array(
        array(
            'table' => 'post_tag_links',
            'alias' => 'PostTagLink',
            'type' => 'inner',
            'conditions' => 'Post.id = PostTagLink.post_id'
        ),
        array(
            'table' => 'tags',
            'alias' => 'Tag',
            'type' => 'inner',
            'conditions' => 'Tag.id = PostTagLink.tag_id'
        )
    ),
    'conditions' => array(
        'Post.published <' => date('Y-m-d H:i:s'),
        'Post.show' => 'Y',
        'Post.deleted' => null,
        $tagCondition
    ),
    'order' => array(
        'Post.published' => 'DESC',
        'Post.id' => 'DESC',
    ),
    'limit' => 999
);

$posts = $this->Paginator->paginate(
    'Post'
);

这里出现的问题是我在查询结果中多次使用相同的帖子,当然要返回标签信息:P。

任何可以帮我解决这个问题的人? ;)

修改

以下是我的模型声明。

post.php中

App::uses('AppModel', 'Model');
class Post extends AppModel
{
    public $hasMany = array(
        'PostComment' => array(
            'className' => 'PostComment',
            'foreignKey' => 'post_id',
            'dependent' => true
        ),
        'PostPhoto' => array(
            'className' => 'PostPhoto',
            'foreignKey' => 'post_id',
            'order' => 'PostPhoto.sequence ASC',
            'conditions' => array(
                'PostPhoto.show' => 'Y'
            ),
            'dependent' => true
        ),
        'PostTagLink' => array(
            'className' => 'PostTagLink',
            'foreignKey' => 'post_id'
        )
    );
}

PostTagLink.php

App::uses('AppModel', 'Model');
class PostTagLink extends AppModel
{
    public $belongsTo = array(
        'Post' => array(
            'className' => 'Post',
            'foreignKey' => 'post_id'
        ),
        'Tag' => array(
            'className' => 'Tag',


    'foreignKey' => 'tag_id'
    )
);

}

Tag.php

App::uses('AppModel', 'Model');
class Tag extends AppModel
{
    public $hasMany = array(
        'PostTagLink' => array(
            'className' => 'PostTagLink',
            'foreignKey' => 'tag_id'
        )
    );
}

0 个答案:

没有答案