Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 复合唯一键的Yii查询有错误_Php_Sql_Yii - Fatal编程技术网

Php 复合唯一键的Yii查询有错误

Php 复合唯一键的Yii查询有错误,php,sql,yii,Php,Sql,Yii,Yii CMS 我在表'category',website_id+link中有一个唯一的复合键,Yii不支持这个键,所以我编写了自己的方法 一个网站有很多链接,每一个都是独一无二的; 2个或多个网站可能有相同的链接;因为联系可能不是绝对的 从链接数组中,我一次提取一个链接,如果类别模式合适,我想存储url preg_match('/\/popular\/(.*?)\/1\.html/ims', $matches_website_url[1], $matches_url); if(count($

Yii CMS

我在表'category',website_id+link中有一个唯一的复合键,Yii不支持这个键,所以我编写了自己的方法

一个网站有很多链接,每一个都是独一无二的; 2个或多个网站可能有相同的链接;因为联系可能不是绝对的

从链接数组中,我一次提取一个链接,如果类别模式合适,我想存储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();
}
方法呢

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;

}
然后返回错误:

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)

我相信您应该能够通过向模型中添加以下内容来允许Yii处理复合密钥:

public function primaryKey()
{
    return array('website_id','link');
}
编辑

对不起,误读了你的问题!对于唯一密钥(非主密钥),您可以尝试以下扩展:

答案如下:

private function avoid_duplicate_category($website_id,$link)
{
    $query = "select * from `category` where `website_id`=:website_id and `link`=:link;";

    $query_category = Yii::app()->db->createCommand($query);

    $query_category->bindParam( ':website_id', $website_id);

    $query_category->bindParam( ':link', $link);

    $results = $query_category->queryAll();

    if(count($results)>0)return false;
    else return true;

}

这不是Pk,是一个非IQE键;我想我可以重命名这个方法,我会接受它;我希望;等我试试看;公共函数规则(){//注意:您应该只为那些//将接收用户输入的属性定义规则。返回数组(…数组('website_id,link','primaryKey'),…);}公共函数primaryKey(){返回数组('website_id','link');}是的,对不起,误读了你的问题!我已经添加了一些扩展的链接,这些扩展可能会对您有所帮助,但是为什么我的方法不起作用呢?为什么即使数据是重复的,也返回true?yii为我推荐了一些复杂的东西