Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8329839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:55:45+00:00 2026-06-09T01:55:45+00:00

I was having trouble with a voting system that I’m trying to make in

  • 0

I was having trouble with a voting system that I’m trying to make in cakePHP (found here cakephp is querying extra column and I can't figure out why) but I figured that out and now I’m having another issue.

I’m making a voting system, and the problem I’m having is that only one user can vote on a given post. For example, if user 1 votes on post 1, and then user 2 votes on post 1, user 2’s vote will overwrite user 1’s vote.

Here are my tables

Votes
id |   user_id   | vote

Posts
id | title | body | created | modified | user_id | vote_total

I’m having trouble setting up the associations

  1. Users can vote on many posts

  2. A post can have many votes, but only 1 per user

This is in my Users model

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

This is in my Posts model

 public $hasMany = array( //there can be multiple votes of the same id (references post table)
    'Votes' => array('foreignKey' => 'id')
    );

I don’t have a votes controller. It’s done through a vote function on PostsController.php

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T01:55:47+00:00Added an answer on June 9, 2026 at 1:55 am
    public $hasMany = array( //there can be multiple votes of the same id (references post table)
        'Votes' => array('foreignKey' => 'id')
        );
    

    is wrong. It should be:

     public $hasMany = array( //there can be multiple votes of the same id (references post table)
    'Votes' => array('foreignKey' => 'post_id')
    );
    

    And so you have to add post_id in the Vote model.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having trouble with pattern and replacement. How can I make the replacement echo a
I'm having some trouble approaching a +1/-1 voting system in PHP, it should vaguely
Having trouble and not sure how to proceed with debug. Found a question that
Having trouble with proper regex for RewriteCond RewriteCond %{REQUEST_URI} !^/foo/ Works as expected, that
I am working on a comment voting system. I have a page profile_new.php?id=194 That
Having trouble with this code, wondering if I'm trying to achieve something impossible. I'm
Having trouble trying to turn an XDocument into an list of objects, in particular
Having trouble finding a solution for my situation here. Sorry if this has been
Having trouble here and not quite sure how to do it. I've attempted jQuery
I'm having trouble registering a vote with registering a vote with django-voting: https://github.com/brosner/django-voting I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.