1
我有以下搜索查詢(需要有一個帖子的年/月和slu in以便在添加評論後正確重定向)。CakePHP 2.1使用DBMS功能時查找命令的奇怪答案格式
$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;
}
EDIT
我發現,當僅使用Post.date_published YEAR(Post.date_published)代替,Post.date_published也出現在後陣列中,就如同蛞蝓一樣。爲什麼使用MYSQL函數改變這個?
謝謝你的男人。按照建議,爲年和月(即時)添加虛擬字段並使用Post__year/Post_month完美地工作。另外,感謝您提供Containable的提示,將會研究這一點。 – wnstnsmth