我在使用cakephp正確查詢時遇到了一些麻煩。 Cake正在試圖在我的一個表中找到一個不存在的列,我找不出原因。我有兩張桌子涉及這個;投票和帖子。用戶可以對帖子投票。cakephp正在查詢額外的列,我無法弄清楚爲什麼
下面是表
Table votes
post_id | user_id | vote
table posts
post_id | tite | body | created | modified | user_id | vote_total
當後他們的投票用戶投票被放置在票表和vote_total增加/他們的選票減少。
在帖子索引中,用戶可以通過這兩個鏈接投票發帖。現在,我已經都發送到相同的功能只是爲了獲得一個工作,然後我會完成其他
<td><?php echo $this->Html->link('Upvote', array('action' => 'vote', $post['Post']['id']))?>
<?php echo $this->Html->link('Downvote', array('action' => 'Downvote', $post['Post']['id']));?></td>
當我查看index.ctp它顯示的帖子,我收到以下錯誤
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Post.id' in 'field list'
這裏是它的預成型
SQL Query: SELECT `Post`.`post_id`, `Post`.`title`, `Post`.`body`, `Post`.`created`, `Post`.`modified`, `Post`.`user_id`, `Post`.`vote_total`, `Post`.`id` FROM `blog`.`posts` AS `Post` WHERE 1 = 1
查詢所以,我的問題是,我不知道在哪裏Post.id正在從查詢。我一直在尋找我的功能和模型,我無法弄清楚發生了什麼。
這裏的功能我postscontroller
public function vote($id=null){
$this->layout = 'votes_layout';
$this->Post->Votes->recursive = 0;
$this->Post->id = $id;
$user = $this->Auth->User();
$userid = $user['id'];
$conditions = array('votes.id' => $id, 'votes.user_id' => $userid);
$data = $this->Post->Votes->find('all', array('conditions' => $conditions));
if (isset($data) && !empty($data)) {
echo '<p>User have already give a vote!</p>';
} else {
if($this->Post->Votes->validates()){
if ($this->Post->Votes->save($this->request->data)) {
$this->Session->setFlash(__('The vote has been saved'));
$this->redirect(array('action' => 'vote'));
} else {
$this->Session->setFlash(__('The vote could not be saved. Please, try again.'));
}
}
}
}
這是在我的崗位模型
public $hasMany = array(
'Votes' => array(
'className' => 'Vote',
)
);
所以,我的問題是我在做什麼,是造成蛋糕查詢post.id?這甚至不是我桌子上的一列,我無法弄清楚查詢來自哪裏。
編輯
當我刪除我的帖子模型票的hasMany關係,即查詢錯誤消失,但後來我有以下錯誤,引用表中的每一個崗位場。
Undefined index: id
難道這不是一種多年的關係嗎?每個帖子都有很多投票,但每個用戶每個帖子只有1票。
我將它改爲-1,沒有任何改變。我檢查了可容納的信息,但我不認爲發生了任何事情。 – user1406951 2012-08-04 04:33:10