One ActiveRecord class => multiple tables / Model "Factory" / Model cache function extension. #172

Closed
qiangxue opened this Issue Feb 15, 2012 · 3 comments

Comments

Projects
None yet
4 participants
@qiangxue
Member

qiangxue commented Feb 15, 2012

Describe the problem feature solves clearly. Explain why you need this and what is it exactly.

Extend CActiveRecord::model to return model instances that do not correspond to real classes, but to dynamically created models.
This allows a single class to be used to refer to different tables and still use features that rely on 'CActiveRecord::model($classname)'.

Currently a CActiveRecord::model($classname) corresponds to 1 table using a real class "$classname".

Some features like the CActiveGridView will use CActiveRecord::model to get new data from the table.

CActiveRecord::model($className) already behaves like a factory/object cache - it looks up the $className in its internal list $_models. However, it can only return instances for real classes (i.e., $className must correspond to a class), and hence 1 model instance <=> 1 table <=> 1 class. The changes allow n model instances <=> n tables <=> 1 class.

I detailed an example in http://www.yiiframework.com/forum/index.php?/topic/9747-extending-the-class-factory/page__p__48374#entry48374 .

Suggest on how it should be implemented if you have an idea.

First of all 'get_class' should not be used directly so that it is possible to override it to a virtual class.
So a call to 'get_class($this)' must be replace with $this->className or $this->getClassName(), and the base method in CModel can look like this:
public function getClassName() {
return get_class($this);
}
This must be in CModel because CActiveDataProvider, CForm, CActiveForm and CHtml rely on get_class($model) (which must be changed to $model->className). Of course gii/generators/model/templates/default/model.php must be adapted too (change get_class call).

Next, the 'CActiveRecord::model' must be changed. In addition to the above, by providing a CActiveRecord, this CActiveRecord will be used as the model (and put in the cache if no model is there yet).:

    public static function model($className=__CLASS__)
    {
            if(is_string($className) &amp;&amp; isset(self::$_models[$className]))
                return self::$_models[$className];
            else if(($className instanceof CActiveRecord) &amp;&amp;
                 isset(self::$_models[$className-&gt;getClassName()]))
                return self::$_models[$className-&gt;getClassName()];
            else {
                    if($className instanceof CActiveRecord) {
                            $model = $className;
                            $className = $model-&gt;getClassName();
                    } else {
                            $model=new $className(null);
                    }
                    self::$_models[$className]=$model;
                    $model-&gt;_md=new CActiveRecordMetaData($model);
                    $model-&gt;attachBehaviors($model-&gt;behaviors());
                    return $model;
            }
    }

So first this checks if the parameter is a string, in which case it checks the model cache using the string, if it is a CActiveRecord, it checks the cache using the 'getClassName' method.
If there is no model in the cache using either method, if the parameter is a CActiveRecord, it will be used as the model and added to the cache. If not, the previously existing functionality will be used.

This should not break existing functionality.

Provide links to existing implementations if any.

http://www.yiiframework.com/forum/index.php?/topic/9747-extending-the-class-factory/page__p__48374#entry48374

Migrated from http://code.google.com/p/yii/issues/detail?id=2956


earlier comments

qiang.xue said, at 2012-01-01T03:37:10.000Z:

set for 1.1.10 milestone

qiang.xue said, at 2012-01-01T03:37:36.000Z:

set for 1.1.10 milestone

mtitov@malkosua.com said, at 2012-01-06T13:47:54.000Z:

Just keep all logic in parent class and use child classes for specifying which table to use.

Mario.De.Weerd said, at 2012-01-06T14:23:26.000Z:

Hi

For the concrete case where I ran into problems, I have dynamic table names that depend on other attributes.

Here is some simplified code showing a method getting the model based on the 'context_id'. The table name is based on the 'context_id' and gets created if it does not exist.

public static function getModel($context_id) {
    $model = new ThisModel(null);
    $model->context_id=$context_id;
    try {
        $model=self::model($model);
    } catch (CDbException $e) {
            // TODO: handle actual error => supposing the table does not exist.
            $sql = "

CREATE TABLE IF NOT EXISTS ".$model->tableName()." (
id INT NOT NULL AUTO_INCREMENT ,
PRIMARY KEY (id)
)
ENGINE = InnoDB
";
$dbc= Yii::app()->getDb()->createCommand($sql);
Yii::trace($sql);
$dbc->execute(null);
$model=self::model($model);
$model->refreshMetaData();
}
}
return $model;
}

/**
 * @return string the associated database table name
 */
public function tableName()
{
    return 'mymodel_c'.$this->context_id;
}

Now as I wrote, the functionnality is pretty much there in Yii, there's just a few locations needing a minor update.

@dinhtrung

This comment has been minimized.

Show comment
Hide comment
@dinhtrung

dinhtrung Apr 4, 2013

I create a patch for this issue. Is it still alive?

diff --git a/framework/db/ar/CActiveFinder.php b/framework/db/ar/CActiveFinder.php
index b441525..976d75f 100644
--- a/framework/db/ar/CActiveFinder.php
+++ b/framework/db/ar/CActiveFinder.php
@@ -97,7 +97,7 @@ class CActiveFinder extends CComponent
     */
    public function findBySql($sql,$params=array())
    {
-       Yii::trace(get_class($this->_joinTree->model).'.findBySql() eagerly','system.db.ar.CActiveRecord');
+       Yii::trace(($this->_joinTree->model->getClassName()).'.findBySql() eagerly','system.db.ar.CActiveRecord');
        if(($row=$this->_builder->createSqlCommand($sql,$params)->queryRow())!==false)
        {
            $baseRecord=$this->_joinTree->model->populateRecord($row,false);
@@ -118,7 +118,7 @@ class CActiveFinder extends CComponent
     */
    public function findAllBySql($sql,$params=array())
    {
-       Yii::trace(get_class($this->_joinTree->model).'.findAllBySql() eagerly','system.db.ar.CActiveRecord');
+       Yii::trace(($this->_joinTree->model->getClassName()).'.findAllBySql() eagerly','system.db.ar.CActiveRecord');
        if(($rows=$this->_builder->createSqlCommand($sql,$params)->queryAll())!==array())
        {
            $baseRecords=$this->_joinTree->model->populateRecords($rows,false);
@@ -141,7 +141,7 @@ class CActiveFinder extends CComponent
     */
    public function count($criteria)
    {
-       Yii::trace(get_class($this->_joinTree->model).'.count() eagerly','system.db.ar.CActiveRecord');
+       Yii::trace(($this->_joinTree->model->getClassName()).'.count() eagerly','system.db.ar.CActiveRecord');
        $this->joinAll=$criteria->together!==true;

        $alias=$criteria->alias===null ? 't' : $criteria->alias;
@@ -209,7 +209,7 @@ class CActiveFinder extends CComponent

            if(($relation=$parent->model->getActiveRelation($with))===null)
                throw new CDbException(Yii::t('yii','Relation "{name}" is not defined in active record class "{class}".',
-                   array('{class}'=>get_class($parent->model), '{name}'=>$with)));
+                   array('{class}'=>($parent->model->getClassName()), '{name}'=>$with)));

            $relation=clone $relation;
            $model=CActiveRecord::model($relation->className);
@@ -521,7 +521,7 @@ class CJoinElement
            $joinTableName=$this->relation->getJunctionTableName();
            if(($joinTable=$schema->getTable($joinTableName))===null)
                throw new CDbException(Yii::t('yii','The relation "{relation}" in active record class "{class}" is not specified correctly: the join table "{joinTable}" given in the foreign key cannot be found in the database.',
-                   array('{class}'=>get_class($parent->model), '{relation}'=>$this->relation->name, '{joinTable}'=>$joinTableName)));
+                   array('{class}'=>($parent->model->getClassName()), '{relation}'=>$this->relation->name, '{joinTable}'=>$joinTableName)));
            $fks=$this->relation->getJunctionForeignKeys();

            $joinAlias=$schema->quoteTableName($this->relation->name.'_'.$this->tableAlias);
@@ -593,7 +593,7 @@ class CJoinElement
            }
            else
                throw new CDbException(Yii::t('yii','The relation "{relation}" in active record class "{class}" is specified with an incomplete foreign key. The foreign key must consist of columns referencing both joining tables.',
-                   array('{class}'=>get_class($parent->model), '{relation}'=>$this->relation->name)));
+                   array('{class}'=>($parent->model->getClassName()), '{relation}'=>$this->relation->name)));
        }
        else
        {
@@ -915,7 +915,7 @@ class CJoinElement
                }
                else
                    throw new CDbException(Yii::t('yii','Active record "{class}" is trying to select an invalid column "{column}". Note, the column must exist in the table or be an expression with alias.',
-                       array('{class}'=>get_class($this->model), '{column}'=>$name)));
+                       array('{class}'=>($this->model->getClassName()), '{column}'=>$name)));
            }
            // add primary key selection if they are not selected
            if(is_string($this->_pkAlias) && !isset($selected[$this->_pkAlias]))
