Created
October 15, 2012 15:53
-
-
Save brandonwestcott/3893236 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Option 1. Always include child relations. If those relations have relations, it will continue to chain. | |
~~~ php | |
class Providers extends AppModel { | |
public $hasMany = array( | |
'ProviderPractices' => array( | |
'to' => 'ProviderPractices', | |
'fieldName' => 'provider_practice_set', | |
'embedded' => 'provider_practice_set', | |
'with' => array( | |
'PracticeInsurancePlans', | |
'Phones', | |
'Facility' | |
), | |
) | |
); | |
} | |
$provider = Providers::first(array('with'=>array('ProviderPractices')); | |
~~~ | |
so you would have provider->provider_practice_set->first()->practice_ins_plan_set->first() | |
Alternately, if you don't always want the relation joined (mainly when you are going to another DB) you can specify it in two ways | |
~~~ php | |
class Providers extends AppModel { | |
public $hasMany = array( | |
'ProviderPractices' => array( | |
'to' => 'ProviderPractices', | |
'fieldName' => 'provider_practice_set', | |
'embedded' => 'provider_practice_set', | |
) | |
); | |
} | |
~~~ | |
Given this model you can: | |
~~~ php | |
$provider = Providers::first(array( | |
'with'=> array( | |
'ProviderPractices' => array( | |
'with' => array('PracticeInsurancePlans') | |
) | |
) | |
); | |
~~~ | |
Again you would have, provider->provider_practice_set->first()->practice_ins_plan_set->first(), but you would have the Phones or Facility models. | |
Same principle alternate syntax that persists through multiple finds. For instance maybe in the init of a controller you always want something joined, but not necessarily always for the model | |
~~~ php | |
Providers::updateRelation('hasMany', array( | |
'ProviderPractices' => array( | |
'with' => array( | |
'PracticeInsurancePlans' | |
) | |
), | |
); | |
$provider = Providers::first(array('with'=>array('ProviderPractices')); | |
~~~ | |
Again you would have, provider->provider_practice_set->first()->practice_ins_plan_set->first(), but you would have the Phones or Facility models. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment