Dinamic rules() is not work.

Model:



class Advertisement extends CActiveRecord


{


	public $entityID;


	private $_rules = array();





	public static function model($className=__CLASS__)


	{


		return parent::model($className);


	}





	public function rules()


	{


		return $this->_rules;


	}





	public function addRule($rule)


	{


		$this->_rules[] = $rule;


	}


}


Controller code:



public function actionCreate()


{


	$a = new Advertisement;


	$a->addRule(array('entityID', 'numerical', 'integerOnly' => true));


	$a->entityID = 'string';


	var_dump($a->validate());


}


Returns bool(true). Why?

Dynamic rules is supported in the upcoming 1.1. In 1.0, rules() is only read once when you first access the model class.

Thanks. Don't forget dinamic relations() too. It needed for write complex AR behaviors like Entity-Attribute-Value behavior for CakePHP and Ruby on Rails.

Today i hack ;) ‘columns’ property of CActiveRecordMetaData to make rules() and relations() work. But it’s not good method, native support for this operations should be fine.

Yii the BEST!

hello,

is there an easy work-around to fix this? (or better yet, when is 1.1 coming out? :) )

depending on my ID, i'd need different rules :confused:

thanks,

–iM

Maybe scenarios can help? If not, you can use static property in you AR class as easy workaround. Do this and dinamic rules() will work.

For example:



class Advertisement extends CActiveRecord


{


   private static $_rules;


For dinamic relations() you can add method addRelation(…) to ARModel with some code from ActiveRecordMetaData.

Example:



$entityMd = Entity::model()->getMetaData();


...


$entityMd->relations[$name] = new CXXXRelation(


	$name, 'Entity'. ucfirst($type), 'entityID',


	compact('with', 'on', 'alias')


);


...