Raiseevent Based On Limit

When someone is creating a new “account” record in the database, I want to first check the “limit” column in the “domain” table to see what the “limit” is and then compare that to the number of “account”'s already created. If the limit has been reached, I want to raise an event, redirect the user to the “index” page of that model and show the event stating that the limit has been reached and that they cannot create anymore “account”'s for that particular “domain”. How can this be achieved?

I’d go with custom validation rules.

Thank you for the suggestion. Can you show me a URL of where I can find an example of how to do this? I have Googled and Googled and cannot find anything close enough. Thank you!

for example here

http://www.yiiframework.com/doc/guide/1.1/en/form.model#declaring-validation-rules

and here

http://www.yiiframework.com/wiki/168/create-your-own-validation-rule/

I don’t think these validators will help me. All of the examples I found in conjunction with what you provided me look like they require you to have an attribute to attach an error to, which is not what I am looking for. I simply need to check how many accounts currently exist, what the limit is, and if the limit has reached do not allow the new account to be created while at the same time displaying an error message on the account/index page stating that the domain is over its limit. I suppose I could do a check before the form is submitted somehow. The biggest problem is how to use the error/event system to display the error message.

Thank you.

Okay, I figured it out:




	public function beforeValidate() {

		$domain = Domain::model()->findByAttributes(array('domain_id'=>$this->domain_id));


		if ($domain->account == 0) {

			return parent::beforeValidate();

		} else {

			$accounts = count(self::model()->findAllByAttributes(array('domain_id'=>$this->domain_id)));


			if ($accounts < $domain->account) {

				return parent::beforeValidate();

			} else {

				$this->addError('account','Account limit has been reached for this domain!');

			}

		}

	}



Okay, it’s not entirely working.




	public function beforeValidate() {

		if ($this->isNewRecord) {

			$domain = Domain::model()->findByAttributes(array('domain_id'=>$this->domain_id));


			if ($domain->account == 0) {

				return parent::beforeValidate();

			} else {

				$accounts = count(self::model()->findAllByAttributes(array('domain_id'=>$this->domain_id)));


				if ($accounts < $domain->account) {

					return parent::beforeValidate();

				} else {

					$this->addError('account','Account limit has been reached for this domain!');

				}

			}

		}

	}



It triggers properly if it is a new record however, if it is not it doesn’t allow the update of the existing account to be processed. What am I missing here? It tries to insert a new record if I add an else to the if isNewRecord condition with a return parent::beforeValidate();

Nevermind, it was my fault! Everything is working now!