Cakephp Classroom image

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

This shows how to use models that are already linked. To understand how associations are defined take a
look at the Associations sectionMore on models
Associations: Linking Models Together
One of the most powerful features of CakePHP is the ability to link relational mapping provided by the
model. In CakePHP, the links between models are handled through associations.
Defining relations between different objects in your application should be a natural process. For example:
in a recipe database, a recipe may have many reviews, reviews have a single author, and authors may have
many recipes. Defining the way these relations work allows you to access your data in an intuitive and
powerful way.
The purpose of this section is to show you how to plan for, define, and utilize associations between models
in CakePHP.
While data can come from a variety of sources, the most common form of storage in web applications is a
relational database. Most of what this section covers will be in that context.
For information on associations with Plugin models, see Plugin Models.
Relationship Types
The four association types in CakePHP are: hasOne, hasMany, belongsTo, and hasAndBelongsToMany
(HABTM).
Relationship Association Type Example
one to one hasOne A user has one profile.
one to many hasMany A user can have multiple recipes.
many to one belongsTo Many recipes belong to a user.
many to many hasAndBelongsToMany Recipes have, and belong to many ingredients.
Associations are defined by creating a class variable named after the association you are defining. The class
variable can sometimes be as simple as a string, but can be as complete as a multidimensional array used to
define association specifics.
class User extends AppModel {
public $hasOne = ’Profile’;
public $hasMany = array(
’Recipe’ => array(
’className’ => ’Recipe’,
’conditions’ => array(’Recipe.approved’ => ’1’),
’order’ => ’Recipe.created DESC’
)
);
}
In the above example, the first instance of the word ‘Recipe’ is what is termed an ‘Alias’. This is an identifier
for the relationship and can be anything you choose. Usually, you will choose the same name as the class
that it references. However, aliases for each model must be unique app wide. For example it is appropriate
to have:class User extends AppModel {
public $hasMany = array(
’MyRecipe’ => array(
’className’ => ’Recipe’,
)
);
public $hasAndBelongsToMany => array(
’MemberOf’ => array(
’className’ => ’Group’,
)
);
}
class Group extends AppModel {
public $hasMany = array(
’MyRecipe’ => array(
’className’ => ’Recipe’,
)
);
public $hasAndBelongsToMany => array(
’Member’ => array(
’className’ => ’User’,
)
);
}
but the following will not work well in all circumstances:
class User extends AppModel {
public $hasMany = array(
’MyRecipe’ => array(
’className’ => ’Recipe’,
)
);
public $hasAndBelongsToMany => array(
’Member’ => array(
’className’ => ’Group’,
)
);
}
class Group extends AppModel {
public $hasMany = array(
’MyRecipe’ => array(
’className’ => ’Recipe’,
)
);
public $hasAndBelongsToMany => array(
’Member’ => array(
’className’ => ’User’,
)
);
}

 
 
 

osdyui

Skills    Cakephp

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

  Students (0)