将postConditions与CakePHP安全组件一起使用

时间:2011-07-24 07:01:13

标签: cakephp

对于Cake in Model中的基本搜索,我一直在使用postConditions方法。在我通过安全组件启用CSRF保护之前,它运行良好。

启用此组件会生成SQL Error: 1054: Unknown column '_Token.key' in 'where clause [...],我可以看到这种情况正在发生,因为$this->data已更改:

Array (
    [CrmPerson] => Array
        (
            [firstname] => john
            [surname] => 
            [email] => 
        )
)

...至...

Array
(
    [_Token] => Array
        (
            [key] => 03aef38c3c2f631d6dc50baa98c7327a3fe6d0cd
            [fields] => 71d0fa03bc4e10f6f4d0de8f91674100836ea498%3A
        )

    [CrmPerson] => Array
        (
            [firstname] => john
            [surname] => 
            [email] => 
        )

)

换句话说,[_Token]的存在会扰乱$this->postConditions电话。

我在控制器中的整个find()函数如下所示:

function find() {
    if(!empty($this->data)) {
        $this->CrmPerson->recursive = 0;
        $conditions = $this->postConditions(
            $this->data,
            array(
                'firstname' => 'LIKE',
                'surname' => 'LIKE',
                'email' => 'LIKE'
            )
        );

        $this->paginate = array(
            'order' => 'CrmPerson.created DESC',
            'limit' => '40',
        );
        $this->set('crmPeople', $this->paginate($conditions));
    }
}

我尝试将$this->data更改为$this->data['CrmPerson'],但postConditions方法需要数据在数组中的较高位置开始。

我是否需要创建一个全新的数组来提供postConditions,或者是否有一个我没有看到的更简单的选项?

2 个答案:

答案 0 :(得分:2)

抱歉,我的周日比较慢......只需重新阅读controller.php中的postConditions()方法@params,我就可以看到我出错了。

此:

        $conditions = $this->postConditions(
            $this->data,
            array(
                'firstname' => 'LIKE',
                'surname' => 'LIKE',
                'email' => 'LIKE'
        );

......需要......

        $conditions = $this->postConditions(
            $this->data,
            array(
                'firstname' => 'LIKE',
                'surname' => 'LIKE',
                'email' => 'LIKE'
            ),'AND',true
        );

答案 1 :(得分:1)

unset($this->data['_Token']);