CakePHP 2.1使用DBMS函数时查找命令的奇怪应答格式

CakePHP 2.1使用DBMS函数时查找命令的奇怪应答格式,cakephp,find,cakephp-2.1,Cakephp,Find,Cakephp 2.1,我有下面的搜索查询(需要有一篇文章的年/月和长度,以便在添加评论后进行适当的重定向) 这就是我通过Debugger::dump($post)得到的结果: 难道不是更符合逻辑的事情吗: array( (int) 0 => array( 'Post' => array( 'slug' => 'this-is-a-second-test-post-in-the-category-internet', 'year' => '2012',

我有下面的搜索查询(需要有一篇文章的年/月和长度,以便在添加评论后进行适当的重定向)

这就是我通过
Debugger::dump($post)得到的结果

难道不是更符合逻辑的事情吗:

array(
(int) 0 => array(
    'Post' => array(
        'slug' => 'this-is-a-second-test-post-in-the-category-internet',
        'year' => '2012',
        'month' => '6'
     )      
  )
)
如果您需要该信息,“published”是一种自定义查找类型,定义如下:

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;
}
protected函数\u findppublished($state,$query,$results=array()){
如果($state=='before'){
$query['conditions']['Post.is_archived']=false;

$query['conditions'][='Post.date\u published这是CakePHP中的情况,当您在查询中添加字段时,我相信您可以使用
MyModel\u MyField
的正确别名命名约定使它们成为主数据数组的一部分:

$settingsForPostQuery = array(
        'fields' => array(
            'Post.slug',
            'YEAR(Post.date_published) as Post__year',
            'MONTH(Post.date_published) as Post__month'
        ),
        'conditions' => array('Post.id' => $this -> request -> params['post_id'])
    );

顺便说一句,您应该查看查询的模型的限制,而不是解除它们的绑定:)

这就是CakePHP中在查询中添加字段时发生的情况,我相信您可以使用正确的别名命名约定
MyModel\uu MyField
,使它们成为主数据数组的一部分:

$settingsForPostQuery = array(
        'fields' => array(
            'Post.slug',
            'YEAR(Post.date_published) as Post__year',
            'MONTH(Post.date_published) as Post__month'
        ),
        'conditions' => array('Post.id' => $this -> request -> params['post_id'])
    );

顺便说一句,您应该查看查询的模型的限制,而不是解除绑定:)

谢谢。为年和月添加虚拟字段(动态)并按照建议使用年/月后虚拟字段非常有效。另外,感谢您对Containable的提示,我们将对此进行研究。谢谢。为年和月添加虚拟字段(在运行中)并按照建议使用年后/月后功能非常有效。另外,感谢您对Containable的提示,我们将对此进行研究。
$settingsForPostQuery = array(
        'fields' => array(
            'Post.slug',
            'YEAR(Post.date_published) as Post__year',
            'MONTH(Post.date_published) as Post__month'
        ),
        'conditions' => array('Post.id' => $this -> request -> params['post_id'])
    );