Implemented Improvement - Base Class Suffix on Model Generator

I’ve implemented a patch to allow for generated models to have a suffix. This allows for easy regeneration of models. I’ve uploaded the patch here:

http://code.google.com/p/yii/issues/detail?id=2064

Here’s a screenshot of the generation screen

1300

generation.png

Filled out like this it will generate a set of classes such as

application.models.db.base.CustomerBase

application.models.db.base.CustomerAddressBase

You’d then extend/inherit from these to create

application.models.db.Customer

application.models.db.CustomerAddress

If you update columns/headers in your customer table you can regenerate the code in the base class without erasing your custom code in the actual model class.

I’ve even done a separate inheritance from the base class for searching as follows:


//generated, placed in application.models.db.base

class ProductBase extends CActiveRecord

{

 ...

}


//inherited, placed in application.models.db

class DistrictBase extends CActiveRecord

{


	public static function model($className=__CLASS__)

	{

		return parent::model($className);

	}


//other custom functions, or overrides such as

	public function attributeLabels()

	{

		return CMap::mergeArray(array('customColumn'=>'Custom Column'),parent::attributeLabels() );

	}


}


//inherited, placed in application.models.db.search

class ProductSearch extends ProductBase

{	

	public static function model($className=__CLASS__)

	{

		return parent::model($className);

	}

	

	public function __construct($scenario='search')

	{

		parent::__construct($scenario);		

	}

	

 	protected function afterConstruct()

 	{

                parent::afterConstruct();

                $this->unsetAttributes();	

        }


// other custom search functions or columns that aren't really on the model

}

With the ProductSearch above you can then use the following in your controllers:


	public function actionIndex()

	{

		$model=new ProductSearch();				

		if(isset($_GET['ProductSearch']))

			$model->attributes=$_GET['ProductSearch'];		

		

		$this->render('index',array(	

			'model'=>$model,

		));

	}