cakephp正在查询额外的列,我不知道为什么 [英] cakephp is querying extra column and I can't figure out why

查看:158
本文介绍了cakephp正在查询额外的列,我不知道为什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些麻烦,让cakephp正确查询。蛋糕试图在我的一个表中找到一个不存在的列,我不知道为什么。我有两个表涉及这个;票和职位。



以下是表格



 表票
post_id | user_id | vote

表帖子
post_id | tite |身体|创建的修改| 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时,得到以下错误



错误:SQLSTATE [42S22]:未找到列:1054'字段列表'中的未知列'Post.id'



以下是正在执行的查询

  SQL查询: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 。我一直在搜索我的函数和模型,我不知道发生了什么。



这里是函数my postscontroller

  public function vote($ id = null){
$ this-> layout ='vote_layout';
$ this-> Post-> Votes-> recursive = 0;

$ this-> Post-> id = $ id;
$ user = $ this-> Auth-> User();
$ userid = $ user ['id'];
$ conditions = array('votes.id'=> $ id,'vot.user_id'=> $ userid);

$ data = $ this-> Post-> Votes-> find('all',array('conditions'=> $ conditions));

if(isset($ data)&&!empty($ data)){
echo'< p>用户已经投票!< / p& ;
} else {

if($ this-> Post-> Votes-> validates()){

if($ this-> Post-> Votes-> save($ this-> request-> data)){
$ this-> Session-> setFlash(__
$ this-> redirect(array('action'=>'vote'));
} else {
$ this-> Session-> setFlash(__('无法保存投票,请重试。
}
}
}
}

在我的帖子模型中

  public $ hasMany = array(
'Votes'=& b'className'=>'Vote',

);

所以,我的问题是,我做的是导致蛋糕查询post.id?它甚至不是我的表中的一列,我不能弄清楚查询来自哪里。



编辑



当我在我的帖子模型中删除hasMany关系该查询错误消失,但是我有以下错误引用表中的每一个post字段。

 未定义索引:id 

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

解决方案

您需要在<$ c $中有 id c>



> posts 表你有 post_id 字段,但是没有必要你的case。



因此,从 posts 表中删除 post_id 检查结果。


I'm having some trouble getting cakephp to properly query. Cake is trying to find a non-existant column in one of my tables and I can't figure out why. I have two tables involved with this ; votes and posts. Users can vote on a post.

Here are the tables

Table votes
post_id |  user_id | vote

table posts
post_id | tite | body | created | modified  | user_id | vote_total

When a user votes on a post their vote gets placed in the votes table, and the vote_total is increased/decreased by their vote.

In the index for posts, users can vote on a post through these two links. Right now I have both sent to the same function just to get one working, then I'll finish the other

<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>

When I view index.ctp which displays posts, I get the following error

Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Post.id' in 'field list'

Here is the query that it is preforming

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

So, my problem is that I don't know where the Post.id is being queried from. I've been searching through my function and the model and I can't figure out what's happening.

Here's the function my 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.'));
        }
    }   
}
}

This is in my post model

 public $hasMany = array(
    'Votes' => array(
        'className'  => 'Vote',
        )
    );

So, my question is what am I doing that is causing cake to query post.id? It's not even a column in my table and I can't figure out where the query is coming from.

edit

When I remove the hasMany relationship for votes in my Posts model that querying error goes away, but then I have the following error which references every single post field in the table.

Undefined index: id 

Is this not a hasmany relationship? Each post has many votes, but each user only has 1 vote per post.

解决方案

You need to have id field in both posts and votes table.

And in you posts table you have post_id field, but it is not necessary for you case.

So, remove the post_id from posts table and check the result.

这篇关于cakephp正在查询额外的列,我不知道为什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