Cakephp Classroom image

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

hasMany
Next step: defining a “User hasMany Comment” association. A hasMany association will allow us to fetch
a user’s comments when we fetch a User record.
When keying your database tables for a hasMany relationship, follow this convention:
hasMany: the other model contains the foreign key.
Relation Schema
User hasMany Comment Comment.user_id
Cake hasMany Virtue Virtue.cake_id
Product hasMany Option Option.product_idWe can define the hasMany association in our User model at /app/Model/User.php using the string syntax
as follows:
class User extends AppModel {
public $hasMany = ’Comment’;
}
We can also define a more specific relationship using array syntax:
class User extends AppModel {
public $hasMany = array(
’Comment’ => array(
’className’ => ’Comment’,
’foreignKey’ => ’user_id’,
’conditions’ => array(’Comment.status’ => ’1’),
’order’ => ’Comment.created DESC’,
’limit’ => ’5’,
’dependent’ => true
)
);
}
Possible keys for hasMany association arrays include:
• className: the classname of the model being associated to the current model. If you’re defining a
‘User hasMany Comment’ relationship, the className key should equal ‘Comment.’
• foreignKey: the name of the foreign key found in the other model. This is especially handy if you
need to define multiple hasMany relationships. The default value for this key is the underscored,
singular name of the actual model, suffixed with ‘_id’.
• conditions: an array of find() compatible conditions or SQL strings such as array(‘Comment.visible’
=> true)
• order: an array of find() compatible order clauses or SQL strings such as array(‘Profile.last_name’
=> ‘ASC’)
• limit: The maximum number of associated rows you want returned.
• offset: The number of associated rows to skip over (given the current conditions and order) before
fetching and associating.
• dependent: When dependent is set to true, recursive model deletion is possible. In this example,
Comment records will be deleted when their associated User record has been deleted.
• exclusive: When exclusive is set to true, recursive model deletion does the delete with a deleteAll()
call, instead of deleting each entity separately. This greatly improves performance, but may not be
ideal for all circumstances.
• finderQuery: A complete SQL query CakePHP can use to fetch associated model records. This
should be used in situations that require very custom results. If a query you’re building requires
a reference to the associated model ID, use the special {$__cakeID__$} marker in the
query. For example, if your Apple model hasMany Orange, the query should look something
like this: SELECT Orange.* from oranges as Orange WHERE Orange.apple_id
= {$__cakeID__$};

 
 
 

pankaj

Skills    Cakephp

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

  Students (0)