Activerecord Enhancement

In general working of any AR-class which extends CActiveRecord looks in this way. During instantiation AR-class calls CactiveRecord::getMetaData() method. CactiveRecordMetaData class is used inside this method and this class makes query request to database to collect some needed information about needed table. It can be the primary keys and information about fields of table. For highload sites it is redundant to make an aditional query to database.

I propose to create way to escape first additional query to database if php-developer knows exactly what properties of the table are used. If developer don’t needs first additional query to database he could reload special method of CactiveRecord in his class to return known information about table.


class myAR extends CActiveRecord

{

/* Any needed methods */


	public function getActiveRecordMetaData()

	{

		return array(

			'primary_key'=>'id',

			'fields'=array(

				'username',

				'password',

				'email'

			)

		);

	}

}

I don’t know exactly what information CactiveRecordMetaData in Yii1.1.14 consists of and what information is needed for CactiveRecord to makes his job. Thats why in example above my function returns dummy information.

In core CActiveRecord class method getActiveRecordMetaData() must make a query to database to collect information about table.

So in CActiveRecord it is needed to make getActiveRecordMetaData() method which makes all job for getting information about table. In expanding classes developers can recreate this method for escaping redundant query to database if they knows exactly what is in their table.

It can be cached easily with one line in connection config, "db" component, enableSchemaCache property.

As I see in Yii2 it is already done. I am just not working with Yii2 yet and not searching it deeply. As I understand it is mabe by the another table with cached data in database. So there is one additional connection also.

It’s done in both 1.1 and 2.0. Backend for storing cache is determined by what you’ve set in “cache” component. It can be files, database, memcached, apc, zend cache, redis etc.