Skip to content

Commit

Permalink
made AR attribute manipulation behave consistent to hasAttribute()
Browse files Browse the repository at this point in the history
  • Loading branch information
cebe committed Nov 3, 2013
1 parent be68268 commit d8b94d6
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions framework/yii/db/ActiveRecord.php
Expand Up @@ -103,7 +103,7 @@ class ActiveRecord extends Model
/**
* @var array related models indexed by the relation names
*/
private $_related;
private $_related = [];

This comment has been minimized.

Copy link
@qiangxue

qiangxue Nov 3, 2013

Member

The reason that $_related is not initialized with an array is to save memory since an array takes a lot of memory. I'm not sure if this is still the case for PHP 5.4.

This comment has been minimized.

Copy link
@cebe

cebe Nov 3, 2013

Author Member
<?php
class A {
    private $_related;
}
$old = memory_get_usage();
$a = new A;
$mem = memory_get_usage();
echo 'memory usage: '.(($mem - $old)/1024).' kb' . "\n";

on PHP 5.4.21: memory usage: 0.1875 kb

<?php
class A {
    private $_related = [];
}
$old = memory_get_usage();
$a = new A;
$mem = memory_get_usage();
echo 'memory usage: '.(($mem - $old)/1024).' kb' . "\n";

on PHP 5.4.21: memory usage: 0.1875 kb

However if I put them together in one script:

memory usage null: 0.19140625 kb
memory usage array: 0.1171875 kb

So array is better here imo.

This comment has been minimized.

Copy link
@qiangxue

qiangxue Nov 3, 2013

Member

I just did some test too and confirmed this doesn't make difference in memory usage for PHP 5.4. So initializing it to an array is fine. Thanks.



/**
Expand Down Expand Up @@ -376,11 +376,11 @@ public function __get($name)
{
if (isset($this->_attributes[$name]) || array_key_exists($name, $this->_attributes)) {
return $this->_attributes[$name];
} elseif (isset($this->getTableSchema()->columns[$name])) {
} elseif ($this->hasAttribute($name)) {
return null;
} else {
$t = strtolower($name);
if (isset($this->_related[$t]) || $this->_related !== null && array_key_exists($t, $this->_related)) {
if (isset($this->_related[$t]) || array_key_exists($t, $this->_related)) {
return $this->_related[$t];
}
$value = parent::__get($name);
Expand Down Expand Up @@ -430,7 +430,7 @@ public function __isset($name)
*/
public function __unset($name)
{
if (isset($this->getTableSchema()->columns[$name])) {
if ($this->hasAttribute($name)) {
unset($this->_attributes[$name]);
} else {
$t = strtolower($name);
Expand Down Expand Up @@ -541,6 +541,16 @@ public function attributes()
return array_keys($this->getTableSchema()->columns);
}

/**
* Returns a value indicating whether the model has an attribute with the specified name.
* @param string $name the name of the attribute
* @return boolean whether the model has an attribute with the specified name.
*/
public function hasAttribute($name)
{
return isset($this->_attributes[$name]) || isset($this->getTableSchema()->columns[$name]);
}

/**
* Returns the named attribute value.
* If this record is the result of a query and the attribute is not loaded,
Expand Down Expand Up @@ -570,16 +580,6 @@ public function setAttribute($name, $value)
}
}

/**
* Returns a value indicating whether the model has an attribute with the specified name.
* @param string $name the name of the attribute
* @return boolean whether the model has an attribute with the specified name.
*/
public function hasAttribute($name)
{
return isset($this->_attributes[$name]) || isset($this->getTableSchema()->columns[$name]);
}

/**
* Returns the old attribute values.
* @return array the old attribute values (name-value pairs)
Expand Down Expand Up @@ -622,7 +622,7 @@ public function getOldAttribute($name)
*/
public function setOldAttribute($name, $value)
{
if (isset($this->_oldAttributes[$name]) || isset($this->getTableSchema()->columns[$name])) {
if (isset($this->_oldAttributes[$name]) || $this->hasAttribute($name)) {
$this->_oldAttributes[$name] = $value;
} else {
throw new InvalidParamException(get_class($this) . ' has no attribute named "' . $name . '".');
Expand Down Expand Up @@ -1138,7 +1138,7 @@ public function refresh()
$this->_attributes[$name] = $record->_attributes[$name];
}
$this->_oldAttributes = $this->_attributes;
$this->_related = null;
$this->_related = [];
return true;
}

Expand Down

0 comments on commit d8b94d6

Please sign in to comment.