问题
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.
回答1:
Assuming you are following CakePHP conventions and have id
in all of your tables.
Since you are calling $this->Post->Vote->recursive = 0
, CakePHP will select all Post
related with your Vote
. Since you have a Post hasMany Vote
relationship, when it selects a vote via $this->Post->Vote->find()
, it will check the Vote.post_id
foreign key first.
Then, in order to select the Post
, it will have to run a query on Posts
table for something like Post.id = Vote.post_id
. That's possibly where the Post.id
is coming from.
What seems strange though is that its calling Post.post_id
(which naturally doesn't exist, as I assume you wouldn't associate a model with itself). The function that might possibly be calling that might still be the recursive
call, however it is attempting to fetch all posts. I'm unaware of how your application is coded, but tips for you to trace that further would be turning recursive
to -1
and see if that is called (don't worry about missing data). If it isn't running that Query after recursive=-1, then try using something more specific like the Containable
Component (See CakePHP's Book) to fetch your data - it might be helpful.
I'm not sure whether this would help, just popped out of my head when I saw Post.id
missing. Please let me know if it works (or not) :)
回答2:
Try this thing :
Your post data is not in correct format :
$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:
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.
来源:https://stackoverflow.com/questions/11805539/cakephp-is-querying-extra-column-and-i-cant-figure-out-why