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

  • Home
  • SEARCH
  • 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 7888853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:56:35+00:00 2026-06-03T05:56:35+00:00

I am doing a one-to-many relationship in my codes right now. Below is my

  • 0

I am doing a one-to-many relationship in my codes right now. Below is my db:

TABLE: categories
FIELDS: category_id (primary)
        categoryName


TABLE: products
FIELDS: id (primary)
        productName
        category_id

I want products.category_id communicate with categories.category_id so that it will select products with its corresponding category_id. But what actually happens in my code is it select category_id via the id in my products which is my primary key and it goes like this:

SELECT `Category`.`category_id`, `Category`.`categoryName`, `Category`.`description`, `Category`.`picture` FROM `categories` AS `Category` WHERE `Category`.`category_id` IN (3, 5, 6)

the values: 3,5,6 are the primary keys of the three products in my products table. But it should be this way

SELECT `Category`.`category_id`, `Category`.`categoryName`, `Category`.`description`, `Category`.`picture` FROM `categories` AS `Category` WHERE `Category`.`category_id` IN (1, 2, 3)

where the values: 1,2,3 are the category_id in my products table corresponding with the category_id in my categories table

Any help would be highly appreciated.

  • 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-03T05:56:37+00:00Added an answer on June 3, 2026 at 5:56 am

    Make sure your model associations are set up correctly:

    //Category model
    public $hasMany = array('Product');
    
    //Product model
    public $belongsTo = array('Category');
    

    And change Category.category_id to just Category.id.

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

Sidebar

Related Questions

I have this basic one-to-many relationship in Core Data: Brand has N Products Product
I am doing an eager load on a one to many relationship in hibernate.
I have a many-to-many relationship between a list of codes and a main table
I'm doing my program in many steps. One of them is to use gevent
I have a one-to-many relationship between Part and Params (a Part has many Params).
I have a EF Code First One-To-Many relationship. It's essentially a Parent/Child relations as
I have a one to many relationship between Games and Players. There is a
I have a Users table and a Networks table with a many-to-many relationship between
Okay I have a one-to-many relationship in core data. A Meal can consists of
New to rails and trying to get a one to many relationship up and

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.