【发布时间】:2021-03-02 16:13:08
【问题描述】:
有什么方法可以在 CodeIgniter 4 中使用 Models 实现复合主键?
像这样?
class SomeModel extends Model{
protected $table = 'table';
protected $primaryKey1 = 'primary_composite_id1';
protected $primaryKey2 = 'primary_composite_id2';
protected $primaryKey3 = 'primary_composite_id3';
// numbers in the identifiers were only added for clarity
...
}
我认为您可以使用Forge 类定义表结构。
这是我如何在 Migrations 中定义表的 sn-p。
在SomeMigration.php
class SomeClass extends Migration{
/* added fields here */
...
// Set them as foreign keys
$this->$forge->addForeignKey('item_id', 'item', 'item_id', 'CASCADE', 'CASCADE');
$this->$forge->addForeignKey('poster_uid', 'user', 'user_id', 'CASCADE', 'CASCADE');
$this->$forge->addForeignKey('customer_uid', 'user', 'user_id', 'CASCADE', 'CASCADE');
// Set them as primary keys
$this->$forge->addPrimaryKey('item_id');
$this->$forge->addPrimaryKey('poster_uid');
$this->$forge->addPrimaryKey('customer_uid');
...
}
另外,如果在Models 中设置复合主键是不可能的,我应该
- 改为为该表创建一个新的主键列?
- 将
$primarykey值留空并仅使用使用查询(例如使用WHERE)? - 使用表中任意一列(设置为PK)作为
$primarykey的值?
问题是根据 Codeigniter MY_Model composite primary key 这个帖子创建的,因为答案没有直接回答问题
我目前正在为我们的学校项目使用该框架。文档中没有提到,所以我被卡住了。非常感谢任何一种替代解决方案。谢谢!干杯!
【问题讨论】:
标签: codeigniter composite-primary-key codeigniter-4