【问题标题】:cakephp is querying extra column and I can't figure out whycakephp 正在查询额外的列,我不知道为什么
【发布时间】:2012-08-04 21:02:36
【问题描述】:

我在让 cakephp 正确查询时遇到了一些问题。 Cake 试图在我的一张桌子上找到一个不存在的列,但我不知道为什么。我有两个表与此有关;投票和帖子。用户可以对帖子进行投票。

这是表格

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 。我一直在搜索我的函数和模型,但我无法弄清楚发生了什么。

这是我的帖子控制器的功能

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',
        )
    );

所以,我的问题是我在做什么导致 cake 查询 post.id?它甚至不是我表中的一列,我无法弄清楚查询的来源。

编辑

当我在我的 Posts 模型中删除投票的 hasMany 关系时,查询错误消失了,但随后出现以下错误,它引用了表中的每个帖子字段。

Undefined index: id 

这不是hasmany关系吗?每个帖子有很多票,但每个用户每个帖子只有一票。

【问题讨论】:

    标签: php mysql cakephp


    【解决方案1】:

    假设您遵循 CakePHP 约定并且在所有表中都有 id

    由于您正在调用$this-&gt;Post-&gt;Vote-&gt;recursive = 0,CakePHP 将选择与您的Vote 相关的所有Post。由于你有Post hasMany Vote 关系,当它通过$this-&gt;Post-&gt;Vote-&gt;find() 选择投票时,它会首先检查Vote.post_id 外键。

    然后,为了选择Post,它必须在Posts 表上运行类似Post.id = Vote.post_id 的查询。这可能就是Post.id 的来源。

    但看起来很奇怪的是它调用Post.post_id(它自然不存在,因为我假设您不会将模型与其自身关联)。可能正在调用的函数可能仍然是 recursive 调用,但它正在尝试获取 所有 帖子。我不知道您的应用程序是如何编码的,但是提示您进一步跟踪的提示是将recursive 转换为-1 并查看是否调用了它(不要担心丢失数据)。如果它在 recursive=-1 后没有运行该查询,那么请尝试使用更具体的东西,例如 Containable 组件(请参阅 CakePHP 的书)来获取您的数据 - 这可能会有所帮助。

    我不确定这是否会有所帮助,当我看到Post.id 失踪时,我突然想到了这一点。请让我知道它是否有效(或无效):)

    【讨论】:

    • 我将其更改为 -1,但没有任何改变。我检查了可收容信息,但我认为没有发生任何事情。
    【解决方案2】:

    试试这个:

    您的帖子数据格式不正确:

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

    【讨论】:

      【解决方案3】:

      postsvotes 表中都需要有 id 字段。

      在你的posts 表中你有post_id 字段,但你的情况没有必要。

      所以,从posts 表中删除post_id 并检查结果。

      【讨论】:

      • 我认为这与它有一些有关,但我感觉我在模型中的关系已经关闭。当我在帖子和投票表中将 post_id 更改为仅 id 时,cake 现在会查询 votes.post_id。当我在两个表中将它们设置为 post_id 时,蛋糕查询 votes.id
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 2021-06-15
      • 2017-07-02
      • 2013-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多