How to set alias in Active Record class?

Hi,

I have old table that created using Grails framework (we still use it).

In Grails ORM, if we type firstName it will generate first_name in the mysql table.

We want to maintain the compatibility with that, so is it possible to create an alias in Active Record Class and how to do that ?

for example

in Yii AR i want to use firstName instead of first_name but it will refer to first_name in mysql table.

I need to do that on table as well … because Grails ORM convert camel case class into table name with underscore (eg: in ORM : CustomerOrder but in mysql table it becomes customer_order)

is it possible to make alias ? I would love to use camel case style than using under score (_)

thanks

You can ovewrite CActiveRecord and CActiveRecordMetaDate, which contains exactly what you want - array columns.

can I have some examples ? do you mean override by extends CActiveRecord right ?

If I have to extends, which folder should I place it ? Can I do that without modify yii framework it self … if just extends I would do that :)

Yes. It’s a typo.

You can make it as an extension and place it in extensions folder or your application.

At first override CActiveRecord like this:




abstract class CMyActiveRecord extends CActiveRecord

{

public static function model($className=__CLASS__)

	{

		if(isset(self::$_models[$className]))

			return self::$_models[$className];

		else

		{

			$model=self::$_models[$className]=new $className(null);

			$model->attachBehaviors($model->behaviors());

			$model->_md=new CMyActiveRecordMetaData($model);

			return $model;

		}

	}

}



Then override CActiveRecordMetaData:




class CMyActiveRecordMetaData extends CActiveRecordMetaData 

{

public function __construct($model)

	{

		$this->_model=$model;


		$tableName=$model->tableName();

		if(($table=$model->getDbConnection()->getSchema()->getTable($tableName))===null)

			throw new CDbException(Yii::t('yii','The table "{table}" for active record class "{class}" cannot be found in the database.',

				array('{class}'=>get_class($model),'{table}'=>$tableName)));

		if($table->primaryKey===null)

			$table->primaryKey=$model->primaryKey();

		$this->tableSchema=$table;

		$this->columns=$table->columns;

//==================================================================

//! place something for your case to match columnName to column_name

//==================================================================

		foreach($table->columns as $name=>$column)

		{

			if(!$column->isPrimaryKey && $column->defaultValue!==null)

				$this->attributeDefaults[$name]=$column->defaultValue;

		}


		foreach($model->relations() as $name=>$config)

		{

			if(isset($config[0],$config[1],$config[2]))  // relation class, AR class, FK

				$this->relations[$name]=new $config[0]($name,$config[1],$config[2],array_slice($config,3));

			else

				throw new CDbException(Yii::t('yii','Active record "{class}" has an invalid configuration for relation "{relation}". It must specify the relation type, the related active record class and the foreign key.',

					array('{class}'=>get_class($model),'{relation}'=>$name)));

		}

	}

}



But I’m not sure it’s the correct way and how all this stuff will work with relations

you should never really override classes… that can lead to problems when the core changes. Instead extend classes. I would recommend extending CActiveRecord, and then only override the magic __get() __set() __isset() and __unset() methods.

I think, that’s what he tried to say (he just forgot the “My” in CActiveRecordMetaData ). At least that’s what i usually want to say if i use this term. But i’m not a native speaker … :)

Thanks. Fixed