Mutiple Column uniqueness Rules


(Muhammad Muzammil) #1

I have a table like this:

(id,A,B,C,D). combination of (A+B+C+D) values is unique, how can i make a rule to validate this.


(Oligalma) #2

Suppose that you have the table "Message" with two primary columns called "id" and "language". Then you have to add this to your model:


	public function rules()

	{

		return array(

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

	            'condition'=>'language=:language',

	            'params'=>array(

	                ':language'=>$this->language

	            )

	        ), 'on' => 'update'),

	        array('id', 'checkMessage', 'on' => 'insert')

		);

	}

	

	public function checkMessage($attribute,$params)

	{

		$model = Message::model()->find('id = ? AND language = ?', array($this->id, $this->language));

		if($model != null)

			$this->addError('id','This id and language already exist');	

	}


(Oligalma) #3

read this topic, because it is more effective than the method I showed you in the previous reply:

http://www.yiiframework.com/wiki/782/how-to-validate-the-uniqueness-of-multiple-columns