|
|
@ -55,10 +55,6 @@ abstract class ActiveRecord extends Model |
|
|
|
* @var array old attribute values indexed by attribute names. |
|
|
|
* @var array old attribute values indexed by attribute names. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private $_oldAttributes; |
|
|
|
private $_oldAttributes; |
|
|
|
/** |
|
|
|
|
|
|
|
* @var array related records indexed by relation names. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private $_related; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -261,18 +257,18 @@ abstract class ActiveRecord extends Model |
|
|
|
* You may override this method if the table is not named after this convention. |
|
|
|
* You may override this method if the table is not named after this convention. |
|
|
|
* @return string the table name |
|
|
|
* @return string the table name |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function tableName() |
|
|
|
public static function tableName() |
|
|
|
{ |
|
|
|
{ |
|
|
|
return StringHelper::camel2id(basename(get_class($this)), '_'); |
|
|
|
return StringHelper::camel2id(basename(get_called_class()), '_'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Returns the schema information of the DB table associated with this AR class. |
|
|
|
* Returns the schema information of the DB table associated with this AR class. |
|
|
|
* @return TableSchema the schema information of the DB table associated with this AR class. |
|
|
|
* @return TableSchema the schema information of the DB table associated with this AR class. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function getTableSchema() |
|
|
|
public static function getTableSchema() |
|
|
|
{ |
|
|
|
{ |
|
|
|
return $this->getDbConnection()->getTableSchema($this->tableName()); |
|
|
|
return static::getDbConnection()->getTableSchema(static::tableName()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -284,23 +280,21 @@ abstract class ActiveRecord extends Model |
|
|
|
* for this AR class. |
|
|
|
* for this AR class. |
|
|
|
* @return string[] the primary keys of the associated database table. |
|
|
|
* @return string[] the primary keys of the associated database table. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function primaryKey() |
|
|
|
public static function primaryKey() |
|
|
|
{ |
|
|
|
{ |
|
|
|
return $this->getTableSchema()->primaryKey; |
|
|
|
return static::getTableSchema()->primaryKey; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Returns the default named scope that should be implicitly applied to all queries for this model. |
|
|
|
* Returns the default named scope that should be implicitly applied to all queries for this model. |
|
|
|
* Note, default scope only applies to SELECT queries. It is ignored for INSERT, UPDATE and DELETE queries. |
|
|
|
* Note, the default scope only applies to SELECT queries. It is ignored for INSERT, UPDATE and DELETE queries. |
|
|
|
* The default implementation simply returns an empty array. You may override this method |
|
|
|
* The default implementation simply returns an empty array. You may override this method |
|
|
|
* if the model needs to be queried with some default criteria (e.g. only active records should be returned). |
|
|
|
* if the model needs to be queried with some default criteria (e.g. only non-deleted users should be returned). |
|
|
|
* @param BaseActiveQuery |
|
|
|
* @param ActiveQuery |
|
|
|
* @return BaseActiveQuery the query criteria. This will be used as the parameter to the constructor |
|
|
|
|
|
|
|
* of {@link CDbCriteria}. |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static function defaultScope($query) |
|
|
|
public static function defaultScope($query) |
|
|
|
{ |
|
|
|
{ |
|
|
|
return $query; |
|
|
|
// todo: should we drop this? |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -312,21 +306,18 @@ abstract class ActiveRecord extends Model |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function __get($name) |
|
|
|
public function __get($name) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (isset($this->_attributes[$name])) { |
|
|
|
if (isset($this->_attributes[$name]) || array_key_exists($name, $this->_attributes)) { |
|
|
|
return $this->_attributes[$name]; |
|
|
|
return $this->_attributes[$name]; |
|
|
|
} |
|
|
|
} elseif (isset($this->getTableSchema()->columns[$name])) { |
|
|
|
if (isset($this->getTableSchema()->columns[$name])) { |
|
|
|
|
|
|
|
return null; |
|
|
|
return null; |
|
|
|
} elseif (method_exists($this, $name)) { |
|
|
|
} elseif (method_exists($this, $name)) { |
|
|
|
if (isset($this->_related[$name]) || $this->_related !== null && array_key_exists($name, $this->_related)) { |
|
|
|
// lazy loading related records |
|
|
|
return $this->_related[$name]; |
|
|
|
$query = $this->$name(); |
|
|
|
|
|
|
|
return $this->_attributes[$name] = $query->multiple ? $query->all() : $query->one(); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
// todo |
|
|
|
|
|
|
|
return $this->_related[$name] = $this->findByRelation($md->relations[$name]); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return parent::__get($name); |
|
|
|
return parent::__get($name); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* PHP setter magic method. |
|
|
|
* PHP setter magic method. |
|
|
@ -336,10 +327,8 @@ abstract class ActiveRecord extends Model |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function __set($name, $value) |
|
|
|
public function __set($name, $value) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (isset($this->getTableSchema()->columns[$name])) { |
|
|
|
if (isset($this->getTableSchema()->columns[$name]) || method_exists($this, $name)) { |
|
|
|
$this->_attributes[$name] = $value; |
|
|
|
$this->_attributes[$name] = $value; |
|
|
|
} elseif (method_exists($this, $name)) { |
|
|
|
|
|
|
|
$this->_related[$name] = $value; |
|
|
|
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
parent::__set($name, $value); |
|
|
|
parent::__set($name, $value); |
|
|
|
} |
|
|
|
} |
|
|
|