@@ -29,11 +29,8 @@ belongsTo, and hasAndBelongsToMany (HABTM).
2929| Relationship | Association Type | Example |
3030| ----| ----| ----|
3131| one to one | hasOne | A user has one profile. |
32- | ------------- | --------------------- | --------------------------------------- |
3332| one to many | hasMany | A user can have multiple recipes. |
34- | ------------- | --------------------- | --------------------------------------- |
3533| many to one | belongsTo | Many recipes belong to a user. |
36- | ------------- | --------------------- | --------------------------------------- |
3734| many to many | hasAndBelongsToMany | Recipes have, and belong to, many ingredients. |
3835
3936To further clarify which way around the associations are defined in the models:
@@ -161,13 +158,11 @@ table will contain a field called user_id. The basic pattern is:
161158
162159** hasOne:** the * other* model contains the foreign key.
163160
164- | Relation | Schema |
165- | ----------------------| --------------------|
166- | Apple hasOne Banana | bananas.apple_id |
167- | -------------------- | ------------------ |
168- | User hasOne Profile | profiles.user_id |
169- | -------------------- | ------------------ |
170- | Doctor hasOne Mentor | mentors.doctor_id |
161+ | Relation | Schema |
162+ | ----------------------| -------------------|
163+ | Apple hasOne Banana | bananas.apple_id |
164+ | User hasOne Profile | profiles.user_id |
165+ | Doctor hasOne Mentor | mentors.doctor_id |
171166
172167> [ !NOTE]
173168> It is not mandatory to follow CakePHP conventions. You can easily override
@@ -269,13 +264,11 @@ follow this convention:
269264
270265** belongsTo:** the * current* model contains the foreign key.
271266
272- | Relation | Schema |
273- | -------------------------| --------------------|
274- | Banana belongsTo Apple | bananas.apple_id |
275- | ----------------------- | ------------------ |
276- | Profile belongsTo User | profiles.user_id |
277- | ----------------------- | ------------------ |
278- | Mentor belongsTo Doctor | mentors.doctor_id |
267+ | Relation | Schema |
268+ | -------------------------| -------------------|
269+ | Banana belongsTo Apple | bananas.apple_id |
270+ | Profile belongsTo User | profiles.user_id |
271+ | Mentor belongsTo Doctor | mentors.doctor_id |
279272
280273> [ !TIP]
281274> If a model(table) contains a foreign key, it belongsTo the other
@@ -377,12 +370,10 @@ table and name it `image_comment_count`.
377370
378371Here are some more examples:
379372
380- | Model | Associated Model | Example |
381- | ----| ----| ----|
382- | User | Image | users.image_count |
383- | ---------- | ----------------------- | ----------------------------------------- |
384- | Image | ImageComment | images.image_comment_count |
385- | ---------- | ----------------------- | ----------------------------------------- |
373+ | Model | Associated Model | Example |
374+ | -----------| ------------------| ---------------------------------------|
375+ | User | Image | users.image_count |
376+ | Image | ImageComment | images.image_comment_count |
386377| BlogEntry | BlogEntryComment | blog_entries.blog_entry_comment_count |
387378
388379Once you have added the counter field, you are good to go. Activate
@@ -434,13 +425,11 @@ relation. It is also possible to define a `counterScope` for each `counterCache`
434425Assuming you have a ` User ` model and a ` Message ` model, and you want to be able
435426to count the amount of read and unread messages for each user.
436427
437- | Model | Field | Description |
438- | ----| ----| ----|
439- | User | users.messages_read | Count read ` Message ` |
440- | --------- | ---------------------- | ------------------------------------------- |
441- | User | users.messages_unread | Count unread ` Message ` |
442- | --------- | ---------------------- | ------------------------------------------- |
443- | Message | messages.is_read | Determines if a ` Message ` is read or not. |
428+ | Model | Field | Description |
429+ | ---------| -----------------------| -------------------------------------------|
430+ | User | users.messages_read | Count read ` Message ` |
431+ | User | users.messages_unread | Count unread ` Message ` |
432+ | Message | messages.is_read | Determines if a ` Message ` is read or not. |
444433
445434With this setup, your ` belongsTo ` would look like this:
446435
@@ -468,13 +457,11 @@ this convention:
468457
469458** hasMany:** the * other* model contains the foreign key.
470459
471- | Relation | Schema |
472- | -------------------------| --------------------|
473- | User hasMany Comment | Comment.user_id |
474- | ----------------------- | ------------------ |
475- | Cake hasMany Virtue | Virtue.cake_id |
476- | ----------------------- | ------------------ |
477- | Product hasMany Option | Option.product_id |
460+ | Relation | Schema |
461+ | ------------------------| -------------------|
462+ | User hasMany Comment | Comment.user_id |
463+ | Cake hasMany Virtue | Virtue.cake_id |
464+ | Product hasMany Option | Option.product_id |
478465
479466We can define the hasMany association in our User model at
480467/app/Model/User.php using the string syntax as follows:
@@ -619,9 +606,7 @@ names.
619606| Relationship | HABTM Table Fields |
620607| ----| ----|
621608| Recipe HABTM Ingredient | ** ingredients_recipes** .id, ** ingredients_recipes** .ingredient_id, ** ingredients_recipes** .recipe_id |
622- | ------------------------- | ---------------------------------------------------------------- |
623609| Cake HABTM Fan | ** cakes_fans** .id, ** cakes_fans** .cake_id, ** cakes_fans** .fan_id |
624- | ------------------------- | ---------------------------------------------------------------- |
625610| Foo HABTM Bar | ** bars_foos** .id, ** bars_foos** .foo_id, ** bars_foos** .bar_id |
626611
627612> [ !NOTE]
0 commit comments