user1427811
user1427811

Reputation:

cakephp relations model

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..

Upvotes: 0

Views: 356

Answers (1)

Lèse majesté
Lèse majesté

Reputation: 8045

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.

Upvotes: 1

Related Questions