我有以下搜索查询(需要有年/月和帖子的标签,以便在添加评论后进行正确的重定向)。
$settingsForPostQuery = array(
'fields' => array(
'Post.slug',
'YEAR(Post.date_published) as year',
'MONTH(Post.date_published) as month'
),
'conditions' => array('Post.id' => $this -> request -> params['post_id'])
);
// unbind post model to fetch only necessary stuff
$this -> Comment -> Post -> unbindModel(array(
'hasMany' => array('Comment'),
'hasAndBelongsToMany' => array('Tag'),
'belongsTo' => array('Category')
));
$post = $this -> Comment -> Post -> find('published', $settingsForPostQuery);
这就是我得到的Debugger::dump($post);
:
array(
(int) 0 => array(
'Post' => array(
'slug' => 'this-is-a-second-test-post-in-the-category-internet'
),
(int) 0 => array(
'year' => '2012',
'month' => '6'
)
)
)
不是更合乎逻辑吗:
array(
(int) 0 => array(
'Post' => array(
'slug' => 'this-is-a-second-test-post-in-the-category-internet',
'year' => '2012',
'month' => '6'
)
)
)
如果您需要信息,“已发布”是自定义查找类型,定义如下:
protected function _findPublished($state, $query, $results = array()) {
if ($state === 'before') {
$query['conditions']['Post.is_archived'] = false;
$query['conditions'][] = 'Post.date_published <= now()';
return $query;
}
return $results;
}
编辑
我发现当只使用 Post.date_published 而不是 YEAR(Post.date_published) 时,Post.date_published 也会出现在 Post 数组中,就像 slug 一样。为什么使用 MYSQL 函数会改变这一点?