0

我试图让我的 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 

请原谅我,因为我可能遗漏了一些明显的东西,但我对蛋糕很陌生。

4

3 回答 3

2

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

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

请务必阅读书中的分页部分。

于 2012-08-02T13:36:32.160 回答
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());
}

希望这可以帮助

于 2012-08-02T13:37:04.830 回答
0
'ExpenseClaim.user_id =='

应该

ExpenseClaim.user_id =

编辑:我打字时有两个答案.... :)

于 2012-08-02T13:37:25.843 回答