@@ -988,7 +988,7 @@ class CJoinElement
            $joinTableName=$this->relation->getJunctionTableName();
            if(($joinTable=$schema->getTable($joinTableName))===null)
                throw new CDbException(Yii::t('yii','The relation "{relation}" in active record class "{class}" is not specified correctly: the join table "{joinTable}" given in the foreign key cannot be found in the database.',
-                   array('{class}'=>get_class($parent->model), '{relation}'=>$this->relation->name, '{joinTable}'=>$joinTableName)));
+                   array('{class}'=>($parent->model->getClassName()), '{relation}'=>$this->relation->name, '{joinTable}'=>$joinTableName)));
            $fks=$this->relation->getJunctionForeignKeys();

            return $this->joinManyMany($joinTable,$fks,$parent);
@@ -1041,7 +1041,7 @@ class CJoinElement

            if(!isset($fke->_table->columns[$fk]))
                throw new CDbException(Yii::t('yii','The relation "{relation}" in active record class "{class}" is specified with an invalid foreign key "{key}". There is no such column in the table "{table}".',
-                   array('{class}'=>get_class($parent->model), '{relation}'=>$this->relation->name, '{key}'=>$fk, '{table}'=>$fke->_table->name)));
+                   array('{class}'=>($parent->model->getClassName()), '{relation}'=>$this->relation->name, '{key}'=>$fk, '{table}'=>$fke->_table->name)));

            if(is_int($i))
            {
@@ -1083,7 +1083,7 @@ class CJoinElement
        {
            if(!isset($joinTable->columns[$fk]))
                throw new CDbException(Yii::t('yii','The relation "{relation}" in active record class "{class}" is specified with an invalid foreign key "{key}". There is no such column in the table "{table}".',
-                   array('{class}'=>get_class($parent->model), '{relation}'=>$this->relation->name, '{key}'=>$fk, '{table}'=>$joinTable->name)));
+                   array('{class}'=>($parent->model->getClassName()), '{relation}'=>$this->relation->name, '{key}'=>$fk, '{table}'=>$joinTable->name)));

            if(isset($joinTable->foreignKeys[$fk]))
            {
@@ -1137,7 +1137,7 @@ class CJoinElement
        }
        else
            throw new CDbException(Yii::t('yii','The relation "{relation}" in active record class "{class}" is specified with an incomplete foreign key. The foreign key must consist of columns referencing both joining tables.',
-               array('{class}'=>get_class($parent->model), '{relation}'=>$this->relation->name)));
+               array('{class}'=>($parent->model->getClassName()), '{relation}'=>$this->relation->name)));
    }
 }

@@ -1354,7 +1354,7 @@ class CStatElement
        $fks=preg_split('/\s*,\s*/',$relation->foreignKey,-1,PREG_SPLIT_NO_EMPTY);
        if(count($fks)!==count($pkTable->primaryKey))
            throw new CDbException(Yii::t('yii','The relation "{relation}" in active record class "{class}" is specified with an invalid foreign key. The columns in the key must match the primary keys of the table "{table}".',
-                       array('{class}'=>get_class($parent->model), '{relation}'=>$relation->name, '{table}'=>$pkTable->name)));
+                       array('{class}'=>($parent->model->getClassName()), '{relation}'=>$relation->name, '{table}'=>$pkTable->name)));

        // set up mapping between fk and pk columns
        $map=array();  // pk=>fk
@@ -1362,7 +1362,7 @@ class CStatElement
        {
            if(!isset($table->columns[$fk]))
                throw new CDbException(Yii::t('yii','The relation "{relation}" in active record class "{class}" is specified with an invalid foreign key "{key}". There is no such column in the table "{table}".',
-                   array('{class}'=>get_class($parent->model), '{relation}'=>$relation->name, '{key}'=>$fk, '{table}'=>$table->name)));
+                   array('{class}'=>($parent->model->getClassName()), '{relation}'=>$relation->name, '{key}'=>$fk, '{table}'=>$table->name)));

            if(isset($table->foreignKeys[$fk]))
            {
@@ -1371,7 +1371,7 @@ class CStatElement
                    $map[$pk]=$fk;
                else
                    throw new CDbException(Yii::t('yii','The relation "{relation}" in active record class "{class}" is specified with a foreign key "{key}" that does not point to the parent table "{table}".',
-                       array('{class}'=>get_class($parent->model), '{relation}'=>$relation->name, '{key}'=>$fk, '{table}'=>$pkTable->name)));
+                       array('{class}'=>($parent->model->getClassName()), '{relation}'=>$relation->name, '{key}'=>$fk, '{table}'=>$pkTable->name)));
            }
            else  // FK constraints undefined
            {
@@ -1465,12 +1465,12 @@ class CStatElement

        if(($joinTable=$builder->getSchema()->getTable($joinTableName))===null)
            throw new CDbException(Yii::t('yii','The relation "{relation}" in active record class "{class}" is not specified correctly. The join table "{joinTable}" given in the foreign key cannot be found in the database.',
-               array('{class}'=>get_class($this->_parent->model), '{relation}'=>$relation->name, '{joinTable}'=>$joinTableName)));
+               array('{class}'=>($this->_parent->model->getClassName()), '{relation}'=>$relation->name, '{joinTable}'=>$joinTableName)));

        $fks=preg_split('/\s*,\s*/',$keys,-1,PREG_SPLIT_NO_EMPTY);
        if(count($fks)!==count($table->primaryKey)+count($pkTable->primaryKey))
            throw new CDbException(Yii::t('yii','The relation "{relation}" in active record class "{class}" is specified with an incomplete foreign key. The foreign key must consist of columns referencing both joining tables.',
-               array('{class}'=>get_class($this->_parent->model), '{relation}'=>$relation->name)));
+               array('{class}'=>($this->_parent->model->getClassName()), '{relation}'=>$relation->name)));

        $joinCondition=array();
        $map=array();
@@ -1480,7 +1480,7 @@ class CStatElement
        {
            if(!isset($joinTable->columns[$fk]))
                throw new CDbException(Yii::t('yii','The relation "{relation}" in active record class "{class}" is specified with an invalid foreign key "{key}". There is no such column in the table "{table}".',
-                   array('{class}'=>get_class($this->_parent->model), '{relation}'=>$relation->name, '{key}'=>$fk, '{table}'=>$joinTable->name)));
+                   array('{class}'=>($this->_parent->model->getClassName()), '{relation}'=>$relation->name, '{key}'=>$fk, '{table}'=>$joinTable->name)));

            if(isset($joinTable->foreignKeys[$fk]))
            {
@@ -1524,7 +1524,7 @@ class CStatElement

        if($joinCondition===array() || $map===array())
            throw new CDbException(Yii::t('yii','The relation "{relation}" in active record class "{class}" is specified with an incomplete foreign key. The foreign key must consist of columns referencing both joining tables.',
-               array('{class}'=>get_class($this->_parent->model), '{relation}'=>$relation->name)));
+               array('{class}'=>($this->_parent->model->getClassName()), '{relation}'=>$relation->name)));

        $records=$this->_parent->records;

diff --git a/framework/db/ar/CActiveRecord.php b/framework/db/ar/CActiveRecord.php
index cb419d1..eaa9afd 100644
--- a/framework/db/ar/CActiveRecord.php
+++ b/framework/db/ar/CActiveRecord.php
@@ -225,6 +225,10 @@ abstract class CActiveRecord extends CModel
        return parent::__call($name,$parameters);
    }

