Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

1.

2
CakePHP Model Associations Cheat Sheet
$hasMany – one to many $hasAndBelongsToMany – many to many
array(  array( 
  'UniqueName' => array(    'UniqueName' => array( 
    'className' => 'thatClassName',      'className' => 'thatClassName',
    'foreignKey' => 'this_fk_id',      'joinTable' => 'join(s)_table(s)',
    'conditions' =>   array(     'with' => 'JoiningClassName',
       'That.field' => 'somevalue'     'foreignKey' => 'this_id', 
    ),      'associationForeignKey'=> 'that_id',
    'order' => 'That.field DESC',      'unique'=> true,
    'limit' => '5',     'conditions' =>   array(
    'fields' => 'That.fields',        'That.field' => 'somevalue'
    'offset' => 0,     ), 
    'dependent'=> true,     'order' => 'That.field DESC', 
    'exclusive' => false,     'limit' => '5',
    'finderQuery' => 'FULL JOIN QUERY'     'fields' => 'That.fields',
  )      'offset' => 0,
);      'finderQuery' => 'FULL JOIN QUERY',
    'deleteQuery' => 'FULL DELETE QUERY',
    'insertQuery' => 'FULL INSERT QUERY'
  ) 
);

$hasOne – one to one Model Relationship Conventions


array(  Foreign keys in hasMany, belongsTo or hasOne relationships
  'UniqueName' => array(  are recognized by default as the (singular) name of the related
    'className' => 'thatClassName',  model followed by _id
    'foreignKey' => 'this_fk_id', 
    'conditions' =>   array(
Join tables, used in hasAndBelongsToMany (HABTM)
       'That.field' => 'somevalue'
    ),  relationships between models should be named after the model
    'fields' => 'That.fields', tables they will join in alphabetical order (apples_zebras rather
    'dependent' => true than zebras_apples).
  ) 
);  All tables with models require a PRIMARY KEY named 'id'.

$belongsTo – many to one


array( 
  'UniqueName' => array( 
    'className' => 'thatClassName', 
    'foreignKey' => 'that_fk_id', 
    'conditions' =>   array(
       'That.field' => 'somevalue'
    ), 
    'fields' => 'That.fields',
    'counterCache' => false
  ) 
); 

Bryan English
cakephp.org - pivotlabs.com – cheatsheetheap.com
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License

You might also like