Cakephp Classroom image

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

[user_id] => 2346
)
[Ingredient] => Array
(
[0] => Array
(
[id] => 123
[name] => Chocolate
)
[1] => Array
(
[id] => 124
[name] => Sugar
)
[2] => Array
(
[id] => 125
[name] => Bombs
)
)
)
Remember to define a HABTM association in the Ingredient model if you’d like to fetch Recipe data when
using the Ingredient model.
Note: HABTM data is treated like a complete set, each time a new data association is added the complete
set of associated rows in database is dropped and created again so you will always need to pass the whole
data set for saving. For an alternative to using HABTM see hasMany through (The Join Model)
Tip: For more information on saving HABTM objects see Saving Related Model Data (HABTM)
hasMany through (The Join Model)
It is sometimes desirable to store additional data with a many to many association. Consider the following
Student hasAndBelongsToMany Course
Course hasAndBelongsToMany Student
In other words, a Student can take many Courses and a Course can be taken by many Students. This is a
simple many to many association demanding a table such as this:
id | student_id | course_id
Now what if we want to store the number of days that were attended by the student on the course and their
final grade? The table we’d want would be:
id | student_id | course_id | days_attended | gradeThe trouble is, hasAndBelongsToMany will not support this type of scenario because when hasAndBelongsToMany
associations are saved, the association is deleted first. You would lose the extra data in the
columns as it is not replaced in the new insert.
Changed in version 2.1. You can set unique setting to keepExisting circumvent losing
extra data during the save operation. See unique key in HABTM association arrays.
The way to implement our requirement is to use a join model, otherwise known as a hasMany through
association. That is, the association is a model itself. So, we can create a new model CourseMembership.
Take a look at the following models.:
// Student.php
class Student extends AppModel {
public $hasMany = array(
’CourseMembership’
);
}
// Course.php
class Course extends AppModel {
public $hasMany = array(
’CourseMembership’
);
}
// CourseMembership.php
class CourseMembership extends AppModel {
public $belongsTo = array(
’Student’, ’Course’
);
}
The CourseMembership join model uniquely identifies a given Student’s participation on a Course in addition
to extra meta-information.
Join models are pretty useful things to be able to use and Cake makes it easy to do so with its built-in
hasMany and belongsTo associations and saveAll feature.

 
 
 

osdyui

Skills    Cakephp

Qualifications :-
Location :-,,,
Description:-
Explore
 

  Students (0)