+   public function getClassName() {
+       return get_class($this);
+   }
+
    /**
     * Returns the related record(s).
     * This method will return the related record(s) of the current record.
@@ -246,9 +250,9 @@ abstract class CActiveRecord extends CModel
        $md=$this->getMetaData();
        if(!isset($md->relations[$name]))
            throw new CDbException(Yii::t('yii','{class} does not have relation "{name}".',
-               array('{class}'=>get_class($this), '{name}'=>$name)));
+               array('{class}'=>$this->getClassName(), '{name}'=>$name)));

-       Yii::trace('lazy loading '.get_class($this).'.'.$name,'system.db.ar.CActiveRecord');
+       Yii::trace('lazy loading '.$this->getClassName().'.'.$name,'system.db.ar.CActiveRecord');
        $relation=$md->relations[$name];
        if($this->getIsNewRecord() && !$refresh && ($relation instanceof CHasOneRelation || $relation instanceof CHasManyRelation))
            return $relation instanceof CHasOneRelation ? null : array();
@@ -379,15 +383,22 @@ abstract class CActiveRecord extends CModel
     */
    public static function model($className=__CLASS__)
    {
-       if(isset(self::$_models[$className]))
+       if(is_string($className) && isset(self::$_models[$className]))
            return self::$_models[$className];
-       else
-       {
-           $model=self::$_models[$className]=new $className(null);
-           $model->_md=new CActiveRecordMetaData($model);
-           $model->attachBehaviors($model->behaviors());
-           return $model;
+       else if(($className instanceof CActiveRecord) && isset(self::$_models[$className->getClassName()]))
+           return self::$_models[$className->getClassName()];
+        else {
+           if($className instanceof CActiveRecord) {
+               $model = $className;
+                $className = $model->getClassName();
+       } else {
+           $model=new $className(null);
        }
+       self::$_models[$className]=$model;
+       $model->_md=new CActiveRecordMetaData($model);
+       $model->attachBehaviors($model->behaviors());
+       return $model;
+        }
    }

    /**
@@ -399,7 +410,7 @@ abstract class CActiveRecord extends CModel
        if($this->_md!==null)
            return $this->_md;
        else
-           return $this->_md=self::model(get_class($this))->_md;
+           return $this->_md=self::model($this->getClassName())->_md;
    }

    /**
@@ -411,7 +422,7 @@ abstract class CActiveRecord extends CModel
     */
    public function refreshMetaData()
    {
-       $finder=self::model(get_class($this));
+       $finder=self::model($this->getClassName());
        $finder->_md=new CActiveRecordMetaData($finder);
        if($this!==$finder)
            $this->_md=$finder->_md;
@@ -425,7 +436,7 @@ abstract class CActiveRecord extends CModel
     */
    public function tableName()
    {
-       return get_class($this);
+       return $this->getClassName();
    }

    /**
@@ -1016,7 +1027,7 @@ abstract class CActiveRecord extends CModel
            throw new CDbException(Yii::t('yii','The active record cannot be inserted to database because it is not new.'));
        if($this->beforeSave())
        {
-           Yii::trace(get_class($this).'.insert()','system.db.ar.CActiveRecord');
+           Yii::trace($this->getClassName().'.insert()','system.db.ar.CActiveRecord');
            $builder=$this->getCommandBuilder();
            $table=$this->getMetaData()->tableSchema;
            $command=$builder->createInsertCommand($table,$this->getAttributes($attributes));
@@ -1064,7 +1075,7 @@ abstract class CActiveRecord extends CModel
            throw new CDbException(Yii::t('yii','The active record cannot be updated because it is new.'));
        if($this->beforeSave())
        {
-           Yii::trace(get_class($this).'.update()','system.db.ar.CActiveRecord');
+           Yii::trace($this->getClassName().'.update()','system.db.ar.CActiveRecord');
            if($this->_pk===null)
                $this->_pk=$this->getPrimaryKey();
            $this->updateByPk($this->getOldPrimaryKey(),$this->getAttributes($attributes));
@@ -1098,7 +1109,7 @@ abstract class CActiveRecord extends CModel
    {
        if(!$this->getIsNewRecord())
        {
-           Yii::trace(get_class($this).'.saveAttributes()','system.db.ar.CActiveRecord');
+           Yii::trace($this->getClassName().'.saveAttributes()','system.db.ar.CActiveRecord');
            $values=array();
            foreach($attributes as $name=>$value)
            {
@@ -1138,7 +1149,7 @@ abstract class CActiveRecord extends CModel
     */
    public function saveCounters($counters)
    {
-       Yii::trace(get_class($this).'.saveCounters()','system.db.ar.CActiveRecord');
+       Yii::trace($this->getClassName().'.saveCounters()','system.db.ar.CActiveRecord');
        $builder=$this->getCommandBuilder();
        $table=$this->getTableSchema();
        $criteria=$builder->createPkCriteria($table,$this->getOldPrimaryKey());
@@ -1162,7 +1173,7 @@ abstract class CActiveRecord extends CModel
    {
        if(!$this->getIsNewRecord())
        {
-           Yii::trace(get_class($this).'.delete()','system.db.ar.CActiveRecord');
+           Yii::trace($this->getClassName().'.delete()','system.db.ar.CActiveRecord');
            if($this->beforeDelete())
            {
                $result=$this->deleteByPk($this->getPrimaryKey())>0;
@@ -1182,7 +1193,7 @@ abstract class CActiveRecord extends CModel
     */
    public function refresh()
    {
-       Yii::trace(get_class($this).'.refresh()','system.db.ar.CActiveRecord');
+       Yii::trace($this->getClassName().'.refresh()','system.db.ar.CActiveRecord');
        if(($record=$this->findByPk($this->getPrimaryKey()))!==null)
        {
            $this->_attributes=array();
@@ -1397,7 +1408,7 @@ abstract class CActiveRecord extends CModel
     */
    public function find($condition='',$params=array())
    {
-       Yii::trace(get_class($this).'.find()','system.db.ar.CActiveRecord');
+       Yii::trace($this->getClassName().'.find()','system.db.ar.CActiveRecord');
        $criteria=$this->getCommandBuilder()->createCriteria($condition,$params);
        return $this->query($criteria);
    }
@@ -1411,7 +1422,7 @@ abstract class CActiveRecord extends CModel
     */
    public function findAll($condition='',$params=array())
    {
-       Yii::trace(get_class($this).'.findAll()','system.db.ar.CActiveRecord');
+       Yii::trace($this->getClassName().'.findAll()','system.db.ar.CActiveRecord');
        $criteria=$this->getCommandBuilder()->createCriteria($condition,$params);
        return $this->query($criteria,true);
    }
@@ -1426,7 +1437,7 @@ abstract class CActiveRecord extends CModel
     */
    public function findByPk($pk,$condition='',$params=array())
    {
-       Yii::trace(get_class($this).'.findByPk()','system.db.ar.CActiveRecord');
+       Yii::trace($this->getClassName().'.findByPk()','system.db.ar.CActiveRecord');
        $prefix=$this->getTableAlias(true).'.';
        $criteria=$this->getCommandBuilder()->createPkCriteria($this->getTableSchema(),$pk,$condition,$params,$prefix);
        return $this->query($criteria);
@@ -1442,7 +1453,7 @@ abstract class CActiveRecord extends CModel
     */
    public function findAllByPk($pk,$condition='',$params=array())
    {
-       Yii::trace(get_class($this).'.findAllByPk()','system.db.ar.CActiveRecord');
+       Yii::trace($this->getClassName().'.findAllByPk()','system.db.ar.CActiveRecord');
        $prefix=$this->getTableAlias(true).'.';
        $criteria=$this->getCommandBuilder()->createPkCriteria($this->getTableSchema(),$pk,$condition,$params,$prefix);
        return $this->query($criteria,true);
@@ -1459,7 +1470,7 @@ abstract class CActiveRecord extends CModel
     */
    public function findByAttributes($attributes,$condition='',$params=array())
    {
-       Yii::trace(get_class($this).'.findByAttributes()','system.db.ar.CActiveRecord');
+       Yii::trace($this->getClassName().'.findByAttributes()','system.db.ar.CActiveRecord');
        $prefix=$this->getTableAlias(true).'.';
        $criteria=$this->getCommandBuilder()->createColumnCriteria($this->getTableSchema(),$attributes,$condition,$params,$prefix);
        return $this->query($criteria);
@@ -1476,7 +1487,7 @@ abstract class CActiveRecord extends CModel
     */
    public function findAllByAttributes($attributes,$condition='',$params=array())
    {
-       Yii::trace(get_class($this).'.findAllByAttributes()','system.db.ar.CActiveRecord');
+       Yii::trace($this->getClassName().'.findAllByAttributes()','system.db.ar.CActiveRecord');
        $prefix=$this->getTableAlias(true).'.';
        $criteria=$this->getCommandBuilder()->createColumnCriteria($this->getTableSchema(),$attributes,$condition,$params,$prefix);
        return $this->query($criteria,true);
@@ -1490,7 +1501,7 @@ abstract class CActiveRecord extends CModel
     */
    public function findBySql($sql,$params=array())
    {
-       Yii::trace(get_class($this).'.findBySql()','system.db.ar.CActiveRecord');
+       Yii::trace($this->getClassName().'.findBySql()','system.db.ar.CActiveRecord');
        $this->beforeFind();
        if(($criteria=$this->getDbCriteria(false))!==null && !empty($criteria->with))
        {
@@ -1513,7 +1524,7 @@ abstract class CActiveRecord extends CModel
     */
    public function findAllBySql($sql,$params=array())
    {
-       Yii::trace(get_class($this).'.findAllBySql()','system.db.ar.CActiveRecord');
+       Yii::trace($this->getClassName().'.findAllBySql()','system.db.ar.CActiveRecord');
        $this->beforeFind();
        if(($criteria=$this->getDbCriteria(false))!==null && !empty($criteria->with))
        {
@@ -1537,7 +1548,7 @@ abstract class CActiveRecord extends CModel
     */
    public function count($condition='',$params=array())
    {
-       Yii::trace(get_class($this).'.count()','system.db.ar.CActiveRecord');
+       Yii::trace($this->getClassName().'.count()','system.db.ar.CActiveRecord');
        $builder=$this->getCommandBuilder();
        $criteria=$builder->createCriteria($condition,$params);
        $this->applyScopes($criteria);
@@ -1563,7 +1574,7 @@ abstract class CActiveRecord extends CModel
     */
    public function countByAttributes($attributes,$condition='',$params=array())
    {
-       Yii::trace(get_class($this).'.countByAttributes()','system.db.ar.CActiveRecord');
+       Yii::trace($this->getClassName().'.countByAttributes()','system.db.ar.CActiveRecord');
        $prefix=$this->getTableAlias(true).'.';
        $builder=$this->getCommandBuilder();
        $criteria=$builder->createColumnCriteria($this->getTableSchema(),$attributes,$condition,$params,$prefix);
@@ -1588,7 +1599,7 @@ abstract class CActiveRecord extends CModel
     */
    public function countBySql($sql,$params=array())
    {
-       Yii::trace(get_class($this).'.countBySql()','system.db.ar.CActiveRecord');
+       Yii::trace($this->getClassName().'.countBySql()','system.db.ar.CActiveRecord');
        return $this->getCommandBuilder()->createSqlCommand($sql,$params)->queryScalar();
    }

@@ -1601,7 +1612,7 @@ abstract class CActiveRecord extends CModel
     */
    public function exists($condition='',$params=array())
    {
-       Yii::trace(get_class($this).'.exists()','system.db.ar.CActiveRecord');
+       Yii::trace($this->getClassName().'.exists()','system.db.ar.CActiveRecord');
        $builder=$this->getCommandBuilder();
        $criteria=$builder->createCriteria($condition,$params);
        $table=$this->getTableSchema();
@@ -1683,7 +1694,7 @@ abstract class CActiveRecord extends CModel
     */
    public function updateByPk($pk,$attributes,$condition='',$params=array())
    {
-       Yii::trace(get_class($this).'.updateByPk()','system.db.ar.CActiveRecord');
+       Yii::trace($this->getClassName().'.updateByPk()','system.db.ar.CActiveRecord');
        $builder=$this->getCommandBuilder();
        $table=$this->getTableSchema();
        $criteria=$builder->createPkCriteria($table,$pk,$condition,$params);
@@ -1702,7 +1713,7 @@ abstract class CActiveRecord extends CModel
     */
    public function updateAll($attributes,$condition='',$params=array())
    {
-       Yii::trace(get_class($this).'.updateAll()','system.db.ar.CActiveRecord');
+       Yii::trace($this->getClassName().'.updateAll()','system.db.ar.CActiveRecord');
        $builder=$this->getCommandBuilder();
        $criteria=$builder->createCriteria($condition,$params);
        $command=$builder->createUpdateCommand($this->getTableSchema(),$attributes,$criteria);
@@ -1721,7 +1732,7 @@ abstract class CActiveRecord extends CModel
     */
    public function updateCounters($counters,$condition='',$params=array())
    {
-       Yii::trace(get_class($this).'.updateCounters()','system.db.ar.CActiveRecord');
+       Yii::trace($this->getClassName().'.updateCounters()','system.db.ar.CActiveRecord');
        $builder=$this->getCommandBuilder();
        $criteria=$builder->createCriteria($condition,$params);
        $command=$builder->createUpdateCounterCommand($this->getTableSchema(),$counters,$criteria);
@@ -1738,7 +1749,7 @@ abstract class CActiveRecord extends CModel
     */
    public function deleteByPk($pk,$condition='',$params=array())
    {
-       Yii::trace(get_class($this).'.deleteByPk()','system.db.ar.CActiveRecord');
+       Yii::trace($this->getClassName().'.deleteByPk()','system.db.ar.CActiveRecord');
        $builder=$this->getCommandBuilder();
        $criteria=$builder->createPkCriteria($this->getTableSchema(),$pk,$condition,$params);
        $command=$builder->createDeleteCommand($this->getTableSchema(),$criteria);
@@ -1754,7 +1765,7 @@ abstract class CActiveRecord extends CModel
     */
    public function deleteAll($condition='',$params=array())
    {
-       Yii::trace(get_class($this).'.deleteAll()','system.db.ar.CActiveRecord');
+       Yii::trace($this->getClassName().'.deleteAll()','system.db.ar.CActiveRecord');
        $builder=$this->getCommandBuilder();
        $criteria=$builder->createCriteria($condition,$params);
        $command=$builder->createDeleteCommand($this->getTableSchema(),$criteria);
@@ -1772,7 +1783,7 @@ abstract class CActiveRecord extends CModel
     */
    public function deleteAllByAttributes($attributes,$condition='',$params=array())
    {
-       Yii::trace(get_class($this).'.deleteAllByAttributes()','system.db.ar.CActiveRecord');
+       Yii::trace($this->getClassName().'.deleteAllByAttributes()','system.db.ar.CActiveRecord');
        $builder=$this->getCommandBuilder();
        $table=$this->getTableSchema();
        $criteria=$builder->createColumnCriteria($table,$attributes,$condition,$params);
@@ -1850,7 +1861,7 @@ abstract class CActiveRecord extends CModel
     */
    protected function instantiate($attributes)
    {
-       $class=get_class($this);
+       $class=$this->getClassName();
        $model=new $class(null);
        return $model;
    }
diff --git a/framework/web/CActiveDataProvider.php b/framework/web/CActiveDataProvider.php
index 87233ea..a63d334 100644
--- a/framework/web/CActiveDataProvider.php
+++ b/framework/web/CActiveDataProvider.php
@@ -75,7 +75,7 @@ class CActiveDataProvider extends CDataProvider
        }
        elseif($modelClass instanceof CActiveRecord)
        {
-           $this->modelClass=get_class($modelClass);
+           $this->modelClass=($modelClass->getClassName());
            $this->model=$modelClass;
        }
        $this->setId($this->modelClass);
diff --git a/framework/web/form/CForm.php b/framework/web/form/CForm.php
index 7b97893..8deb336 100644
--- a/framework/web/form/CForm.php
+++ b/framework/web/form/CForm.php
@@ -216,7 +216,7 @@ class CForm extends CFormElement implements ArrayAccess
    {
        if($this->_model!==null)
        {
-           $class=get_class($this->_model);
+           $class=($this->_model->getClassName());
            if(strcasecmp($this->getRoot()->method,'get'))
            {
                if(isset($_POST[$class]))
diff --git a/framework/web/widgets/CActiveForm.php b/framework/web/widgets/CActiveForm.php
index e0ddb68..c235122 100644
--- a/framework/web/widgets/CActiveForm.php
+++ b/framework/web/widgets/CActiveForm.php
@@ -434,7 +434,7 @@ class CActiveForm extends CWidget
            'id'=>$id,
            'inputID'=>$inputID,
            'errorID'=>$htmlOptions['id'],
-           'model'=>get_class($model),
+           'model'=>($model->getClassName()),
            'name'=>$attribute,
            'enableAjaxValidation'=>$enableAjaxValidation,
        );
@@ -830,8 +830,8 @@ class CActiveForm extends CWidget
            $models=array($models);
        foreach($models as $model)
        {
-           if($loadInput && isset($_POST[get_class($model)]))
-               $model->attributes=$_POST[get_class($model)];
+           if($loadInput && isset($_POST[$model->getClassName()]))
+               $model->attributes=$_POST[$model->getClassName()];
            $model->validate($attributes);
            foreach($model->getErrors() as $attribute=>$errors)
                $result[CHtml::activeId($model,$attribute)]=$errors;
@@ -858,8 +858,8 @@ class CActiveForm extends CWidget
            $models=array($models);
        foreach($models as $i=>$model)
        {
-           if($loadInput && isset($_POST[get_class($model)][$i]))
-               $model->attributes=$_POST[get_class($model)][$i];
+           if($loadInput && isset($_POST[$model->getClassName()][$i]))
+               $model->attributes=$_POST[$model->getClassName()][$i];
            $model->validate($attributes);
            foreach($model->getErrors() as $attribute=>$errors)
                $result[CHtml::activeId($model,'['.$i.']'.$attribute)]=$errors;
diff --git a/framework/web/widgets/CInputWidget.php b/framework/web/widgets/CInputWidget.php
index 2148084..511e3f3 100644
--- a/framework/web/widgets/CInputWidget.php
+++ b/framework/web/widgets/CInputWidget.php
@@ -57,7 +57,7 @@ abstract class CInputWidget extends CWidget
        elseif($this->hasModel())
            $name=CHtml::activeName($this->model,$this->attribute);
        else
-           throw new CException(Yii::t('yii','{class} must specify "model" and "attribute" or "name" property values.',array('{class}'=>get_class($this))));
+           throw new CException(Yii::t('yii','{class} must specify "model" and "attribute" or "name" property values.',array('{class}'=>($this->getClassName()))));

        if(($id=$this->getId(false))===null)
        {
diff --git a/framework/zii/widgets/jui/CJuiInputWidget.php b/framework/zii/widgets/jui/CJuiInputWidget.php
index 01f756a..53bc920 100644
--- a/framework/zii/widgets/jui/CJuiInputWidget.php
+++ b/framework/zii/widgets/jui/CJuiInputWidget.php
@@ -49,7 +49,7 @@ abstract class CJuiInputWidget extends CJuiWidget
        elseif($this->hasModel())
            $name=CHtml::activeName($this->model,$this->attribute);
        else
-           throw new CException(Yii::t('zii','{class} must specify "model" and "attribute" or "name" property values.',array('{class}'=>get_class($this))));
+           throw new CException(Yii::t('zii','{class} must specify "model" and "attribute" or "name" property values.',array('{class}'=>$this->getClassName())));

        if(($id=$this->getId(false))===null)
        {

I create a patch for this issue. Is it still alive?

diff --git a/framework/db/ar/CActiveFinder.php b/framework/db/ar/CActiveFinder.php
index b441525..976d75f 100644
--- a/framework/db/ar/CActiveFinder.php
+++ b/framework/db/ar/CActiveFinder.php
@@ -97,7 +97,7 @@ class CActiveFinder extends CComponent
     */
    public function findBySql($sql,$params=array())
    {
-       Yii::trace(get_class($this->_joinTree->model).'.findBySql() eagerly','system.db.ar.CActiveRecord');
+       Yii::trace(($this->_joinTree->model->getClassName()).'.findBySql() eagerly','system.db.ar.CActiveRecord');
        if(($row=$this->_builder->createSqlCommand($sql,$params)->queryRow())!==false)
        {
            $baseRecord=$this->_joinTree->model->populateRecord($row,false);
@@ -118,7 +118,7 @@ class CActiveFinder extends CComponent
     */
    public function findAllBySql($sql,$params=array())
    {
-       Yii::trace(get_class($this->_joinTree->model).'.findAllBySql() eagerly','system.db.ar.CActiveRecord');
+       Yii::trace(($this->_joinTree->model->getClassName()).'.findAllBySql() eagerly','system.db.ar.CActiveRecord');
        if(($rows=$this->_builder->createSqlCommand($sql,$params)->queryAll())!==array())
        {
            $baseRecords=$this->_joinTree->model->populateRecords($rows,false);
@@ -141,7 +141,7 @@ class CActiveFinder extends CComponent
     */
    public function count($criteria)
    {
-       Yii::trace(get_class($this->_joinTree->model).'.count() eagerly','system.db.ar.CActiveRecord');
+       Yii::trace(($this->_joinTree->model->getClassName()).'.count() eagerly','system.db.ar.CActiveRecord');
        $this->joinAll=$criteria->together!==true;

        $alias=$criteria->alias===null ? 't' : $criteria->alias;
@@ -209,7 +209,7 @@ class CActiveFinder extends CComponent

            if(($relation=$parent->model->getActiveRelation($with))===null)
                throw new CDbException(Yii::t('yii','Relation "{name}" is not defined in active record class "{class}".',
-                   array('{class}'=>get_class($parent->model), '{name}'=>$with)));
+                   array('{class}'=>($parent->model->getClassName()), '{name}'=>$with)));

            $relation=clone $relation;
            $model=CActiveRecord::model($relation->className);
@@ -521,7 +521,7 @@ class CJoinElement
            $joinTableName=$this->relation->getJunctionTableName();
            if(($joinTable=$schema->getTable($joinTableName))===null)
                throw new CDbException(Yii::t('yii','The relation "{relation}" in active record class "{class}" is not specified correctly: the join table "{joinTable}" given in the foreign key cannot be found in the database.',
-                   array('{class}'=>get_class($parent->model), '{relation}'=>$this->relation->name, '{joinTable}'=>$joinTableName)));
+                   array('{class}'=>($parent->model->getClassName()), '{relation}'=>$this->relation->name, '{joinTable}'=>$joinTableName)));
            $fks=$this->relation->getJunctionForeignKeys();

            $joinAlias=$schema->quoteTableName($this->relation->name.'_'.$this->tableAlias);
@@ -593,7 +593,7 @@ class CJoinElement
            }
            else
                throw new CDbException(Yii::t('yii','The relation "{relation}" in active record class "{class}" is specified with an incomplete foreign key. The foreign key must consist of columns referencing both joining tables.',
-                   array('{class}'=>get_class($parent->model), '{relation}'=>$this->relation->name)));
+                   array('{class}'=>($parent->model->getClassName()), '{relation}'=>$this->relation->name)));
        }
        else
        {
@@ -915,7 +915,7 @@ class CJoinElement
                }
                else
                    throw new CDbException(Yii::t('yii','Active record "{class}" is trying to select an invalid column "{column}". Note, the column must exist in the table or be an expression with alias.',
-                       array('{class}'=>get_class($this->model), '{column}'=>$name)));
+                       array('{class}'=>($this->model->getClassName()), '{column}'=>$name)));
            }
            // add primary key selection if they are not selected
            if(is_string($this->_pkAlias) && !isset($selected[$this->_pkAlias]))
@@ -988,7 +988,7 @@ class CJoinElement
            $joinTableName=$this->relation->getJunctionTableName();
            if(($joinTable=$schema->getTable($joinTableName))===null)
                throw new CDbException(Yii::t('yii','The relation "{relation}" in active record class "{class}" is not specified correctly: the join table "{joinTable}" given in the foreign key cannot be found in the database.',
-                   array('{class}'=>get_class($parent->model), '{relation}'=>$this->relation->name, '{joinTable}'=>$joinTableName)));
+                   array('{class}'=>($parent->model->getClassName()), '{relation}'=>$this->relation->name, '{joinTable}'=>$joinTableName)));
            $fks=$this->relation->getJunctionForeignKeys();

            return $this->joinManyMany($joinTable,$fks,$parent);
@@ -1041,7 +1041,7 @@ class CJoinElement

            if(!isset($fke->_table->columns[$fk]))
                throw new CDbException(Yii::t('yii','The relation "{relation}" in active record class "{class}" is specified with an invalid foreign key "{key}". There is no such column in the table "{table}".',
-                   array('{class}'=>get_class($parent->model), '{relation}'=>$this->relation->name, '{key}'=>$fk, '{table}'=>$fke->_table->name)));
+                   array('{class}'=>($parent->model->getClassName()), '{relation}'=>$this->relation->name, '{key}'=>$fk, '{table}'=>$fke->_table->name)));

            if(is_int($i))
            {
@@ -1083,7 +1083,7 @@ class CJoinElement
        {
            if(!isset($joinTable->columns[$fk]))
                throw new CDbException(Yii::t('yii','The relation "{relation}" in active record class "{class}" is specified with an invalid foreign key "{key}". There is no such column in the table "{table}".',
-                   array('{class}'=>get_class($parent->model), '{relation}'=>$this->relation->name, '{key}'=>$fk, '{table}'=>$joinTable->name)));
+                   array('{class}'=>($parent->model->getClassName()), '{relation}'=>$this->relation->name, '{key}'=>$fk, '{table}'=>$joinTable->name)));

            if(isset($joinTable->foreignKeys[$fk]))
            {
@@ -1137,7 +1137,7 @@ class CJoinElement
        }
        else
            throw new CDbException(Yii::t('yii','The relation "{relation}" in active record class "{class}" is specified with an incomplete foreign key. The foreign key must consist of columns referencing both joining tables.',
-               array('{class}'=>get_class($parent->model), '{relation}'=>$this->relation->name)));
+               array('{class}'=>($parent->model->getClassName()), '{relation}'=>$this->relation->name)));
    }
 }

@@ -1354,7 +1354,7 @@ class CStatElement
        $fks=preg_split('/\s*,\s*/',$relation->foreignKey,-1,PREG_SPLIT_NO_EMPTY);
        if(count($fks)!==count($pkTable->primaryKey))
            throw new CDbException(Yii::t('yii','The relation "{relation}" in active record class "{class}" is specified with an invalid foreign key. The columns in the key must match the primary keys of the table "{table}".',
-                       array('{class}'=>get_class($parent->model), '{relation}'=>$relation->name, '{table}'=>$pkTable->name)));
+                       array('{class}'=>($parent->model->getClassName()), '{relation}'=>$relation->name, '{table}'=>$pkTable->name)));

        // set up mapping between fk and pk columns
        $map=array();  // pk=>fk
@@ -1362,7 +1362,7 @@ class CStatElement
        {
            if(!isset($table->columns[$fk]))
                throw new CDbException(Yii::t('yii','The relation "{relation}" in active record class "{class}" is specified with an invalid foreign key "{key}". There is no such column in the table "{table}".',
-                   array('{class}'=>get_class($parent->model), '{relation}'=>$relation->name, '{key}'=>$fk, '{table}'=>$table->name)));
+                   array('{class}'=>($parent->model->getClassName()), '{relation}'=>$relation->name, '{key}'=>$fk, '{table}'=>$table->name)));

            if(isset($table->foreignKeys[$fk]))
            {
@@ -1371,7 +1371,7 @@ class CStatElement
                    $map[$pk]=$fk;
                else
                    throw new CDbException(Yii::t('yii','The relation "{relation}" in active record class "{class}" is specified with a foreign key "{key}" that does not point to the parent table "{table}".',
-                       array('{class}'=>get_class($parent->model), '{relation}'=>$relation->name, '{key}'=>$fk, '{table}'=>$pkTable->name)));
+                       array('{class}'=>($parent->model->getClassName()), '{relation}'=>$relation->name, '{key}'=>$fk, '{table}'=>$pkTable->name)));
            }
            else  // FK constraints undefined
            {
@@ -1465,12 +1465,12 @@ class CStatElement

        if(($joinTable=$builder->getSchema()->getTable($joinTableName))===null)
            throw new CDbException(Yii::t('yii','The relation "{relation}" in active record class "{class}" is not specified correctly. The join table "{joinTable}" given in the foreign key cannot be found in the database.',
-               array('{class}'=>get_class($this->_parent->model), '{relation}'=>$relation->name, '{joinTable}'=>$joinTableName)));
+               array('{class}'=>($this->_parent->model->getClassName()), '{relation}'=>$relation->name, '{joinTable}'=>$joinTableName)));

        $fks=preg_split('/\s*,\s*/',$keys,-1,PREG_SPLIT_NO_EMPTY);
        if(count($fks)!==count($table->primaryKey)+count($pkTable->primaryKey))
            throw new CDbException(Yii::t('yii','The relation "{relation}" in active record class "{class}" is specified with an incomplete foreign key. The foreign key must consist of columns referencing both joining tables.',
-               array('{class}'=>get_class($this->_parent->model), '{relation}'=>$relation->name)));
+               array('{class}'=>($this->_parent->model->getClassName()), '{relation}'=>$relation->name)));

        $joinCondition=array();
        $map=array();
@@ -1480,7 +1480,7 @@ class CStatElement
        {
            if(!isset($joinTable->columns[$fk]))
                throw new CDbException(Yii::t('yii','The relation "{relation}" in active record class "{class}" is specified with an invalid foreign key "{key}". There is no such column in the table "{table}".',
-                   array('{class}'=>get_class($this->_parent->model), '{relation}'=>$relation->name, '{key}'=>$fk, '{table}'=>$joinTable->name)));
+                   array('{class}'=>($this->_parent->model->getClassName()), '{relation}'=>$relation->name, '{key}'=>$fk, '{table}'=>$joinTable->name)));

            if(isset($joinTable->foreignKeys[$fk]))
            {
@@ -1524,7 +1524,7 @@ class CStatElement

        if($joinCondition===array() || $map===array())
            throw new CDbException(Yii::t('yii','The relation "{relation}" in active record class "{class}" is specified with an incomplete foreign key. The foreign key must consist of columns referencing both joining tables.',
-               array('{class}'=>get_class($this->_parent->model), '{relation}'=>$relation->name)));
+               array('{class}'=>($this->_parent->model->getClassName()), '{relation}'=>$relation->name)));

        $records=$this->_parent->records;

diff --git a/framework/db/ar/CActiveRecord.php b/framework/db/ar/CActiveRecord.php
index cb419d1..eaa9afd 100644
--- a/framework/db/ar/CActiveRecord.php
+++ b/framework/db/ar/CActiveRecord.php
@@ -225,6 +225,10 @@ abstract class CActiveRecord extends CModel
        return parent::__call($name,$parameters);
    }

+   public function getClassName() {
+       return get_class($this);
+   }
+
    /**
     * Returns the related record(s).
     * This method will return the related record(s) of the current record.
@@ -246,9 +250,9 @@ abstract class CActiveRecord extends CModel
        $md=$this->getMetaData();
        if(!isset($md->relations[$name]))
            throw new CDbException(Yii::t('yii','{class} does not have relation "{name}".',
-               array('{class}'=>get_class($this), '{name}'=>$name)));
+               array('{class}'=>$this->getClassName(), '{name}'=>$name)));

-       Yii::trace('lazy loading '.get_class($this).'.'.$name,'system.db.ar.CActiveRecord');
+       Yii::trace('lazy loading '.$this->getClassName().'.'.$name,'system.db.ar.CActiveRecord');
        $relation=$md->relations[$name];
        if($this->getIsNewRecord() && !$refresh && ($relation instanceof CHasOneRelation || $relation instanceof CHasManyRelation))
            return $relation instanceof CHasOneRelation ? null : array();
@@ -379,15 +383,22 @@ abstract class CActiveRecord extends CModel
     */
    public static function model($className=__CLASS__)
    {
-       if(isset(self::$_models[$className]))
+       if(is_string($className) && isset(self::$_models[$className]))
            return self::$_models[$className];
-       else
-       {
-           $model=self::$_models[$className]=new $className(null);
-           $model->_md=new CActiveRecordMetaData($model);
-           $model->attachBehaviors($model->behaviors());
-           return $model;
+       else if(($className instanceof CActiveRecord) && isset(self::$_models[$className->getClassName()]))
+           return self::$_models[$className->getClassName()];
+        else {
+           if($className instanceof CActiveRecord) {
+               $model = $className;
+                $className = $model->getClassName();
+       } else {
+           $model=new $className(null);
        }
+       self::$_models[$className]=$model;
+       $model->_md=new CActiveRecordMetaData($model);
+       $model->attachBehaviors($model->behaviors());
+       return $model;
+        }
    }

    /**
@@ -399,7 +410,7 @@ abstract class CActiveRecord extends CModel
        if($this->_md!==null)
            return $this->_md;
        else
-           return $this->_md=self::model(get_class($this))->_md;
+           return $this->_md=self::model($this->getClassName())->_md;
    }

    /**
@@ -411,7 +422,7 @@ abstract class CActiveRecord extends CModel
     */
    public function refreshMetaData()
    {
-       $finder=self::model(get_class($this));
+       $finder=self::model($this->getClassName());
        $finder->_md=new CActiveRecordMetaData($finder);
        if($this!==$finder)
            $this->_md=$finder->_md;
@@ -425,7 +436,7 @@ abstract class CActiveRecord extends CModel
     */
    public function tableName()
    {
-       return get_class($this);
+       return $this->getClassName();
    }

    /**
@@ -1016,7 +1027,7 @@ abstract class CActiveRecord extends CModel
            throw new CDbException(Yii::t('yii','The active record cannot be inserted to database because it is not new.'));
        if($this->beforeSave())
        {
-           Yii::trace(get_class($this).'.insert()','system.db.ar.CActiveRecord');
+           Yii::trace($this->getClassName().'.insert()','system.db.ar.CActiveRecord');
            $builder=$this->getCommandBuilder();
            $table=$this->getMetaData()->tableSchema;
            $command=$builder->createInsertCommand($table,$this->getAttributes($attributes));
@@ -1064,7 +1075,7 @@ abstract class CActiveRecord extends CModel
            throw new CDbException(Yii::t('yii','The active record cannot be updated because it is new.'));
        if($this->beforeSave())
        {
-           Yii::trace(get_class($this).'.update()','system.db.ar.CActiveRecord');
+           Yii::trace($this->getClassName().'.update()','system.db.ar.CActiveRecord');
            if($this->_pk===null)
                $this->_pk=$this->getPrimaryKey();
            $this->updateByPk($this->getOldPrimaryKey(),$this->getAttributes($attributes));
@@ -1098,7 +1109,7 @@ abstract class CActiveRecord extends CModel
    {
        if(!$this->getIsNewRecord())
        {
-           Yii::trace(get_class($this).'.saveAttributes()','system.db.ar.CActiveRecord');
+           Yii::trace($this->getClassName().'.saveAttributes()','system.db.ar.CActiveRecord');
            $values=array();
            foreach($attributes as $name=>$value)
            {
@@ -1138,7 +1149,7 @@ abstract class CActiveRecord extends CModel
     */
    public function saveCounters($counters)
    {
-       Yii::trace(get_class($this).'.saveCounters()','system.db.ar.CActiveRecord');
+       Yii::trace($this->getClassName().'.saveCounters()','system.db.ar.CActiveRecord');
        $builder=$this->getCommandBuilder();
        $table=$this->getTableSchema();
        $criteria=$builder->createPkCriteria($table,$this->getOldPrimaryKey());
@@ -1162,7 +1173,7 @@ abstract class CActiveRecord extends CModel
    {
        if(!$this->getIsNewRecord())
        {
-           Yii::trace(get_class($this).'.delete()','system.db.ar.CActiveRecord');
+           Yii::trace($this->getClassName().'.delete()','system.db.ar.CActiveRecord');
            if($this->beforeDelete())
            {
                $result=$this->deleteByPk($this->getPrimaryKey())>0;
@@ -1182,7 +1193,7 @@ abstract class CActiveRecord extends CModel
     */
    public function refresh()
    {
-       Yii::trace(get_class($this).'.refresh()','system.db.ar.CActiveRecord');
+       Yii::trace($this->getClassName().'.refresh()','system.db.ar.CActiveRecord');
        if(($record=$this->findByPk($this->getPrimaryKey()))!==null)
        {
            $this->_attributes=array();
@@ -1397,7 +1408,7 @@ abstract class CActiveRecord extends CModel
     */
    public function find($condition='',$params=array())
    {
-       Yii::trace(get_class($this).'.find()','system.db.ar.CActiveRecord');
+       Yii::trace($this->getClassName().'.find()','system.db.ar.CActiveRecord');
        $criteria=$this->getCommandBuilder()->createCriteria($condition,$params);
        return $this->query($criteria);
    }
@@ -1411,7 +1422,7 @@ abstract class CActiveRecord extends CModel
     */
    public function findAll($condition='',$params=array())
    {
-       Yii::trace(get_class($this).'.findAll()','system.db.ar.CActiveRecord');
+       Yii::trace($this->getClassName().'.findAll()','system.db.ar.CActiveRecord');
        $criteria=$this->getCommandBuilder()->createCriteria($condition,$params);
        return $this->query($criteria,true);
    }
@@ -1426,7 +1437,7 @@ abstract class CActiveRecord extends CModel
     */
    public function findByPk($pk,$condition='',$params=array())
    {
-       Yii::trace(get_class($this).'.findByPk()','system.db.ar.CActiveRecord');
+       Yii::trace($this->getClassName().'.findByPk()','system.db.ar.CActiveRecord');
        $prefix=$this->getTableAlias(true).'.';
        $criteria=$this->getCommandBuilder()->createPkCriteria($this->getTableSchema(),$pk,$condition,$params,$prefix);
        return $this->query($criteria);
@@ -1442,7 +1453,7 @@ abstract class CActiveRecord extends CModel
     */
    public function findAllByPk($pk,$condition='',$params=array())
    {
-       Yii::trace(get_class($this).'.findAllByPk()','system.db.ar.CActiveRecord');
+       Yii::trace($this->getClassName().'.findAllByPk()','system.db.ar.CActiveRecord');
        $prefix=$this->getTableAlias(true).'.';
        $criteria=$this->getCommandBuilder()->createPkCriteria($this->getTableSchema(),$pk,$condition,$params,$prefix);
        return $this->query($criteria,true);
@@ -1459,7 +1470,7 @@ abstract class CActiveRecord extends CModel
     */
    public function findByAttributes($attributes,$condition='',$params=array())
    {
-       Yii::trace(get_class($this).'.findByAttributes()','system.db.ar.CActiveRecord');
+       Yii::trace($this->getClassName().'.findByAttributes()','system.db.ar.CActiveRecord');
        $prefix=$this->getTableAlias(true).'.';
        $criteria=$this->getCommandBuilder()->createColumnCriteria($this->getTableSchema(),$attributes,$condition,$params,$prefix);
        return $this->query($criteria);
@@ -1476,7 +1487,7 @@ abstract class CActiveRecord extends CModel
     */
    public function findAllByAttributes($attributes,$condition='',$params=array())
    {
-       Yii::trace(get_class($this).'.findAllByAttributes()','system.db.ar.CActiveRecord');
+       Yii::trace($this->getClassName().'.findAllByAttributes()','system.db.ar.CActiveRecord');
        $prefix=$this->getTableAlias(true).'.';
        $criteria=$this->getCommandBuilder()->createColumnCriteria($this->getTableSchema(),$attributes,$condition,$params,$prefix);
        return $this->query($criteria,true);
@@ -1490,7 +1501,7 @@ abstract class CActiveRecord extends CModel
     */
    public function findBySql($sql,$params=array())
    {
-       Yii::trace(get_class($this).'.findBySql()','system.db.ar.CActiveRecord');
+       Yii::trace($this->getClassName().'.findBySql()','system.db.ar.CActiveRecord');
        $this->beforeFind();
        if(($criteria=$this->getDbCriteria(false))!==null && !empty($criteria->with))
        {
@@ -1513,7 +1524,7 @@ abstract class CActiveRecord extends CModel
     */
    public function findAllBySql($sql,$params=array())
    {
-       Yii::trace(get_class($this).'.findAllBySql()','system.db.ar.CActiveRecord');
+       Yii::trace($this->getClassName().'.findAllBySql()','system.db.ar.CActiveRecord');
        $this->beforeFind();
        if(($criteria=$this->getDbCriteria(false))!==null && !empty($criteria->with))
        {
@@ -1537,7 +1548,7 @@ abstract class CActiveRecord extends CModel
     */
    public function count($condition='',$params=array())
    {
-       Yii::trace(get_class($this).'.count()','system.db.ar.CActiveRecord');
+       Yii::trace($this->getClassName().'.count()','system.db.ar.CActiveRecord');
        $builder=$this->getCommandBuilder();
        $criteria=$builder->createCriteria($condition,$params);
        $this->applyScopes($criteria);
@@ -1563,7 +1574,7 @@ abstract class CActiveRecord extends CModel
     */
    public function countByAttributes($attributes,$condition='',$params=array())
    {
-       Yii::trace(get_class($this).'.countByAttributes()','system.db.ar.CActiveRecord');
+       Yii::trace($this->getClassName().'.countByAttributes()','system.db.ar.CActiveRecord');
        $prefix=$this->getTableAlias(true).'.';
        $builder=$this->getCommandBuilder();
        $criteria=$builder->createColumnCriteria($this->getTableSchema(),$attributes,$condition,$params,$prefix);
@@ -1588,7 +1599,7 @@ abstract class CActiveRecord extends CModel
     */
    public function countBySql($sql,$params=array())
    {
-       Yii::trace(get_class($this).'.countBySql()','system.db.ar.CActiveRecord');
+       Yii::trace($this->getClassName().'.countBySql()','system.db.ar.CActiveRecord');
        return $this->getCommandBuilder()->createSqlCommand($sql,$params)->queryScalar();
    }

@@ -1601,7 +1612,7 @@ abstract class CActiveRecord extends CModel
     */
    public function exists($condition='',$params=array())
    {
-       Yii::trace(get_class($this).'.exists()','system.db.ar.CActiveRecord');
+       Yii::trace($this->getClassName().'.exists()','system.db.ar.CActiveRecord');
        $builder=$this->getCommandBuilder();
        $criteria=$builder->createCriteria($condition,$params);
        $table=$this->getTableSchema();
@@ -1683,7 +1694,7 @@ abstract class CActiveRecord extends CModel
     */
    public function updateByPk($pk,$attributes,$condition='',$params=array())
    {
-       Yii::trace(get_class($this).'.updateByPk()','system.db.ar.CActiveRecord');
+       Yii::trace($this->getClassName().'.updateByPk()','system.db.ar.CActiveRecord');
        $builder=$this->getCommandBuilder();
        $table=$this->getTableSchema();
        $criteria=$builder->createPkCriteria($table,$pk,$condition,$params);
@@ -1702,7 +1713,7 @@ abstract class CActiveRecord extends CModel
     */
    public function updateAll($attributes,$condition='',$params=array())
    {
-       Yii::trace(get_class($this).'.updateAll()','system.db.ar.CActiveRecord');
+       Yii::trace($this->getClassName().'.updateAll()','system.db.ar.CActiveRecord');
        $builder=$this->getCommandBuilder();
        $criteria=$builder->createCriteria($condition,$params);
        $command=$builder->createUpdateCommand($this->getTableSchema(),$attributes,$criteria);
@@ -1721,7 +1732,7 @@ abstract class CActiveRecord extends CModel
     */
    public function updateCounters($counters,$condition='',$params=array())
    {
-       Yii::trace(get_class($this).'.updateCounters()','system.db.ar.CActiveRecord');
+       Yii::trace($this->getClassName().'.updateCounters()','system.db.ar.CActiveRecord');
        $builder=$this->getCommandBuilder();
        $criteria=$builder->createCriteria($condition,$params);
        $command=$builder->createUpdateCounterCommand($this->getTableSchema(),$counters,$criteria);
@@ -1738,7 +1749,7 @@ abstract class CActiveRecord extends CModel
     */
    public function deleteByPk($pk,$condition='',$params=array())
    {
-       Yii::trace(get_class($this).'.deleteByPk()','system.db.ar.CActiveRecord');
+       Yii::trace($this->getClassName().'.deleteByPk()','system.db.ar.CActiveRecord');
        $builder=$this->getCommandBuilder();
        $criteria=$builder->createPkCriteria($this->getTableSchema(),$pk,$condition,$params);
        $command=$builder->createDeleteCommand($this->getTableSchema(),$criteria);
@@ -1754,7 +1765,7 @@ abstract class CActiveRecord extends CModel
     */
    public function deleteAll($condition='',$params=array())
    {
-       Yii::trace(get_class($this).'.deleteAll()','system.db.ar.CActiveRecord');
+       Yii::trace($this->getClassName().'.deleteAll()','system.db.ar.CActiveRecord');
        $builder=$this->getCommandBuilder();
        $criteria=$builder->createCriteria($condition,$params);
        $command=$builder->createDeleteCommand($this->getTableSchema(),$criteria);
@@ -1772,7 +1783,7 @@ abstract class CActiveRecord extends CModel
     */
    public function deleteAllByAttributes($attributes,$condition='',$params=array())
    {
-       Yii::trace(get_class($this).'.deleteAllByAttributes()','system.db.ar.CActiveRecord');
+       Yii::trace($this->getClassName().'.deleteAllByAttributes()','system.db.ar.CActiveRecord');
        $builder=$this->getCommandBuilder();
        $table=$this->getTableSchema();
        $criteria=$builder->createColumnCriteria($table,$attributes,$condition,$params);
@@ -1850,7 +1861,7 @@ abstract class CActiveRecord extends CModel
     */
    protected function instantiate($attributes)
    {
-       $class=get_class($this);
+       $class=$this->getClassName();
        $model=new $class(null);
        return $model;
    }
diff --git a/framework/web/CActiveDataProvider.php b/framework/web/CActiveDataProvider.php
index 87233ea..a63d334 100644
--- a/framework/web/CActiveDataProvider.php
+++ b/framework/web/CActiveDataProvider.php
@@ -75,7 +75,7 @@ class CActiveDataProvider extends CDataProvider
        }
        elseif($modelClass instanceof CActiveRecord)
        {
-           $this->modelClass=get_class($modelClass);
+           $this->modelClass=($modelClass->getClassName());
            $this->model=$modelClass;
        }
        $this->setId($this->modelClass);
diff --git a/framework/web/form/CForm.php b/framework/web/form/CForm.php
index 7b97893..8deb336 100644
--- a/framework/web/form/CForm.php
+++ b/framework/web/form/CForm.php
@@ -216,7 +216,7 @@ class CForm extends CFormElement implements ArrayAccess
    {
        if($this->_model!==null)
        {
-           $class=get_class($this->_model);
+           $class=($this->_model->getClassName());
            if(strcasecmp($this->getRoot()->method,'get'))
            {
                if(isset($_POST[$class]))
diff --git a/framework/web/widgets/CActiveForm.php b/framework/web/widgets/CActiveForm.php
index e0ddb68..c235122 100644
--- a/framework/web/widgets/CActiveForm.php
+++ b/framework/web/widgets/CActiveForm.php
@@ -434,7 +434,7 @@ class CActiveForm extends CWidget
            'id'=>$id,
            'inputID'=>$inputID,
            'errorID'=>$htmlOptions['id'],
-           'model'=>get_class($model),
+           'model'=>($model->getClassName()),
            'name'=>$attribute,
            'enableAjaxValidation'=>$enableAjaxValidation,
        );
@@ -830,8 +830,8 @@ class CActiveForm extends CWidget
            $models=array($models);
        foreach($models as $model)
        {
-           if($loadInput && isset($_POST[get_class($model)]))
-               $model->attributes=$_POST[get_class($model)];
+           if($loadInput && isset($_POST[$model->getClassName()]))
+               $model->attributes=$_POST[$model->getClassName()];
            $model->validate($attributes);
            foreach($model->getErrors() as $attribute=>$errors)
                $result[CHtml::activeId($model,$attribute)]=$errors;
@@ -858,8 +858,8 @@ class CActiveForm extends CWidget
            $models=array($models);
        foreach($models as $i=>$model)
        {
-           if($loadInput && isset($_POST[get_class($model)][$i]))
-               $model->attributes=$_POST[get_class($model)][$i];
+           if($loadInput && isset($_POST[$model->getClassName()][$i]))
+               $model->attributes=$_POST[$model->getClassName()][$i];
            $model->validate($attributes);
            foreach($model->getErrors() as $attribute=>$errors)
                $result[CHtml::activeId($model,'['.$i.']'.$attribute)]=$errors;
diff --git a/framework/web/widgets/CInputWidget.php b/framework/web/widgets/CInputWidget.php
index 2148084..511e3f3 100644
--- a/framework/web/widgets/CInputWidget.php
+++ b/framework/web/widgets/CInputWidget.php
@@ -57,7 +57,7 @@ abstract class CInputWidget extends CWidget
        elseif($this->hasModel())
            $name=CHtml::activeName($this->model,$this->attribute);
        else
-           throw new CException(Yii::t('yii','{class} must specify "model" and "attribute" or "name" property values.',array('{class}'=>get_class($this))));
+           throw new CException(Yii::t('yii','{class} must specify "model" and "attribute" or "name" property values.',array('{class}'=>($this->getClassName()))));

        if(($id=$this->getId(false))===null)
        {
diff --git a/framework/zii/widgets/jui/CJuiInputWidget.php b/framework/zii/widgets/jui/CJuiInputWidget.php
index 01f756a..53bc920 100644
--- a/framework/zii/widgets/jui/CJuiInputWidget.php
+++ b/framework/zii/widgets/jui/CJuiInputWidget.php
@@ -49,7 +49,7 @@ abstract class CJuiInputWidget extends CJuiWidget
        elseif($this->hasModel())
            $name=CHtml::activeName($this->model,$this->attribute);
        else
-           throw new CException(Yii::t('zii','{class} must specify "model" and "attribute" or "name" property values.',array('{class}'=>get_class($this))));
+           throw new CException(Yii::t('zii','{class} must specify "model" and "attribute" or "name" property values.',array('{class}'=>$this->getClassName())));

        if(($id=$this->getId(false))===null)
        {
@klimov-paul

This comment has been minimized.

Show comment
Hide comment
@klimov-paul

klimov-paul Apr 4, 2013

Member

@dinhtrung, instead of creating patches please create a Pull Request.
To do so please read workflow for Yiicontributors.

Member

klimov-paul commented Apr 4, 2013

@dinhtrung, instead of creating patches please create a Pull Request.
To do so please read workflow for Yiicontributors.

@dinhtrung

This comment has been minimized.

Show comment
Hide comment
@dinhtrung

dinhtrung Apr 5, 2013

Thanks, I use github long time ago but hadn't create any Pull Request. Actually, GIT is quite new for me. :)

Thanks, I use github long time ago but hadn't create any Pull Request. Actually, GIT is quite new for me. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment