Skip to content

Commit fda66f2

Browse files
committed
added Model::map method
Added `Model::map(...)` method
1 parent f340acf commit fda66f2

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/Model.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,29 @@ private static function _get_model_info() {
140140
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)');
141141
}
142142

143+
/**
144+
* Create an array given a field from the data set
145+
*
146+
* @param $field
147+
* the field to map
148+
*
149+
*/
150+
public static function map($field = 'id') {
151+
$model = self::_get_model_info();
152+
if (!$model) return false;
153+
if (!self::get_field($field)) return false;
154+
155+
$table = $model['table'];
156+
$active_field = self::get_field('active') ? "active = 1" : "";
157+
158+
$data = self::query("SELECT $field FROM `$table` WHERE $active_field");
159+
if (!$data) return [];
160+
161+
return array_map(function($row) use ($field) {
162+
return is_array($row) ? $row[$field] : $row->{$field};
163+
}, $data);
164+
}
165+
143166
/**
144167
* Sets the type of a property value
145168
*

0 commit comments

Comments
 (0)