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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:27:59+00:00 2026-06-01T03:27:59+00:00

I have 2 models which are connected via a HABTM association, in the same

  • 0

I have 2 models which are connected via a HABTM association, in the same way that Recipes and Ingredients are associated in the cookbook.

I need to do the equivalent of finding all recipes which use a particular ingredient – how do I do that?

So basically, I’ll have ingredient_id, and I want to do something like:

$this->Recipe->find('all', array('conditions' => 'recipe uses this ingredient'));

I’d also like to retrieve a list of all ingredients which are used in at least one recipe.

  • 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-01T03:28:01+00:00Added an answer on June 1, 2026 at 3:28 am

    When you use HABTM (hasAndBelongsToMany) relationships in CakePHP, it creates a model by combining the names of both models in alphabetical order (reference). You can use that model to perform your query.

    Example:

    <?php
    
    $recipes = $this->Recipe->IngredientsRecipe->find('all', array(
        'conditions' => array(
            'IngredientsRecipe.ingredient_id' => 1 // This could also be an array of ingredients
        ),
        'group' => array('Recipe.id')
    ));
    

    In the example above, we’re using the IngredientsRecipe model which has 2 relationships, both belongsTo with Ingredient and Recipe. Just in case a Recipe has multiple ingredients that match, we’re also grouping by the recipe ID.

    PS: You don’t need to define the relationship to IngredientsRecipe as long as you’ve defined the hasAndBelongsToMany relationship between Recipe and Ingredients. CakePHP will do the rest automatically.

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

Sidebar

Related Questions

I have three models connected via an has_many_through association. class Board < ActiveRecord::Base has_many
What I mean is if I have two models, connected by a has_and_belongs_to_many association,
Here's what I need to do. I have a Tournament model, which is connected
I have various models of which I would like to keep track and collect
I have a class which models all categories and they can be ordered hierarchically.
I have a Class which models a User and another which models his country.
I have two models: Person and Address which I'd like to create in a
I have an object which contains models for my ASP.NET MVC web app. The
I have a process which iterates through a bunch of ActiveRecord models, does some
I have two models, Worker and Project, and they are connected with has_many through

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.