2012-08-04 44 views
0

我在使用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票。

回答

0

假設您遵循CakePHP約定,並且在所有表中都有id

由於您致電$this->Post->Vote->recursive = 0,CakePHP將選擇與您的Vote相關的所有Post。由於您有Post hasMany Vote的關係,因此當它通過$this->Post->Vote->find()選擇投票時,它將首先檢查Vote.post_id外鍵。

然後,爲了選擇Post,它將不得不在Posts表上運行Post.id = Vote.post_id之類的查詢。這可能是Post.id的來源。

雖然它的調用Post.post_id(自然不存在,因爲我假設你不會將模型與自己關聯),這似乎很奇怪。可能調用的函數可能仍然是recursive調用,但它試圖獲取所有帖子。我不知道你的應用程序是如何編碼的,但是提示你進一步追蹤的內容是將recursive轉換爲-1並查看是否調用了該函數(不必擔心丟失數據)。如果在遞歸= -1之後沒有運行該查詢,則嘗試使用更類似Containable組件(請參閱CakePHP的書)的內容來獲取您的數據 - 這可能會有所幫助。

我不確定這是否會有所幫助,只是當我看到Post.id丟失時突然出現在我的頭上。請讓我知道,如果它的工作原理(或沒有):)

+0

我將它改爲-1,沒有任何改變。我檢查了可容納的信息,但我不認爲發生了任何事情。 – user1406951 2012-08-04 04:33:10

0

嘗試這件事情:

您的文章的數據格式不正確:

$data = array('post_id'=>$id,'user_id'=>$userid); 

if ($this->Post->Votes->save($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.')); 
    } 
0

你需要有兩個postsid場和votes表。

而且在你的posts表中你有post_id這個字段,但這對你沒有必要的情況。

因此,從posts表中刪除post_id並檢查結果。

+0

我認爲這有*與它做的事情,但我有一種感覺,我在模型中的關係是關閉的。當我在帖子和投票表中將post_id更改爲id時,cake現在查詢votes.post_id。當我將它們設置爲兩個表中的post_id時,對於votes.id的cake查詢 – user1406951 2012-08-04 14:12:58

相關問題