簡體   English   中英

cakephp正在查詢額外的列,我不知道為什么

[英]cakephp is querying extra column and I can't figure out why

我在獲取cakephp正確查詢時遇到了一些麻煩。 Cake試圖在我的一張表中找到一個不存在的列,但我不知道為什么。 我有兩個與此有關的表; 投票和職位。 用戶可以對帖子投票。

這是桌子

Table votes
post_id |  user_id | vote

table posts
post_id | tite | body | created | modified  | user_id | 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。 我一直在搜索我的函數和模型,但無法弄清楚發生了什么。

這是我的后控制器的功能

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? 它甚至不在我的表中的列,而且我無法弄清楚查詢的來源。

編輯

當我在Posts模型中刪除hasMany投票關系時,查詢錯誤消失了,但是隨后出現以下錯誤,該錯誤引用了表中的每個帖子字段。

Undefined index: id 

這不是倉促的關系嗎? 每個帖子都有很多票,但是每個用戶每個帖子只有1票。

假設您遵循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 (例如Containable Component)(請參見CakePHP的書)來獲取數據-這可能會有所幫助。

我不確定這是否有幫助,當我看到Post.id丟失時,突然從我腦海中Post.id 請讓我知道它是否有效:)

試試這個東西:

您的帖子數據格式不正確:

$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.'));
    }

您需要在postsvotes表中都包含id字段。

並且在您的posts表中,您具有post_id字段,但是對於您而言,這不是必需的。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM