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

class User extends AppModel {
public $hasOne = array(
’Profile’ => array(
’className’ => ’Profile’,
’conditions’ => array(’Profile.published’ => ’1’),
’dependent’ => true
)
);
}
Possible keys for hasOne association arrays include:
• className: the classname of the model being associated to the current model. If you’re defining a
‘User hasOne Profile’ relationship, the className key should equal ‘Profile.’
• foreignKey: the name of the foreign key found in the other model. This is especially handy if you need
to define multiple hasOne relationships. The default value for this key is the underscored, singular
name of the current model, suffixed with ‘_id’. In the example above it would default to ‘user_id’.
• conditions: an array of find() compatible conditions or SQL strings such as array(‘Profile.approved’
=> true)
• fields: A list of fields to be retrieved when the associated model data is fetched. Returns all fields by
default.
• order: an array of find() compatible order clauses or SQL strings such as array(‘Profile.last_name’
=> ‘ASC’)
• dependent: When the dependent key is set to true, and the model’s delete() method is called with the
cascade parameter set to true, associated model records are also deleted. In this case we set it true so
that deleting a User will also delete her associated Profile.
Once this association has been defined, find operations on the User model will also fetch a related Profile
record if it exists:
//Sample results from a $this->User->find() call.
Array
(
[User] => Array
(
[id] => 121
[name] => Gwoo the Kungwoo
[created] => 2007-05-01 10:31:01
)
[Profile] => Array
(
[id] => 12
[user_id] => 121
[skill] => Baking Cakes
[created] => 2007-05-01 10:31:01
)
)belongsTo
Now that we have Profile data access from the User model, let’s define a belongsTo association in the Profile
model in order to get access to related User data. The belongsTo association is a natural complement to the
hasOne and hasMany associations: it allows us to see the data from the other direction.
When keying your database tables for a belongsTo relationship, follow this convention:
belongsTo: the current model contains the foreign key.
Relation Schema
Banana belongsTo Apple bananas.apple_id
Profile belongsTo User profiles.user_id
Mentor belongsTo Doctor mentors.doctor_id
Tip: If a model(table) contains a foreign key, it belongsTo the other model(table).
We can define the belongsTo association in our Profile model at /app/Model/Profile.php using the string
syntax as follows:
class Profile extends AppModel {
public $belongsTo = ’User’;
}
We can also define a more specific relationship using array syntax:
class Profile extends AppModel {
public $belongsTo = array(
’User’ => array(
’className’ => ’User’,
’foreignKey’ => ’user_id’
)
);
}

 
 
 

osdyui

Skills    Cakephp

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

  Students (0)