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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:16:45+00:00 2026-06-08T02:16:45+00:00

I have a site developed in CakePHP 2.0. I have a database with many

  • 0

I have a site developed in CakePHP 2.0.
I have a database with many tables and I want to relation two tables. I have done this many times, but with two tables I can’t do this and I don’t know why.
This is my two tables:

ingredient_aliases:

id             INT(10) UNSIGNED AUTO_INCREMENT 
ingredient_id  INT(10)          
user_id        INT(10)          
alias          VARCHAR(100) latin1_swedish_ci 

acitivity_ingredients

id             INT(10) UNSIGNED     
activity_id    INT(11)      
ingredient_id  INT(10)          
created        DATETIME

ingredient_id is my foreign key and this is my model, I want to to make the ingredient_id my foreign key.

class ActivityIngredients extends AppModel{
        public $name = 'ActivityIngredients'; 
        public $useTable = 'activity_ingredients';

        public $belongsTo = array(
            'IngredientAlias' => array(
                'className'     => 'IngredientAlias',
                'conditions'    => '',
                'order'         => '',
                'foreignKey'    => 'ingredient_id'
            )
        );


    }

class IngredientAlias extends AppModel {
    public $name = 'IngredientAlias';
    public $useTable = 'ingredient_aliases';
    public $belongsTo = array(
        'Ingredient' => array(
            'className'    => 'Ingredient',
            'foreignKey'   => 'ingredient_id'
        ),
         'User' => array(
            'className'    => 'User',
            'foreignKey'   => 'user_id'
        )
    );

    public $hasMany = array (
        'ActivityIngredients' => array (
            'className'     => 'ActivityIngredients',
            'dependent' => true,
            'foreignKey'   => false,
            'associatedKey'   => 'ingredient_id'            
        )
    );

When I make the var_dump of my variables in IngredientAlias there is nothing, is empty like it doesn’t take the foreign key.. why?
The problem is in the relations?
I have try to write

'foreignKey'   => 'ingredient_id'

But nothing.. same

The query is a simple query with 3 of recursive, a select all i think is not there the problem but in the relation with the table..

  • 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-08T02:16:47+00:00Added an answer on June 8, 2026 at 2:16 am

    That’s not how hasMany/belongsTo relationships work.

    Your current code describes a relationship where:

    • ActiveIngredient belongs to IngredientAlias
    • IngredientAlias has many ActiveIngredient

    This is a one-to-many relationship from IngredientAlias to ActiveIngredient.

    But your database schema describes the following table relationships:

    • ActiveIngredient belongs to Ingredient
    • Ingredient has many ActiveIngredient
    • IngredientAlias belongs to Ingredient
    • Ingredient has many IngredientAlias

    In other words, a one-to-many relationship from Ingredient to ActiveIngredient, and another one-to-many relationship from Ingredient to IngredientAlias.

    So your model relationships should be exactly what the database schema describes. (Also, your foreign keys should reference a candidate key. MySQL allows you to create foreign key constraints that aren’t referencing a candidate key, but I don’t think most other databases allow this; thus CakePHP probably also doesn’t allow it.)

    Lastly, recursive levels in CakePHP range from -1 to 2 (inclusive). There is no recursive level of 3. If you simply set recursive to 2, then a findAll on ActiveIngredient will return its parent Ingredient and that parent Ingredient‘s child IngredientAliases.


    Edit:
    recursive uses multiple queries under the hood. However, most of the time, it’s going to fetch a lot of unnecessary data from models you don’t need. The best thing to do is to either use unbindModel() to remove the associations that aren’t need, or, where possible, set recursive to -1 and use joins to fetch associated data.

    Another variation of the first option is to use the Containable behavior. This lets you define the exact relations and fields that you want returned. This is usually the easiest way to optimize finds that require recursive queries.

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

Sidebar

Related Questions

I have a site developed in cakephp 2.0, I want to make a relation
I have developed a site on linux server(my testing host server). But When I
I have a site developed with cakePHP. I have it under source control with
I have a site developed with django at www.example.com I want that django insert/serve
I have a site developed in MOSS2007. where can I see the list of
I have a web site already developed with tons of updatePanels, buttons, and integrated
I have developed a site with ASP.NET MVC 1, and now I'm trying to
I have developed a site, which has some static pages. Like explore, home, feedback.
I have a asp.net web site that was developed on the .Net Framework v2
I have developed a web site that requires user registration and authentication for some

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.