Created
November 19, 2012 12:21
-
-
Save tmdvs/4110368 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* Handle the dynamic retrieval of attributes and associations. | |
* | |
* @param string $key | |
* @return mixed | |
*/ | |
public function __get($key) | |
{ | |
// Lets check if the model has a function for this first | |
if(method_exists($this, $key)) | |
{ | |
return $this->$key(); | |
} | |
// First we will check to see if the requested key is an already loaded | |
// relationship and return it if it is. All relationships are stored | |
// in the special relationships array so they are not persisted. | |
if (array_key_exists($key, $this->relationships)) | |
{ | |
return $this->relationships[$key]; | |
} | |
// Next we'll check if the requested key is in the array of attributes | |
// for the model. These are simply regular properties that typically | |
// correspond to a single column on the database for the model. | |
else if (array_key_exists($key, $this->attributes)) | |
{ | |
return $this->{"get_{$key}"}(); | |
} | |
// If the item is not a loaded relationship, it may be a relationship | |
// that hasn't been loaded yet. If it is, we will lazy load it and | |
// set the value of the relationship in the relationship array. | |
else if (method_exists($this, $key)) | |
{ | |
return $this->relationships[$key] = $this->$key()->results(); | |
} | |
// Finally we will just assume the requested key is just a regular | |
// attribute and attempt to call the getter method for it, which | |
// will fall into the __call method if one doesn't exist. | |
else | |
{ | |
return $this->{"get_{$key}"}(); | |
} | |
} |
This file contains hidden or 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
/** | |
* Handle the dynamic retrieval of attributes and associations. | |
* | |
* @param string $key | |
* @return mixed | |
*/ | |
public function __get($key) | |
{ | |
// Lets check if the model has a function for this first | |
if(method_exists($this, $key)) | |
{ | |
return $this->$key(); | |
} | |
// First we will check to see if the requested key is an already loaded | |
// relationship and return it if it is. All relationships are stored | |
// in the special relationships array so they are not persisted. | |
if (array_key_exists($key, $this->relationships)) | |
{ | |
return $this->relationships[$key]; | |
} | |
// Next we'll check if the requested key is in the array of attributes | |
// for the model. These are simply regular properties that typically | |
// correspond to a single column on the database for the model. | |
else if (array_key_exists($key, $this->attributes)) | |
{ | |
return $this->{"get_{$key}"}(); | |
} | |
// If the item is not a loaded relationship, it may be a relationship | |
// that hasn't been loaded yet. If it is, we will lazy load it and | |
// set the value of the relationship in the relationship array. | |
else if (method_exists($this, $key)) | |
{ | |
return $this->relationships[$key] = $this->$key()->results(); | |
} | |
// Finally we will just assume the requested key is just a regular | |
// attribute and attempt to call the getter method for it, which | |
// will fall into the __call method if one doesn't exist. | |
else | |
{ | |
return $this->{"get_{$key}"}(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment