Skip to content

Commit 13c67c4

Browse files
committed
Primary Key Guessing Implemented
1 parent 6e63f59 commit 13c67c4

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

core/MY_Model.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class MY_Model extends CI_Model
3131
* This model's default primary key or unique identifier.
3232
* Used by the get(), update() and delete() functions.
3333
*/
34-
protected $primary_key = 'id';
34+
protected $primary_key = NULL;
3535

3636
/**
3737
* Support for soft deletes and this model's 'deleted' key
@@ -104,6 +104,7 @@ public function __construct()
104104
$this->load->helper('inflector');
105105

106106
$this->_fetch_table();
107+
$this->_fetch_primary_key();
107108
$this->_database = $this->db;
108109

109110
array_unshift($this->before_create, 'protect_attributes');
@@ -859,6 +860,17 @@ private function _fetch_table()
859860
}
860861
}
861862

863+
/**
864+
* Guess the primary key for current table
865+
*/
866+
private function _fetch_primary_key()
867+
{
868+
if($this->primary_key == NULl)
869+
{
870+
$this->primary_key = $this->_database->query("SHOW KEYS FROM `".$this->_table."` WHERE Key_name = 'PRIMARY'")->row()->Column_name;
871+
}
872+
}
873+
862874
/**
863875
* Set WHERE parameters, cleverly
864876
*/

0 commit comments

Comments
 (0)