Skip to content

Commit

Permalink
Fixes to ORM
Browse files Browse the repository at this point in the history
  • Loading branch information
andrevanzuydam committed Jan 16, 2020
1 parent e9795d6 commit f5d9f04
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions Tina4/ORM.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class ORM implements \JsonSerializable
*/
function __construct($request = null, $tableName = "", $fieldMapping = "", $primaryKey = "", $tableFilter = "", $DBA = null)
{
$this->create ($request = null, $tableName = "", $fieldMapping = "", $primaryKey = "", $tableFilter = "", $DBA = null);
$this->create ($request, $tableName, $fieldMapping, $primaryKey, $tableFilter, $DBA);
}

/**
Expand Down Expand Up @@ -96,14 +96,18 @@ function create($request = null, $tableName = "", $fieldMapping = "", $primaryKe
}
}



if ($request) {

if (!is_array($request) && !is_object($request) && json_decode($request)) {
$request = json_decode($request);
foreach ($request as $key => $value) {
$this->{$key} = $value;
}
} else {
foreach ($request as $key => $value) {

if (property_exists($this, $key)) {
$this->{$key} = $value;
} else {
Expand Down Expand Up @@ -145,11 +149,8 @@ function getFieldName($name, $fieldMapping = [])
* @param string $name Improper object name
* @return string Proper object name
*/
function getObjectName($name)
function getObjectName($name, $dbResult=false)
{
//if (property_exists($this, $name) && empty($this->fieldMapping)) return $name;
//print_r ($this->fieldMapping);

if (!empty($this->fieldMapping) && $this->fieldMapping[$name]) {
$fieldName = $this->fieldMapping[$name];
return $fieldName;
Expand All @@ -166,11 +167,15 @@ function getObjectName($name)
}
}
} else {
for ($i = 0; $i < strlen($name); $i++) {
if ($name[$i] !== strtolower($name[$i])) {
$fieldName .= "_" . strtolower($name[$i]);
} else {
$fieldName .= $name[$i];
if ($dbResult) {
$fieldName = strtolower($name);
} else {
for ($i = 0; $i < strlen($name); $i++) {
if ($name[$i] !== strtolower($name[$i])) {
$fieldName .= "_" . strtolower($name[$i]);
} else {
$fieldName .= $name[$i];
}
}
}
}
Expand Down Expand Up @@ -207,7 +212,7 @@ function generateInsertSQL($tableData, $tableName = "")

}
if (is_null($fieldValue)) $fieldValue = "null";
if ($fieldValue === "null" || is_numeric($fieldValue) && $fieldValue[0] !== "0") {
if ($fieldValue === "null" || is_numeric($fieldValue)) {
$insertValues[] = $fieldValue;
} else {
$fieldValue = str_replace("'", "''", $fieldValue);
Expand Down Expand Up @@ -235,8 +240,9 @@ function generateUpdateSQL($tableData, $filter, $tableName = "")

$fieldName = $this->getObjectName($fieldName);


if (is_null($fieldValue)) $fieldValue = "null";
if ($fieldValue === "null" || is_numeric($fieldValue && $fieldValue[0] !== "0")) {
if ($fieldValue === "null" || is_numeric($fieldValue)) {
$updateValues[] = "{$fieldName} = {$fieldValue}";
} else {
$fieldValue = str_replace("'", "''", $fieldValue);
Expand Down Expand Up @@ -426,14 +432,11 @@ function save($tableName = "", $fieldMapping = [])

$sqlFetch = "select * from {$tableName} where {$primaryCheck}";

//print_r ($this->DBA->fetch($sqlFetch, 1));


$fetchData = json_decode($this->DBA->fetch($sqlFetch, 1) . "")->data[0];
$fetchData = $this->DBA->fetch($sqlFetch, 1)->record(0);

$tableResult = [];
foreach ($fetchData as $fieldName => $fieldValue) {
$tableResult[self::getObjectName($fieldName)] = $fieldValue;
$tableResult[self::getObjectName($fieldName,true)] = $fieldValue;
}
return (object)$tableResult;
} else {
Expand Down Expand Up @@ -570,4 +573,4 @@ public function generateForm($columns=1, $ignoreFields=null, $namePrefix="", $gr
}


}
}

0 comments on commit f5d9f04

Please sign in to comment.