Cakephp Classroom image

Anil  Bist / Professional / Web Technology

 
To post your Question Join Classroom
 
Lesson Topics's No:-  First|4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12|Last
Lessons:-Models

hasAndBelongsToMany (HABTM)
Alright. At this point, you can already call yourself a CakePHP model associations professional. You’re
already well versed in the three associations that take up the bulk of object relations.
Let’s tackle the final relationship type: hasAndBelongsToMany, or HABTM. This association is used when
you have two models that need to be joined up, repeatedly, many times, in many different ways.
The main difference between hasMany and HABTM is that a link between models in HABTM is not exclusive.
For example, we’re about to join up our Recipe model with an Ingredient model using HABTM. Using
tomatoes as an Ingredient for my grandma’s spaghetti recipe doesn’t “use up” the ingredient. I can also use
it for a salad Recipe.with an underscore ( _ ). The contents of the table should be two fields, each foreign keys (which should
be integers) pointing to both of the primary keys of the involved models. To avoid any issues - don’t define
a combined primary key for these two fields, if your application requires it you can define a unique index.
If you plan to add any extra information to this table, or use a ‘with’ model, you should add an additional
primary key field (by convention ‘id’).
HABTM requires a separate join table that includes both model names.
Relationship HABTM Table Fields
Recipe HABTM
Ingredient
ingredients_recipes.id, ingredients_recipes.ingredient_id,
ingredients_recipes.recipe_id
Cake HABTM Fan cakes_fans.id, cakes_fans.cake_id, cakes_fans.fan_id
Foo HABTM Bar bars_foos.id, bars_foos.foo_id, bars_foos.bar_id
Note: Table names are by convention in alphabetical order. It is possible to define a custom table name in
association definition
Make sure primary keys in tables cakes and recipes have “id” fields as assumed by convention. If they’re
different than assumed, it has to be changed in model’s primaryKey
Once this new table has been created, we can define the HABTM association in the model files. We’re gonna
skip straight to the array syntax this time:
class Recipe extends AppModel {
public $hasAndBelongsToMany = array(
’Ingredient’ =>
array(
’className’ => ’Ingredient’,
’joinTable’ => ’ingredients_recipes’,
’foreignKey’ => ’recipe_id’,
’associationForeignKey’ => ’ingredient_id’,
’unique’ => true,
’conditions’ => ’’,
’fields’ => ’’,
’order’ => ’’,
’limit’ => ’’,
’offset’ => ’’,
’finderQuery’ => ’’,
’deleteQuery’ => ’’,
’insertQuery’ => ’’
)
);
}
Links between hasMany associated objects are exclusive. If my User hasMany Comments, a comment is
only linked to a specific user. It’s no longer up for grabs.
Moving on. We’ll need to set up an extra table in the database to handle HABTM associations. This new
join table’s name needs to include the names of both models involved, in alphabetical order, and separated

 
 
 
image
Anil  Bist

Skills    Cakephp

Qualifications :- High School - SLV, College/University - Graphic Era Deemed Univ University,
Location :-Dehradun,Dehradun,Uttarakhand,India
Description:-

I started my Professional Journey in 2006 with one of the Web Development Company in Bangalore and my 1st framework was "Ruby on Rail" as Web development and delivered around 5+ Projects using this platform. Then came another dimension as JEE/Sturst framework, Gradually I realized that I want to build something on my own and give my passion and energy on creating something different a
Explore

 

  Students (0)