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 6916427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:36:06+00:00 2026-05-27T09:36:06+00:00

I have this model and controller and it returns fine if it is a

  • 0

I have this model and controller and it returns fine if it is a hasOne relationship but now DishCategory hasMany dishes. When I change the code below to hasMany it gives me an error saying Dish.id is not a known column while if it is a hasOne it works fine. How can I make it so that it returns all the Dish.id’s that have id=1? (still using the join).

  class DishCategory extends AppModel{

               public $hasOne = array(
                'Dish' => array(
                    'className' => 'Dish',
                    'foreignKey' => 'dish_category_id'
                )
            );

     }

class DishCategoriesController extends AppController {

 function get_categories($id)
      { 
        // find category with a dish of $id
        $this->set('dishes', $this->DishCategory->find('all', array(
          'conditions' => array(
            'Dish.id' => $id
          )
        )));

        // set master layout
        $this->layout = 'master_layout';
      }     
  }
  • 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-05-27T09:36:07+00:00Added an answer on May 27, 2026 at 9:36 am

    hasOne relations can be joined into the primary SQL query. hasMany relations cannot and need to be queried in a separate query. The conditions you specify for the query only apply to the primary query, all separate relational queries are build just based on the ids retrieved in the primary query. Set debug to 2 and have a good look at the query log to see what I mean.

    To find the category of a dish, fetch the dish and look at its related category record:

    $dish = $this->Dish->find('first', array('conditions' => array('Dish.id' => $id)));
    echo $dish['DishCategory']['name'];
    

    Since I take it that a dish belongsTo one category, your query “find all categories which have a dish with id x” makes little sense; there should only be one anyway.

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

Sidebar

Related Questions

I have this code in my model when the controller requests to get all
I have the following Model and Controller files, and when i visit this url,
I have this model in django: class JournalsGeneral(models.Model): jid = models.AutoField(primary_key=True) code = models.CharField(Code,
I'm using FluentNHibernate but NHibernate XML will do. Say I have this model public
I have a problem with the my Controller code. GET works fine (both empty
Let's say I have this model named Product with a field named brand .
I have this Task model: class Task < ActiveRecord::Base acts_as_tree :order => 'sort_order' end
I have this dictionary in my apps model file: TYPE_DICT = ( (1, Shopping
I have a model that holds user address. This model has to have first_name
Okay so I have this mode: class Posts(db.Model): rand1 = db.FloatProperty() #other models here

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.