|
|
@ -373,21 +373,13 @@ abstract class ActiveRecord extends \yii\base\Model |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (isset($this->_attributes[$name])) { |
|
|
|
if (isset($this->_attributes[$name])) { |
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} elseif (isset($this->getMetaData()->columns[$name])) { |
|
|
|
elseif (isset($this->getMetaData()->columns[$name])) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} elseif (isset($this->_related[$name])) { |
|
|
|
elseif (isset($this->_related[$name])) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} elseif (isset($this->getMetaData()->relations[$name])) { |
|
|
|
elseif (isset($this->getMetaData()->relations[$name])) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return $this->getRelatedRecord($name) !== null; |
|
|
|
return $this->getRelatedRecord($name) !== null; |
|
|
|
} |
|
|
|
} else { |
|
|
|
else |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return parent::__isset($name); |
|
|
|
return parent::__isset($name); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -402,13 +394,9 @@ abstract class ActiveRecord extends \yii\base\Model |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (isset($this->getMetaData()->columns[$name])) { |
|
|
|
if (isset($this->getMetaData()->columns[$name])) { |
|
|
|
unset($this->_attributes[$name]); |
|
|
|
unset($this->_attributes[$name]); |
|
|
|
} |
|
|
|
} elseif (isset($this->getMetaData()->relations[$name])) { |
|
|
|
elseif (isset($this->getMetaData()->relations[$name])) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
unset($this->_related[$name]); |
|
|
|
unset($this->_related[$name]); |
|
|
|
} |
|
|
|
} else { |
|
|
|
else |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
parent::__unset($name); |
|
|
|
parent::__unset($name); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -426,9 +414,7 @@ abstract class ActiveRecord extends \yii\base\Model |
|
|
|
if (isset($this->getMetaData()->relations[$name])) { |
|
|
|
if (isset($this->getMetaData()->relations[$name])) { |
|
|
|
if (empty($parameters)) { |
|
|
|
if (empty($parameters)) { |
|
|
|
return $this->getRelatedRecord($name, false); |
|
|
|
return $this->getRelatedRecord($name, false); |
|
|
|
} |
|
|
|
} else { |
|
|
|
else |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return $this->getRelatedRecord($name, false, $parameters[0]); |
|
|
|
return $this->getRelatedRecord($name, false, $parameters[0]); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -442,6 +428,20 @@ abstract class ActiveRecord extends \yii\base\Model |
|
|
|
return parent::__call($name, $parameters); |
|
|
|
return parent::__call($name, $parameters); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function initRelatedRecord($relation) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
$this->_related[$relation->name] = $relation->hasMany ? array() : null; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function addRelatedRecord($relation, $record) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if ($relation->hasMany) { |
|
|
|
|
|
|
|
$this->_related[$relation->name][] = $record; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
$this->_related[$relation->name] = $record; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Returns the related record(s). |
|
|
|
* Returns the related record(s). |
|
|
|
* This method will return the related record(s) of the current record. |
|
|
|
* This method will return the related record(s) of the current record. |
|
|
@ -464,8 +464,7 @@ abstract class ActiveRecord extends \yii\base\Model |
|
|
|
|
|
|
|
|
|
|
|
$md = $this->getMetaData(); |
|
|
|
$md = $this->getMetaData(); |
|
|
|
if (!isset($md->relations[$name])) { |
|
|
|
if (!isset($md->relations[$name])) { |
|
|
|
throw new Exception(Yii::t('yii', '{class} does not have relation "{name}".', |
|
|
|
throw new Exception(Yii::t('yii', '{class} does not have relation "{name}".', array('{class}' => get_class($this), '{name}' => $name))); |
|
|
|
array('{class}' => get_class($this), '{name}' => $name))); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Yii::trace('lazy loading ' . get_class($this) . '.' . $name, 'system.db.ar.ActiveRecord'); |
|
|
|
Yii::trace('lazy loading ' . get_class($this) . '.' . $name, 'system.db.ar.ActiveRecord'); |
|
|
@ -481,8 +480,7 @@ abstract class ActiveRecord extends \yii\base\Model |
|
|
|
$save = $this->_related[$name]; |
|
|
|
$save = $this->_related[$name]; |
|
|
|
} |
|
|
|
} |
|
|
|
$r = array($name => $params); |
|
|
|
$r = array($name => $params); |
|
|
|
} else |
|
|
|
} else { |
|
|
|
{ |
|
|
|
|
|
|
|
$r = $name; |
|
|
|
$r = $name; |
|
|
|
} |
|
|
|
} |
|
|
|
unset($this->_related[$name]); |
|
|
|
unset($this->_related[$name]); |
|
|
@ -493,13 +491,9 @@ abstract class ActiveRecord extends \yii\base\Model |
|
|
|
if (!isset($this->_related[$name])) { |
|
|
|
if (!isset($this->_related[$name])) { |
|
|
|
if ($relation instanceof CHasManyRelation) { |
|
|
|
if ($relation instanceof CHasManyRelation) { |
|
|
|
$this->_related[$name] = array(); |
|
|
|
$this->_related[$name] = array(); |
|
|
|
} |
|
|
|
} elseif ($relation instanceof CStatRelation) { |
|
|
|
elseif ($relation instanceof CStatRelation) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
$this->_related[$name] = $relation->defaultValue; |
|
|
|
$this->_related[$name] = $relation->defaultValue; |
|
|
|
} |
|
|
|
} else { |
|
|
|
else |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
$this->_related[$name] = null; |
|
|
|
$this->_related[$name] = null; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -508,14 +502,11 @@ abstract class ActiveRecord extends \yii\base\Model |
|
|
|
$results = $this->_related[$name]; |
|
|
|
$results = $this->_related[$name]; |
|
|
|
if ($exists) { |
|
|
|
if ($exists) { |
|
|
|
$this->_related[$name] = $save; |
|
|
|
$this->_related[$name] = $save; |
|
|
|
} |
|
|
|
} else { |
|
|
|
else |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
unset($this->_related[$name]); |
|
|
|
unset($this->_related[$name]); |
|
|
|
} |
|
|
|
} |
|
|
|
return $results; |
|
|
|
return $results; |
|
|
|
} else |
|
|
|
} else { |
|
|
|
{ |
|
|
|
|
|
|
|
return $this->_related[$name]; |
|
|
|
return $this->_related[$name]; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -617,9 +608,7 @@ abstract class ActiveRecord extends \yii\base\Model |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (property_exists($this, $name)) { |
|
|
|
if (property_exists($this, $name)) { |
|
|
|
return $this->$name; |
|
|
|
return $this->$name; |
|
|
|
} |
|
|
|
} elseif (isset($this->_attributes[$name])) { |
|
|
|
elseif (isset($this->_attributes[$name])) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return $this->_attributes[$name]; |
|
|
|
return $this->_attributes[$name]; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -636,13 +625,9 @@ abstract class ActiveRecord extends \yii\base\Model |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (property_exists($this, $name)) { |
|
|
|
if (property_exists($this, $name)) { |
|
|
|
$this->$name = $value; |
|
|
|
$this->$name = $value; |
|
|
|
} |
|
|
|
} elseif (isset($this->getMetaData()->table->columns[$name])) { |
|
|
|
elseif (isset($this->getMetaData()->table->columns[$name])) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
$this->_attributes[$name] = $value; |
|
|
|
$this->_attributes[$name] = $value; |
|
|
|
} |
|
|
|
} else { |
|
|
|
else |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
return true; |
|
|
|
return true; |
|
|
@ -660,31 +645,24 @@ abstract class ActiveRecord extends \yii\base\Model |
|
|
|
public function getAttributes($names = true) |
|
|
|
public function getAttributes($names = true) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$attributes = $this->_attributes; |
|
|
|
$attributes = $this->_attributes; |
|
|
|
foreach ($this->getMetaData()->columns as $name => $column) |
|
|
|
foreach ($this->getMetaData()->columns as $name => $column) { |
|
|
|
{ |
|
|
|
|
|
|
|
if (property_exists($this, $name)) { |
|
|
|
if (property_exists($this, $name)) { |
|
|
|
$attributes[$name] = $this->$name; |
|
|
|
$attributes[$name] = $this->$name; |
|
|
|
} |
|
|
|
} elseif ($names === true && !isset($attributes[$name])) { |
|
|
|
elseif ($names === true && !isset($attributes[$name])) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
$attributes[$name] = null; |
|
|
|
$attributes[$name] = null; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if (is_array($names)) { |
|
|
|
if (is_array($names)) { |
|
|
|
$attrs = array(); |
|
|
|
$attrs = array(); |
|
|
|
foreach ($names as $name) |
|
|
|
foreach ($names as $name) { |
|
|
|
{ |
|
|
|
|
|
|
|
if (property_exists($this, $name)) { |
|
|
|
if (property_exists($this, $name)) { |
|
|
|
$attrs[$name] = $this->$name; |
|
|
|
$attrs[$name] = $this->$name; |
|
|
|
} |
|
|
|
} else { |
|
|
|
else |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
$attrs[$name] = isset($attributes[$name]) ? $attributes[$name] : null; |
|
|
|
$attrs[$name] = isset($attributes[$name]) ? $attributes[$name] : null; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return $attrs; |
|
|
|
return $attrs; |
|
|
|
} else |
|
|
|
} else { |
|
|
|
{ |
|
|
|
|
|
|
|
return $attributes; |
|
|
|
return $attributes; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -716,9 +694,7 @@ abstract class ActiveRecord extends \yii\base\Model |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (!$runValidation || $this->validate($attributes)) { |
|
|
|
if (!$runValidation || $this->validate($attributes)) { |
|
|
|
return $this->getIsNewRecord() ? $this->insert($attributes) : $this->update($attributes); |
|
|
|
return $this->getIsNewRecord() ? $this->insert($attributes) : $this->update($attributes); |
|
|
|
} |
|
|
|
} else { |
|
|
|
else |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -798,8 +774,7 @@ abstract class ActiveRecord extends \yii\base\Model |
|
|
|
$event = new CModelEvent($this); |
|
|
|
$event = new CModelEvent($this); |
|
|
|
$this->onBeforeSave($event); |
|
|
|
$this->onBeforeSave($event); |
|
|
|
return $event->isValid; |
|
|
|
return $event->isValid; |
|
|
|
} else |
|
|
|
} else { |
|
|
|
{ |
|
|
|
|
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -830,8 +805,7 @@ abstract class ActiveRecord extends \yii\base\Model |
|
|
|
$event = new CModelEvent($this); |
|
|
|
$event = new CModelEvent($this); |
|
|
|
$this->onBeforeDelete($event); |
|
|
|
$this->onBeforeDelete($event); |
|
|
|
return $event->isValid; |
|
|
|
return $event->isValid; |
|
|
|
} else |
|
|
|
} else { |
|
|
|
{ |
|
|
|
|
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -910,11 +884,8 @@ abstract class ActiveRecord extends \yii\base\Model |
|
|
|
if ($table->sequenceName !== null) { |
|
|
|
if ($table->sequenceName !== null) { |
|
|
|
if (is_string($primaryKey) && $this->$primaryKey === null) { |
|
|
|
if (is_string($primaryKey) && $this->$primaryKey === null) { |
|
|
|
$this->$primaryKey = $builder->getLastInsertID($table); |
|
|
|
$this->$primaryKey = $builder->getLastInsertID($table); |
|
|
|
} |
|
|
|
} elseif (is_array($primaryKey)) { |
|
|
|
elseif (is_array($primaryKey)) |
|
|
|
foreach ($primaryKey as $pk) { |
|
|
|
{ |
|
|
|
|
|
|
|
foreach ($primaryKey as $pk) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if ($this->$pk === null) { |
|
|
|
if ($this->$pk === null) { |
|
|
|
$this->$pk = $builder->getLastInsertID($table); |
|
|
|
$this->$pk = $builder->getLastInsertID($table); |
|
|
|
break; |
|
|
|
break; |
|
|
@ -955,8 +926,7 @@ abstract class ActiveRecord extends \yii\base\Model |
|
|
|
$this->_pk = $this->getPrimaryKey(); |
|
|
|
$this->_pk = $this->getPrimaryKey(); |
|
|
|
$this->afterSave(); |
|
|
|
$this->afterSave(); |
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} else |
|
|
|
} else { |
|
|
|
{ |
|
|
|
|
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -984,13 +954,10 @@ abstract class ActiveRecord extends \yii\base\Model |
|
|
|
if (!$this->getIsNewRecord()) { |
|
|
|
if (!$this->getIsNewRecord()) { |
|
|
|
Yii::trace(get_class($this) . '.saveAttributes()', 'system.db.ar.ActiveRecord'); |
|
|
|
Yii::trace(get_class($this) . '.saveAttributes()', 'system.db.ar.ActiveRecord'); |
|
|
|
$values = array(); |
|
|
|
$values = array(); |
|
|
|
foreach ($attributes as $name => $value) |
|
|
|
foreach ($attributes as $name => $value) { |
|
|
|
{ |
|
|
|
|
|
|
|
if (is_integer($name)) { |
|
|
|
if (is_integer($name)) { |
|
|
|
$values[$value] = $this->$value; |
|
|
|
$values[$value] = $this->$value; |
|
|
|
} |
|
|
|
} else { |
|
|
|
else |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
$values[$name] = $this->$name = $value; |
|
|
|
$values[$name] = $this->$name = $value; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -1000,12 +967,10 @@ abstract class ActiveRecord extends \yii\base\Model |
|
|
|
if ($this->updateByPk($this->getOldPrimaryKey(), $values) > 0) { |
|
|
|
if ($this->updateByPk($this->getOldPrimaryKey(), $values) > 0) { |
|
|
|
$this->_pk = $this->getPrimaryKey(); |
|
|
|
$this->_pk = $this->getPrimaryKey(); |
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} else |
|
|
|
} else { |
|
|
|
{ |
|
|
|
|
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
} else |
|
|
|
} else { |
|
|
|
{ |
|
|
|
|
|
|
|
throw new Exception(Yii::t('yii', 'The active record cannot be updated because it is new.')); |
|
|
|
throw new Exception(Yii::t('yii', 'The active record cannot be updated because it is new.')); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -1032,13 +997,11 @@ abstract class ActiveRecord extends \yii\base\Model |
|
|
|
$criteria = $builder->createPkCriteria($table, $this->getOldPrimaryKey()); |
|
|
|
$criteria = $builder->createPkCriteria($table, $this->getOldPrimaryKey()); |
|
|
|
$command = $builder->createUpdateCounterCommand($this->getTableSchema(), $counters, $criteria); |
|
|
|
$command = $builder->createUpdateCounterCommand($this->getTableSchema(), $counters, $criteria); |
|
|
|
if ($command->execute()) { |
|
|
|
if ($command->execute()) { |
|
|
|
foreach ($counters as $name => $value) |
|
|
|
foreach ($counters as $name => $value) { |
|
|
|
{ |
|
|
|
|
|
|
|
$this->$name = $this->$name + $value; |
|
|
|
$this->$name = $this->$name + $value; |
|
|
|
} |
|
|
|
} |
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} else |
|
|
|
} else { |
|
|
|
{ |
|
|
|
|
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -1056,12 +1019,10 @@ abstract class ActiveRecord extends \yii\base\Model |
|
|
|
$result = $this->deleteByPk($this->getPrimaryKey()) > 0; |
|
|
|
$result = $this->deleteByPk($this->getPrimaryKey()) > 0; |
|
|
|
$this->afterDelete(); |
|
|
|
$this->afterDelete(); |
|
|
|
return $result; |
|
|
|
return $result; |
|
|
|
} else |
|
|
|
} else { |
|
|
|
{ |
|
|
|
|
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
} else |
|
|
|
} else { |
|
|
|
{ |
|
|
|
|
|
|
|
throw new Exception(Yii::t('yii', 'The active record cannot be deleted because it is new.')); |
|
|
|
throw new Exception(Yii::t('yii', 'The active record cannot be deleted because it is new.')); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -1076,19 +1037,15 @@ abstract class ActiveRecord extends \yii\base\Model |
|
|
|
if (!$this->getIsNewRecord() && ($record = $this->findByPk($this->getPrimaryKey())) !== null) { |
|
|
|
if (!$this->getIsNewRecord() && ($record = $this->findByPk($this->getPrimaryKey())) !== null) { |
|
|
|
$this->_attributes = array(); |
|
|
|
$this->_attributes = array(); |
|
|
|
$this->_related = array(); |
|
|
|
$this->_related = array(); |
|
|
|
foreach ($this->getMetaData()->columns as $name => $column) |
|
|
|
foreach ($this->getMetaData()->columns as $name => $column) { |
|
|
|
{ |
|
|
|
|
|
|
|
if (property_exists($this, $name)) { |
|
|
|
if (property_exists($this, $name)) { |
|
|
|
$this->$name = $record->$name; |
|
|
|
$this->$name = $record->$name; |
|
|
|
} |
|
|
|
} else { |
|
|
|
else |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
$this->_attributes[$name] = $record->$name; |
|
|
|
$this->_attributes[$name] = $record->$name; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} else |
|
|
|
} else { |
|
|
|
{ |
|
|
|
|
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|