Skip to content

Instantly share code, notes, and snippets.

@brandonwestcott
Created October 15, 2012 15:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brandonwestcott/3893236 to your computer and use it in GitHub Desktop.
Save brandonwestcott/3893236 to your computer and use it in GitHub Desktop.
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