How To Set Unique Checking In The Model?

Hi,

I have a database table with 2 column: ‘racer_id’, and ‘turn’. On the ‘turn’ there is a unique index.

But I don’t know exactly, how I can validate this situation in my code.

When I try to insert a new row with the same ‘turn’ I get this exception:

“SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry ‘3’ for key ‘turn’. The SQL statement executed was: INSERT INTO tbl_bets_round_wins (racer_id, turn) VALUES (:yp0, :yp1)”

But the Yii doesn’t handle this problem at AjaxValidation:

Controller:




        public function actionCreate()

	{

               Yii::app()->language='hu';

               

               //try{

                    $model=new BetsRoundWins;


                     // Uncomment the following line if AJAX validation is needed

                     $this->performAjaxValidation($model);


                     if(isset($_POST['BetsRoundWins']))

                     {

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

                             if($model->save())

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

                     }  

                     

                     $this->render('create',array(

                            'model'=>$model,

                    ));

                     

                //}catch (CDbException $e){

                //    if($e->getCode()===23000){

                        //You can have nother error handling                        

                //        header("HTTP/1.0 400 Relation Restriction");

                //    }else{

                //        throw $e;

                //    }

                //}	

	}


        protected function performAjaxValidation($model)

	{

		if(isset($_POST['ajax']) && $_POST['ajax']==='bets-round-wins-form')

		{

			echo CActiveForm::validate($model);

			Yii::app()->end();

		}

	}




And I modified my model’s rule method:




        public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('racer_id', 'required'),

                        array('racer_id', 'length', 'max'=>10),

			array('turn', 'numerical', 'integerOnly'=>true),			

                        array('turn', 'unique', 'className'=> 'BetsRoundWins'),

			// The following rule is used by search().

			// @todo Please remove those attributes that should not be searched.

			array('racer_id, turn', 'safe', 'on'=>'search'),

		);

	}



And my problem is that, the ajaxValidaton doesn’t work… It doesn’t check the unique.

I don’t know, why doesn’t work the ajaxValidation(). How can I handle this in Yii?

Hi,

First you can check the db type on racer_id and defind the indexes

second thing if you want to add the ajaxvalidation please see

Thanks for the link, but I don’t understand exactly how I can check the db type on racer_id, and why I have to check it? :)

Hi just remove the unique key of turn because it’s alredy inserted in db that’s why it’s getting the error

Yes, but for the app is important to store unique turns. I will try the ajaxValidation. I hope it will work.

Thanks for the help :)

Hi,

if you want check unique you want to addError after form submit

please see it

i hope it’s some help.

Thanks for the lots of help. :)