Cakephp Classroom image

deepa
 
To post your Question Join Classroom
 
Lesson Topics's No:-  First|1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9|Last
Lessons:-Models

because here we have the alias ‘Member’ referring to both the User (in Group) and the Group (in User)
model in the HABTM associations. Choosing non-unique names for model aliases across models can cause
unexpected behavior.
Cake will automatically create links between associated model objects. So for example in your User model
you can access the Recipe model as:
$this->Recipe->someFunction();
Similarly in your controller you can access an associated model simply by following your model associations:
$this->User->Recipe->someFunction();
Note: Remember that associations are defined ‘one way’. If you define User hasMany Recipe that has no
effect on the Recipe Model. You need to define Recipe belongsTo User to be able to access the User model
from your Recipe model
hasOne
Let’s set up a User model with a hasOne relationship to a Profile model.
First, your database tables need to be keyed correctly. For a hasOne relationship to work, one table has to
contain a foreign key that points to a record in the other. In this case the profiles table will contain a field
called user_id. The basic pattern is:
hasOne: the other model contains the foreign key.
Relation Schema
Apple hasOne Banana bananas.apple_id
User hasOne Profile profiles.user_id
Doctor hasOne Mentor mentors.doctor_id
Note: It is not mandatory to follow CakePHP conventions, you can easily override the use of any foreignKey
in your associations definitions. Nevertheless sticking to conventions will make your code less
repetitive, easier to read and to maintain.
The User model file will be saved in /app/Model/User.php. To define the ‘User hasOne Profile’ association,
add the $hasOne property to the model class. Remember to have a Profile model in /app/Model/Profile.php,
or the association won’t work:
class User extends AppModel {
public $hasOne = ’Profile’;
}
There are two ways to describe this relationship in your model files. The simplest method is to set the
$hasOne attribute to a string containing the classname of the associated model, as we’ve done above.
If you need more control, you can define your associations using array syntax. For example, you might want
to limit the association to include only certain records.

 
 
 

deepa

Skills    Cakephp

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

  Students (0)