Skip to content

Instantly share code, notes, and snippets.

@tmdvs
Created November 19, 2012 12:21
Show Gist options
  • Save tmdvs/4110368 to your computer and use it in GitHub Desktop.
Save tmdvs/4110368 to your computer and use it in GitHub Desktop.
/**
* 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}"}();
}
}
/**
* 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