I am trying to implement unique records in the database table. The fields product_id and image_name should be unique as a combination - how do I specify a combination of fields to check on?
I also need to pass back an error message to the view (maybe using $model->addError)?
public function rules()
{
return array(
array('image_name', 'checkCombination'),
);
}
public function checkCombination($attribute,$params)
{
if(!$this->hasErrors()) // we only want to authenticate when no input errors
{
$product = Product::model()->find('product_id = ? AND image_name = ?', array($this->product_id, $this->image_name));
if($product)
$this->addError('image_name','Too Bad.');
}
}
cheers mbi - that kind of half works - it does not add the duplicate entry to the database but the error is not displayed on the page. I have put <?php echo CHtml::errorSummary($model); ?> on the page.