Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
rajakodings committed Jan 26, 2020
1 parent 26ea08e commit d3c60df
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 26 deletions.
File renamed without changes.
Empty file added logs/index.html
Empty file.
16 changes: 1 addition & 15 deletions system/Helpers/Helper.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
<?php

if(!function_exists('get_model_config')) {
function get_model_config($class_name) {
$class_array = explode("\\",$class_name);
$class_model_name = end($class_array);
if($cache_model = get_singleton("gmc_".$class_model_name)) {
return $cache_model;
} else {
$file = include base_path("app/Configs/Models/".$class_model_name.".php");
put_singleton("gmc_".$class_model_name, $file);
return $file;
}
}
}

$singleton_data = [];

if(!function_exists("put_singleton")) {
Expand Down Expand Up @@ -293,7 +279,7 @@ function abort($response_code = 404, $message = null) {
* @param string $type
*/
function logging($content, $type = "error") {
file_put_contents(getcwd()."/system/Logs/".date("Y-m-d").".log", "[".date("Y-m-d H:i:s")."][".$type."] - ".$content."\n\n", FILE_APPEND);
file_put_contents(getcwd()."/logs/".date("Y-m-d").".log", "[".date("Y-m-d H:i:s")."][".$type."] - ".$content."\n\n", FILE_APPEND);
}
}

Expand Down
33 changes: 22 additions & 11 deletions system/Models/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@
class Model
{

private static function get_model_config($class_name) {
$class_array = explode("\\",$class_name);
$class_model_name = end($class_array);
if($cache_model = get_singleton("gmc_".$class_model_name)) {
return $cache_model;
} else {
$file = include base_path("app/Configs/Models/".$class_model_name.".php");
put_singleton("gmc_".$class_model_name, $file);
return $file;
}
}

private static function modelSetter($model, $columns, $row) {
foreach($columns as $column) {
$method_name = "set".convert_snake_to_CamelCase($column['name'], true);
Expand All @@ -23,14 +35,13 @@ private static function modelSetter($model, $columns, $row) {
return $model;
}


public static function getPrimaryKey() {
$config = get_model_config(get_called_class());
$config = static::get_model_config(get_called_class());
return $config['primary_key'];
}

public static function getTableName() {
$config = get_model_config(get_called_class());
$config = static::get_model_config(get_called_class());
return $config['table'];
}

Expand All @@ -42,11 +53,11 @@ public static function getTableName() {
* @throws \Exception
*/
private static function queryAll($limit, $offset, callable $query = null) {
$config = get_model_config(get_called_class());
$config = static::get_model_config(get_called_class());
$data = DB($config['table']);
foreach($config['columns'] as $i=>$column) {
if(isset($column['join_model'])) {
$join_config = get_model_config($column['join_model']);
$join_config = static::get_model_config($column['join_model']);
$data->join($join_config['table']." as ".$join_config['table']." on ".$join_config['table'].".".$join_config['primary_key']." = ".$column['column']);
foreach($join_config['columns'] as $join_column) {
$data->addSelect($join_config['table'].".".$join_column['column']." as ".$join_config['table']."___".$join_column['column']);
Expand Down Expand Up @@ -81,7 +92,7 @@ private static function queryAll($limit, $offset, callable $query = null) {
* @return mixed
*/
public static function loadArray(array $data_array) {
$config = get_model_config(get_called_class());
$config = static::get_model_config(get_called_class());
return static::modelSetter(new static(), $config['columns'], $data_array);
}

Expand Down Expand Up @@ -120,7 +131,7 @@ public static function findById($id) {
if($last_data = get_singleton(basename(get_called_class()).'_findById_'.$id)) {
return $last_data;
} else {
$config = get_model_config(get_called_class());
$config = static::get_model_config(get_called_class());
// Get record
$row = DB($config['table'])->find($id);
if($row) {
Expand Down Expand Up @@ -148,7 +159,7 @@ public static function findBy($column, $value) {
if($last_data = get_singleton(basename(get_called_class()).'_findBy_'.$column.'_'.$value)) {
return $last_data;
} else {
$config = get_model_config(get_called_class());
$config = static::get_model_config(get_called_class());
// Get record
$row = DB($config['table'])->where($column." = '".$value."'")->find();
if ($row) {
Expand All @@ -170,7 +181,7 @@ public static function findBy($column, $value) {
* @throws \Exception
*/
public function save() {
$config = get_model_config(get_called_class());
$config = static::get_model_config(get_called_class());
$method_get_pk = "get".convert_snake_to_CamelCase($config['primary_key'], true);
$data_array = [];
foreach($config['columns'] as $column) {
Expand Down Expand Up @@ -211,7 +222,7 @@ public function save() {
* @throws \Exception
*/
public static function delete($id) {
$config = get_model_config(get_called_class());
$config = static::get_model_config(get_called_class());
DB($config['table'])->delete($id);
}

Expand All @@ -220,7 +231,7 @@ public static function delete($id) {
* @throws \Exception
*/
public static function deleteBy(array $param) {
$config = get_model_config(get_called_class());
$config = static::get_model_config(get_called_class());
DB($config['table'])->delete($param);
}

Expand Down

0 comments on commit d3c60df

Please sign in to comment.