Skip to content

Commit

Permalink
add cache model attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
dimabdc committed Oct 17, 2016
1 parent e1768b3 commit 3e104c2
Showing 1 changed file with 28 additions and 13 deletions.
41 changes: 28 additions & 13 deletions lib/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,9 @@ class Model
*/
static $delegate = array();

/**
protected $_cacheAttributesData = [];

/**
* Constructs a model.
*
* When a user instantiates a new object (e.g.: it was not ActiveRecord that instantiated via a find)
Expand Down Expand Up @@ -334,15 +336,21 @@ public function __construct(array $attributes=array(), $guard_attributes=true, $
*/
public function &__get($name)
{
if (isset($this->_cacheAttributesData[$name])) {
return $this->_cacheAttributesData[$name];
}

// check for getter
if (method_exists($this, "get_$name"))
{
$name = "get_$name";
$value = $this->$name();
return $value;
}
} else {
$value = &$this->read_attribute($name);
}

return $this->read_attribute($name);
$this->_cacheAttributesData[$name] = &$value;
return $value;
}

/**
Expand Down Expand Up @@ -408,6 +416,8 @@ public function __isset($attribute_name)
*/
public function __set($name, $value)
{
$this->_cacheAttributesData = [];

if (array_key_exists($name, static::$alias_attribute))
$name = static::$alias_attribute[$name];

Expand Down Expand Up @@ -447,6 +457,8 @@ public function __wakeup()
*/
public function assign_attribute($name, $value)
{
$this->_cacheAttributesData = [];

$table = static::table();
if (!is_object($value)) {
if (array_key_exists($name, $table->columns)) {
Expand Down Expand Up @@ -1243,15 +1255,18 @@ private function set_attributes_via_mass_assignment(array &$attributes, $guard_a
throw new UndefinedPropertyException(get_called_class(),$exceptions);
}

/**
* Add a model to the given named ($name) relationship.
*
* @internal This should <strong>only</strong> be used by eager load
* @param Model $model
* @param $name of relationship for this table
* @return void
*/
public function set_relationship_from_eager_load(Model $model=null, $name)
/**
* Add a model to the given named ($name) relationship.
*
* @internal This should <strong>only</strong> be used by eager load
*
* @param Model $model
* @param string $name of relationship for this table
*
* @return Model|array|null
* @throws RelationshipException
*/
public function set_relationship_from_eager_load($model=null, $name)
{
$table = static::table();

Expand Down

0 comments on commit 3e104c2

Please sign in to comment.