Cakephp查找所有数据库错误

时间:2012-08-02 13:30:05

标签: cakephp

我正在尝试使我的index()函数仅显示属于该用户登录用户的ExpenseClaims。

这是功能:

public function index() {
    $this->ExpenseClaim->recursive = 0;
    $currentUsersClaims = $this->ExpenseClaim->find('all', array('conditions' => array('ExpenseClaim.user_id ==' => $this->Auth->user('id'))));
    $this->set('currentUsersClaims', $this->paginate());
}

这是我得到的错误:

数据库错误

错误:SQLSTATE [42000]:语法错误或访问冲突:1064 SQL语法中有错误;检查与MySQL服务器版本对应的手册,以便在第1行的“== 3”附近使用正确的语法

SQL Query: SELECT `ExpenseClaim`.`id`, `ExpenseClaim`.`user_id`, `ExpenseClaim`.`claim_status_id`, `ExpenseClaim`.`created`, `ExpenseClaim`.`modified`, `ExpenseClaim`.`approved`, `ExpenseClaim`.`approved_by`, `ExpenseClaim`.`date_submitted`, `User`.`id`, `User`.`username`, `User`.`password`, `User`.`group_id`, `User`.`name`, `User`.`address`, `User`.`department_id`, `User`.`mileage_rate`, `User`.`created`, `User`.`modified`, `ClaimStatus`.`id`, `ClaimStatus`.`name`, `ClaimStatus`.`created`, `ClaimStatus`.`modified` FROM `expenses`.`expense_claims` AS `ExpenseClaim` LEFT JOIN `expenses`.`users` AS `User` ON (`ExpenseClaim`.`user_id` = `User`.`id`) LEFT JOIN `expenses`.`claim_statuses` AS `ClaimStatus` ON (`ExpenseClaim`.`claim_status_id` = `ClaimStatus`.`id`) WHERE `ExpenseClaim`.`user_id` == 3 

原谅我,因为我可能错过了一些显而易见的东西,但我很擅长蛋糕。

3 个答案:

答案 0 :(得分:2)

首先,错误是因为你使用了一个双等于==,这在MySQL中是无效的(单等号就可以了)。此外,我注意到您正在尝试对这些结果进行分页,您实际上只需将您想要的条件传递给分页查询:

$this->set(
    'currentUsersClaims',
    $this->paginate('ExpenseClaim', array(
        'ExpenseClaim.user_id' => $this->Auth->user('id')
    ))
);

请务必阅读本书中的pagination部分。

答案 1 :(得分:2)

==在查询n_n

中不正确

您的查询应该是

public function index() {
    $this->ExpenseClaim->recursive = 0;
    $currentUsersClaims = $this->ExpenseClaim->find('all', 
              array('conditions' => array('ExpenseClaim.user_id' => $this->Auth->user('id'))));
    $this->set('currentUsersClaims', $this->paginate());
}

希望这有帮助

答案 2 :(得分:0)

'ExpenseClaim.user_id =='

应该是

ExpenseClaim.user_id =

编辑:我在输入时的两个答案......:)