Allow Once Per Category

Hello guys, i’m currently involved in a project in which the user should be allowed to create a row in a table.

The idea is that there are 14 different categories the user has to choose between when creating the row. The problem however is how to allow a user to create one, and only one, row per category.

Your help will be very much appreciated :)

If you can brief the question it would be great for other readers.

I needed the same functionality in the past and written an extension for it: http://www.yiiframew…valuevalidator/

But, there are other ways to achieve this. Basically, a logical way would be to enforce this limitation in the validation phase of a model being submitted/updated. for this for see the relevant page in the guide.

I see now that it was kind of a blurry explanation of my problem, so i’m gonna try again:

I have a table that is set up kind of like this:

id INT

create_user_id INT

subject INT

category INT

content TEXT

The thing i want to achieve is that a certain user with the permission to create a row in the table will be able to create a row for each category for each subject. There are 14 different categories in my project, so a user will be able to create 14 different rows with the value of the subject being 1 (for example), one for each category. In the next subject, he will again be able to create 14 different rows, one for each category, and so on.

I hope that helped you understand what i want to create :)

I think this logic should be in the model. I would have went for a validator method or class. The logic would be embedded in this validator.

Also, this can and should be forced in the DB level using a unique constraint on both columns (or maybe 3 columns, including user id? I’m not sure - depending on your requirements)

I’ll try that. Thanks for your help :)

Dear Friend

kindly check whether the following is helpful.

Using a custom validator method:

In Model.

public function rules()

	{       //along with other rules.


		return array(

			array('catagory', 'checkRecord'),




public function checkRecord($attribute,$params)


             if(MyModel::model()->exists("subject=:subject AND catagory=:catagory AND create_user_id=:create_user_id",array(':subject'=>$this->subject,':catagory'=>$this->catagory,':create_user_id'=>Yii::app()->user->id)))

             $this->addError($attribute,"You have already created a node on ".$attribute." in ".$this->subject);


Using unique validator.

public function rules()

	{       //along with other rules.


		return array(

			array('catagory', 'unique','criteria'=>array(

                          'condition'=>"subject=:subject AND create_user_id=:create_user_id",






I could not test it in my localhost.

If there is any shortcomings ,kindly bear with me.


I’m not able to test it my self either since i’m away from my workstation for about two weeks, but i’m sure this will help me a great deal.

However, i’m not sure if you mean i should choose one of the 2 options (I think that, but not sure) or if you are saying i should write this in two different models (and if so, what models?). For me, the natural thing is to choose between the 2, but then again i’m not that good at programming/yii (yet)

Either way, thanks for taking the time to help me with this, and even write a great deal of the code. Really appreciated :)

You can also add to the user experience by preventing the user from selecting a category that already has a row defined.

Its like bringing a kid to a toy store and letting him choose any toy but only once at the cash register do you tell him he cannot have all the toys only a few. :)