gpt4 book ai didi

Codeigniter MY_Model 复合主键

转载 作者:行者123 更新时间:2023-12-03 06:14:29 26 4
gpt4 key购买 nike

我正在使用基类 MY_Model 来访问数据库,其中包含所有 CRUD 方法。我使用的模型是 Jens Segers 的 MY_Model:

https://github.com/jenssegers/CodeIgniter-My-Model

现在,我的数据库中有一个表,其中包含复合主键(article_id 和 tag_id)。通常,当主键仅包含 ID 时,我可以使用:

protected $primary_key = "id";

是否可以使用复合主键,或者我应该添加列 ID 作为唯一的主键?

提前致谢。

最佳答案

不要牺牲您的数据库结构,因为您使用的自定义库并不完全支持您所需要的。

但是有一个解决方法:

该库使用 CI 的数据库库,并在 db->where() 函数中使用 $primary_key

该函数接受以下参数格式:

$this->db->where('name', $name);
$this->db->where($array);
$this->db->where($where);//$where is a custom MySQL query string

因此,您不能通过设置来设置复合主键:

$primary_key = array("article_id", "tag_id");

因为库不断使用上面 where() 方法的第一个方法。

相反,每次只需使用 array("article_id"=> $article_id, “tag_id => tag_id)

如果您尝试插入与主键匹配的任何内容,数据库将抛出错误,因此您可以处理该错误。

关于Codeigniter MY_Model 复合主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17062606/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com