Skip to content

Commit ac1d788

Browse files
authored
Update Model.php
1 parent 868fae5 commit ac1d788

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/Model.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public static function set_db($db) {
109109

110110
return self::_error('db is not valid');
111111
}
112-
112+
113113
/**
114114
* Get the DB object from the base Model
115115
*
@@ -125,10 +125,19 @@ public static function get_db() {
125125
* @return bool
126126
* Returns true if table exists and successfully registered to the Model class. Otherwise false.
127127
*/
128-
public static function register($table, $pk = 'id') {
129-
if ($table_info = self::$db->get_table_info($table)) {
128+
public static function register($table, $pk = null) {
129+
if ($fields = self::$db->get_table_info($table)) {
130+
if (!$pk) {
131+
foreach ($fields as $field => $info) {
132+
if ($info['primary'] === true) {
133+
$pk = $field;
134+
break;
135+
}
136+
}
137+
}
138+
130139
self::$_models[self::get_name()] = [
131-
'fields' => self::$db->get_table_info($table),
140+
'fields' => $fields,
132141
'table' => $table,
133142
'pk' => $pk
134143
];

0 commit comments

Comments
 (0)