php - How to limit maximum number of items in category in yii? -
i have page model. pages nested - each page can belong another. because of space limit need force maximum number of "main pages"(pages without parent page). question best place check limit? in beforesave
? or in custom validation rule? or elsewhere?
here example of custom rule class page, provided parentid
attribute nullable foreign key parent page, sets null
if page has no parent, i.e. main page.
class page extends cactiverecord { const mainpages_limit = 10; public function rules() { return array( ... array('parentid', 'maynewmainpagebecreated', 'on'=>'insert'), ... ); } // custom rule validator public function maynewmainpagebecreated($attribute, $params) { $count = page::model()->count("parentid null"); if ($count >= self::mainpages_limit) { $this->adderror($attribute, "can't create more main pages"); } } }
Comments
Post a Comment