I want to use this as my unique key

Hey guys,

I have a table which takes in a word from the user. I want this to be a unique key and if it is submitted again, I want it to add 1 to the count of the previous instance of the key.

In my MySQL database/table I have sent the field to unique key. Obviously this means there can only be one.

Now when I try add the same word (through a submitted form) I get:

CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry ‘wordtest’ for key ‘uniqueword’

Whats the best way to go about this problem? (I want to add 1 to the count instead of getting that error)




if(Model::model()->exists('wordColumn=:userInput', array(':userInput'=>$input)))

   Model::model()->updateCounters(array('count'=>+1), 'wordColumn=:userInput', array(':userInput'=>$input));

else

{

   $newWord = new Model;

   ...

}



Thanks for your reply. It makes sense, but I am not sure how to go about using that info. First off, where does $input come from? My user is inputting through a form.

And where do you suggest I put that code? In save()?

I added in the code in my ActionCreate() method in my entry controller.


public function actionCreate() {

        $model = new Entry;


            if(isset($_POST['Entry'])) {

                $model->attributes=$_POST['Entry'];


           if(Entry::model()->find('content=:userInput', array(':userInput'=>$model->$content))) {

          Entry::model()->updateCounters(array('voteCount'=>+1), 'content=:userInput', array(':userInput'=>$model->$content));

          $this->redirect(array('show','id'=>$model->id));

            } else if($model->save()){

                    $this->redirect(array('show','id'=>$model->id));

                }

            }

        $this->render('create',array('model'=>$model));

    }

I am getting an error:

I guess




$model->$content



should be




$model->content



Thanks! I didn’t see that. Argh, spent ages messing with code. Thanks!