Tuesday, 10 June 2014

Model in Cakephp

Models are the classes that sit as the business layer in your application. This means that they should be responsible for managing almost everything that happens regarding your data, its validity, interactions and evolution of the information workflow in your domain of work.
 Define Model

App::uses(’AppModel’, ’Model’);
class Member extends AppModel
{
          var $name='Member';
}
Association in Model
1# HasOne : One to one relation with two tables

public $hasOne = array(
’Profile’ => array(
’className’ => ’Profile’,
foreignKey’ => ’user_id’,
’dependent’ => true)
);

Notes:  Here foreignKey founds in the other model.

2# HasMany: One to many

var $hasMany=array(
          'BusinessHours'=>array(
 'className'=>'BusinessHours',
                     'dependent'=>true,
                     'foreignKey'=>'member_id'
                             )
      );

Note: Here foreignkey founds in other model.

3#  Belongs to

var $belongsTo = array(
          'CountryLocation'=>array(
'className'=>'CountryLocation',
          'foreignKey'=>'country')
          );

Notes: Here foreignKey founds in current table.

4# HABTM

public $hasAndBelongsToMany = array(

'CountryLocation'=>array(
'className'=>'CountryLocation',
          'foreignKey'=>'country')
          );


Notes: Here foreignKey founds in current table.



Limit results hasMany
Limits the maximum number of results for the child hasMany model
$this->ModelName->hasMany['ChildModelName']['limit'] = 10;
 



0 comments:

Post a Comment