Refactored the empty object initialization to make it easier to clone to other model functions.

This commit is contained in:
Steve Ireland
2023-03-25 20:43:28 -04:00
parent 34ee5cc752
commit c70fcdbe60
4 changed files with 84 additions and 44 deletions

View File

@@ -378,7 +378,7 @@ class Items extends Secure_Controller
$data['tax_category'] = '';
}
$data['logo_exists'] = $item_info->pic_filename !== '';
$data['logo_exists'] = $item_info->pic_filename !== null;
$file_extension = pathinfo($item_info->pic_filename, PATHINFO_EXTENSION);
if(empty($file_extension))

View File

@@ -102,28 +102,40 @@ class Customer extends Person
}
else
{
//Get empty base parent object, as $customer_id is NOT a customer
$person_obj = parent::get_info(NEW_ENTRY);
// Initialize empty object
foreach ($this->db->getFieldData('customers') as $field) {
$field_name = $field->name;
if (in_array($field->type, array('int', 'tinyint', 'decimal')))
{
$person_obj->$field_name = 0;
}
else
{
$person_obj->$field_name = NULL;
}
}
$person_obj->person_id = NEW_ENTRY;
return $person_obj;
return $this->getEmptyObject('customers');
}
}
/**
* Initializes an empty object based on database definitions
* @param string $table_name
* @return object
*/
private function getEmptyObject(string $table_name): object
{
// Return an empty base parent object, as $item_id is NOT an item
$empty_obj = parent::get_info(NEW_ENTRY);
// Iterate through field definitions to determine how the fields should be initialized
foreach($this->db->getFieldData($table_name) as $field) {
$field_name = $field->name;
if(in_array($field->type, array('int', 'tinyint', 'decimal')))
{
$empty_obj->$field_name = ($field->primary_key == 1) ? NEW_ENTRY : 0;
}
else
{
$empty_obj->$field_name = NULL;
}
}
return $empty_obj;
}
/**
* Gets stats about a particular customer
*/

View File

@@ -42,6 +42,7 @@ class Item extends Model
'hsn_code'
];
/**
* Determines if a given item_id is an item
*/
@@ -325,25 +326,36 @@ class Item extends Model
return $query->getRow();
}
//Get empty base parent object, as $item_id is NOT an item
$item_obj = new stdClass();
return $this->getEmptyObject('items');
}
// Initialize empty object
/**
* Initializes an empty object based on database definitions
* @param string $table_name
* @return object
*/
private function getEmptyObject(string $table_name): object
{
// Return an empty base parent object, as $item_id is NOT an item
$empty_obj = new stdClass();
// Iterate through field definitions to determine how the fields should be initialized
foreach($this->db->getFieldData($table_name) as $field) {
foreach ($this->db->getFieldData('items') as $field) {
$field_name = $field->name;
if (in_array($field->type, array('int', 'tinyint', 'decimal')))
if(in_array($field->type, array('int', 'tinyint', 'decimal')))
{
$item_obj->$field_name = 0;
$empty_obj->$field_name = ($field->primary_key == 1) ? NEW_ENTRY : 0;
}
else
{
$item_obj->$field_name = NULL;
$empty_obj->$field_name = NULL;
}
}
$item_obj->item_id = NEW_ENTRY;
return $item_obj;
return $empty_obj;
}
/**

View File

@@ -92,24 +92,40 @@ class Person extends Model
}
else
{
$person_obj = new stdClass();
foreach ($this->db->getFieldData('people') as $field) {
$field_name = $field->name;
if (in_array($field->type, array('int', 'tinyint', 'decimal')))
{
$person_obj->$field_name = 0;
}
else
{
$person_obj->$field_name = NULL;
}
}
$person_obj->person_id = NEW_ENTRY;
return $person_obj;
return $this->getEmptyObject('people');
}
}
/**
* Initializes an empty object based on database definitions
* @param string $table_name
* @return object
*/
private function getEmptyObject(string $table_name): object
{
// Return an empty base parent object, as $item_id is NOT an item
$empty_obj = new stdClass();
// Iterate through field definitions to determine how the fields should be initialized
foreach($this->db->getFieldData($table_name) as $field) {
$field_name = $field->name;
if(in_array($field->type, array('int', 'tinyint', 'decimal')))
{
$empty_obj->$field_name = ($field->primary_key == 1) ? NEW_ENTRY : 0;
}
else
{
$empty_obj->$field_name = NULL;
}
}
return $empty_obj;
}
/**
* Gets information about people as an array of rows
*