Skip to content

Commit f340acf

Browse files
committed
flexibility stuff
1 parent 5f9b68a commit f340acf

File tree

2 files changed

+54
-29
lines changed

2 files changed

+54
-29
lines changed

src/DB.php

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -437,31 +437,13 @@ public function get_info($table) {
437437
* @return array
438438
*/
439439
public function get_fields($table = '', $return_fields = []) {
440-
$table = $this->_prefix . $table;
441-
$driver = $this->getAttribute(\PDO::ATTR_DRIVER_NAME);
442-
443-
if ($driver == 'sqlite') {
444-
$sql = "PRAGMA table_info('$table');";
445-
$key = "name";
446-
} elseif ($driver == 'mysql') {
447-
$sql = "DESCRIBE `$table`;";
448-
$key = "Field";
449-
} else {
450-
$sql = "SELECT column_name FROM information_schema.columns ";
451-
$sql .= "WHERE table_name = '$table';";
452-
$key = "column_name";
453-
}
454-
455-
if (false !== ($list = $this->run($sql))) {
456-
$fields = [];
457-
foreach ($list as $record) {
458-
$fields[] = $record->{$key};
459-
}
460-
440+
$info = $this->get_info($table);
441+
if ($info) {
442+
$fields = array_keys($info);
461443
return $return_fields ? array_values(array_intersect($fields, $return_fields)) : $fields;
462444
}
463445

464-
return [];
446+
return false;
465447
}
466448

467449
/**

src/Model.php

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ public static function instance($arg1, $arg2 = null) {
8585
}
8686
}
8787

88-
return self::query_row("SELECT * FROM `$table` WHERE $fields AND active = 1", $bind);
88+
$active_field = self::get_field('active') ? "AND active = 1" : "";
89+
90+
return self::query_row("SELECT * FROM `$table` WHERE $fields $active_field", $bind);
8991
}
9092

9193
/**
@@ -109,11 +111,12 @@ public static function set_db($db) {
109111
* @return bool
110112
* Returns true if table exists and successfully registered to the Model class. Otherwise false.
111113
*/
112-
public static function register($table) {
114+
public static function register($table, $pk = 'id') {
113115
if ($table_info = self::$db->get_info($table)) {
114116
self::$_models[self::get_name()] = [
115117
'fields' => self::$db->get_info($table),
116-
'table' => $table
118+
'table' => $table,
119+
'pk' => $pk
117120
];
118121

119122
return true;
@@ -134,7 +137,7 @@ private static function _get_model_info() {
134137
return self::$_models[$class_name];
135138
}
136139

137-
return self::_error(self::get_name().' must be registered with it\'s corresponding table name and database. Use '.self::get_name().'::register($db, $table)');
140+
return self::_error(self::get_name().' must be registered with it\'s corresponding table name and database. Use \Models\\'.self::get_name().'::register($db, $table)');
138141
}
139142

140143
/**
@@ -277,6 +280,7 @@ public function update($arg1, $arg2 = null) {
277280
if (!$model) return false;
278281

279282
$table = $model['table'];
283+
$pk = $model['pk'];
280284
$data = [];
281285

282286
if (isset($arg1) && !isset($arg2)) {
@@ -287,23 +291,62 @@ public function update($arg1, $arg2 = null) {
287291
return false;
288292
}
289293

290-
$updated = self::$db->update($table, $data, 'id = :id', [':id' => $this->id]);
294+
$updated = self::$db->update($table, $data, "$pk = :pk", [':pk' => $this->{$pk}]);
291295
if ($updated) {
292-
self::$db->query_row("SELECT * FROM `$table` WHERE id = :id", [':id' => $this->id], $this, \PDO::FETCH_INTO);
296+
self::$db->query_row("SELECT * FROM `$table` WHERE $pk = :pk", [':pk' => $this->{$pk}], $this, \PDO::FETCH_INTO);
293297
}
294298

295299
return $updated;
296300
}
297301

302+
/**
303+
* Get field info
304+
*
305+
* @param $field
306+
* Field name
307+
*
308+
* @return bool
309+
*
310+
*/
311+
public static function get_field($field) {
312+
$model = self::_get_model_info();
313+
if (!$model) return false;
314+
315+
return isset($model['fields'][$field]) ? $model['fields'][$field] : false;
316+
}
317+
318+
/**
319+
* Is field a primary key
320+
*
321+
* @param @field
322+
* Field name
323+
*
324+
* @return bool
325+
*
326+
*/
327+
public static function is_pk($field) {
328+
$field = self::get_field($field);
329+
return $field ? $field['primary'] : false;
330+
}
331+
298332
/**
299333
* Delete the model (deactivate). We are not actually deleting row but instead setting `active` = 1 (hence active field is required)
300334
*
301335
* @return int
302336
* Returns the number of rows affected from "update"
303337
*/
304338
public function delete() {
339+
$model = self::_get_model_info();
340+
if (!$model) return false;
341+
342+
$table = $model['table'];
343+
$pk = $model['pk'];
344+
305345
// we don't delete here :P
306-
return $this->update('active', 0);
346+
if (self::get_field('active'))
347+
return $this->update('active', 0);
348+
else
349+
return self::$db->delete("DELETE FROM $table WHERE $pk = :pk", [':pk' => $this->{$pk}]);
307350
}
308351

309352
/**

0 commit comments

Comments
 (0)