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..
That’s not how hasMany/belongsTo relationships work.
Your current code describes a relationship where:
ActiveIngredientbelongs toIngredientAliasIngredientAliashas manyActiveIngredientThis is a one-to-many relationship from
IngredientAliastoActiveIngredient.But your database schema describes the following table relationships:
ActiveIngredientbelongs toIngredientIngredienthas manyActiveIngredientIngredientAliasbelongs toIngredientIngredienthas manyIngredientAliasIn other words, a one-to-many relationship from
IngredienttoActiveIngredient, and another one-to-many relationship fromIngredienttoIngredientAlias.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,
recursivelevels in CakePHP range from-1to2(inclusive). There is norecursivelevel of3. If you simply setrecursiveto2, then afindAllonActiveIngredientwill return its parentIngredientand that parentIngredient‘s childIngredientAliases.Edit:
recursiveuses 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 useunbindModel()to remove the associations that aren’t need, or, where possible, setrecursiveto-1and usejoinsto 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.