Yii CMS
I have a unique composite key in the table ‘category’, website_id + link, and Yii does not support this keys, so I wrote my own method
a website has many links, each unique;
2 or more websites may have the same link; because the link may not be absolute;
from a array of links, I extract one link at a time, and if the category pattern fits, I want to store the url;
preg_match('/\/popular\/(.*?)\/1\.html/ims', $matches_website_url[1], $matches_url);
if(count($matches_url) > 0 &&
$this->avoid_duplicate_category($website['id'], $matches_url[1]) )
{
$category = new Category();
$category->website_id = $website['id'];
$category->link = $matches_url[0];
$category->name = $matches_url[1];
$category->urls = 0;
$category->update = time();
$category->save();
}
and the method
private function avoid_duplicate_category($website_id,$link)
{
$query_category = Yii::app()->db->createCommand("select * from `category` where `website_id`='.$website_id.' and `link`='.$link.';");
$results = $query_category->queryAll();
if(count($results)>0)return false;
else return true;
}
and the error returned :
CDbException
CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'Cute' for key 'name'. The SQL statement executed was: INSERT INTO `category` (`website_id`, `link`, `name`, `urls`, `update`) VALUES (:yp0, :yp1, :yp2, :yp3, :yp4)
I believe you should be able to allow Yii to handle the composite key by adding the following to your model:
EDIT,
Sorry, misread your question! For unique keys (not primary) you could try these extensions:
http://www.yiiframework.com/extension/composite-unique-key-validatable/
http://www.yiiframework.com/extension/unique-index-validator