Skip to content

Commit

Permalink
Merge pull request #28 from nguyenanhung/v3.2.0-develop
Browse files Browse the repository at this point in the history
V3.2.0 develop
  • Loading branch information
nguyenanhung authored Mar 27, 2023
2 parents 6e2603b + a38d6fb commit 163a6a8
Showing 1 changed file with 50 additions and 55 deletions.
105 changes: 50 additions & 55 deletions hungng/HungNG_Custom_Based_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,25 @@ public function getTableName()
return $this->tableName;
}

/**
* Function bindDBPrefix
*
* @param $table
*
* @return string
* @author : 713uk13m <dev@nguyenanhung.com>
* @copyright: 713uk13m <dev@nguyenanhung.com>
* @time : 27/03/2023 53:42
*/
public function bindDBPrefix($table)
{
if (strpos($table, $this->db->dbprefix)) {
return $table;
}

return $this->db->dbprefix($table);
}

/**
* Function close
*
Expand Down Expand Up @@ -331,7 +350,7 @@ public function build_order_result($order_by_field, $direction = 'desc', $field
if (!empty($table)) {
$tableName = trim($table) . '.';
} else {
$tableName = $this->db->dbprefix($this->tableName) . '.';
$tableName = $this->bindDBPrefix($this->tableName) . '.';
}
if ($table === 'order_by_field') {
$tableName = '';
Expand Down Expand Up @@ -398,7 +417,7 @@ public function build_list_id_with_parent_id($allSubId, $parentId)
*/
public function prepare_simple_wheres_not_statement($value, $field = 'id', $table = '')
{
$tableName = !empty($table) ? trim($table) : $this->db->dbprefix($this->tableName);
$tableName = !empty($table) ? $this->bindDBPrefix(trim($table)) : $this->bindDBPrefix($this->tableName);
if ($value !== null) {
if (is_array($value)) {
$this->db->where_not_in($tableName . '.' . $field, $value);
Expand All @@ -424,7 +443,7 @@ public function prepare_simple_wheres_not_statement($value, $field = 'id', $tabl
*/
public function prepare_simple_wheres_statement($value, $field = 'id', $table = '')
{
$tableName = !empty($table) ? trim($table) : $this->db->dbprefix($this->tableName);
$tableName = !empty($table) ? $this->bindDBPrefix(trim($table)) : $this->bindDBPrefix($this->tableName);
if ($value !== null) {
if (is_array($value)) {
$this->db->where_in($tableName . '.' . $field, $value);
Expand Down Expand Up @@ -469,21 +488,6 @@ public function prepare_wheres_statement($wheres)
return $this->db;
}

/**
* Function apply_wheres
*
* @param $wheres
*
* @return bool|\CI_DB_query_builder|object
* @author : 713uk13m <dev@nguyenanhung.com>
* @copyright: 713uk13m <dev@nguyenanhung.com>
* @time : 27/03/2023 07:52
*/
public function apply_wheres($wheres)
{
return $this->prepare_wheres_statement($wheres);
}

/**
* Function prepare_wheres_not_statement
*
Expand Down Expand Up @@ -517,21 +521,6 @@ public function prepare_wheres_not_statement($wheres)
return $this->db;
}

/**
* Function apply_not_wheres
*
* @param $wheres
*
* @return bool|\CI_DB_query_builder|object
* @author : 713uk13m <dev@nguyenanhung.com>
* @copyright: 713uk13m <dev@nguyenanhung.com>
* @time : 27/03/2023 08:21
*/
public function apply_not_wheres($wheres)
{
return $this->prepare_wheres_not_statement($wheres);
}

/**
* Function only_status_is_active
*
Expand All @@ -547,15 +536,18 @@ public function apply_not_wheres($wheres)
public function only_status_is_active($act = true, $field = 'status', $table = '')
{
if ($act === true) {
$useTable = !empty($table) ? trim($table) : $this->tableName;
$tableName = !empty($table) ? $this->bindDBPrefix(trim($table)) : $this->bindDBPrefix($this->tableName);
$useField = !empty($field) ? trim($field) : 'status';
$tableExists = $this->db->table_exists($useTable);
$fieldExists = $this->db->field_exists($useField, $useTable);
if ($tableExists === false || $fieldExists === false) {
$tableExists = $this->db->table_exists($tableName);
if ($tableExists === false) {
return $this->db;
}
$fieldExists = $this->db->field_exists($useField, $tableName);
if ($fieldExists === false) {
return $this->db;
}

return $this->db->where($useTable . '.' . $useField, self::DEFAULT_STATUS_IS_ACTIVE);
return $this->db->where($tableName . '.' . $useField, self::DEFAULT_STATUS_IS_ACTIVE);
}

return $this->db;
Expand All @@ -576,11 +568,14 @@ public function only_status_is_active($act = true, $field = 'status', $table = '
public function only_status_is_de_active($act = true, $field = 'status', $table = '')
{
if ($act === true) {
$tableName = !empty($table) ? trim($table) : $this->db->dbprefix($this->tableName);
$tableName = !empty($table) ? $this->bindDBPrefix(trim($table)) : $this->bindDBPrefix($this->tableName);
$useField = !empty($field) ? trim($field) : 'status';
$tableExists = $this->db->table_exists($tableName);
if ($tableExists === false) {
return $this->db;
}
$fieldExists = $this->db->field_exists($useField, $tableName);
if ($tableExists === false || $fieldExists === false) {
if ($fieldExists === false) {
return $this->db;
}

Expand All @@ -605,7 +600,7 @@ public function only_status_is_de_active($act = true, $field = 'status', $table
*/
public function bind_recursive_from_category($allSubId, $parentId, $field = 'categoryId', $table = '')
{
$tableName = !empty($table) ? trim($table) : $this->db->dbprefix($this->tableName);
$tableName = !empty($table) ? $this->bindDBPrefix(trim($table)) : $this->bindDBPrefix($this->tableName);
$listID = $this->build_list_id_with_parent_id($allSubId, $parentId);
if (is_array($listID)) {
$this->db->where_in($tableName . '.' . $field, $listID);
Expand All @@ -630,7 +625,7 @@ public function bind_recursive_from_category($allSubId, $parentId, $field = 'cat
*/
public function filter_by_primary_id($id, $field = 'id', $table = '')
{
$tableName = !empty($table) ? trim($table) : $this->db->dbprefix($this->tableName);
$tableName = !empty($table) ? $this->bindDBPrefix(trim($table)) : $this->bindDBPrefix($this->tableName);
if ($id !== null) {
if (is_array($id)) {
$this->db->where_in($tableName . '.' . $field, $id);
Expand All @@ -656,7 +651,7 @@ public function filter_by_primary_id($id, $field = 'id', $table = '')
*/
public function build_operator_equal_to($id, $field = 'id', $table = '')
{
$tableName = !empty($table) ? trim($table) : $this->db->dbprefix($this->tableName);
$tableName = !empty($table) ? $this->bindDBPrefix(trim($table)) : $this->bindDBPrefix($this->tableName);
if ($id !== null) {
if (is_array($id)) {
$this->db->where_in($tableName . '.' . $field, $id);
Expand All @@ -682,7 +677,7 @@ public function build_operator_equal_to($id, $field = 'id', $table = '')
*/
public function build_operator_not_equal_to($id, $field = 'id', $table = '')
{
$tableName = !empty($table) ? trim($table) : $this->db->dbprefix($this->tableName);
$tableName = !empty($table) ? $this->bindDBPrefix(trim($table)) : $this->bindDBPrefix($this->tableName);
if ($id !== null) {
if (is_array($id)) {
$this->db->where_not_in($tableName . '.' . $field, $id);
Expand All @@ -708,7 +703,7 @@ public function build_operator_not_equal_to($id, $field = 'id', $table = '')
*/
public function build_operator_less_than_to($id, $field = 'id', $table = '')
{
$tableName = !empty($table) ? trim($table) : $this->db->dbprefix($this->tableName);
$tableName = !empty($table) ? $this->bindDBPrefix(trim($table)) : $this->bindDBPrefix($this->tableName);
$this->db->where($tableName . '.' . $field . ' ' . self::OPERATOR_LESS_THAN, $id);

return $this->db;
Expand All @@ -728,7 +723,7 @@ public function build_operator_less_than_to($id, $field = 'id', $table = '')
*/
public function build_operator_greater_than_to($id, $field = 'id', $table = '')
{
$tableName = !empty($table) ? trim($table) : $this->db->dbprefix($this->tableName);
$tableName = !empty($table) ? $this->bindDBPrefix(trim($table)) : $this->bindDBPrefix($this->tableName);
$this->db->where($tableName . '.' . $field . ' ' . self::OPERATOR_GREATER_THAN, $id);

return $this->db;
Expand All @@ -748,7 +743,7 @@ public function build_operator_greater_than_to($id, $field = 'id', $table = '')
*/
public function build_operator_less_than_or_equal_to($id, $field = 'id', $table = '')
{
$tableName = !empty($table) ? trim($table) : $this->db->dbprefix($this->tableName);
$tableName = !empty($table) ? $this->bindDBPrefix(trim($table)) : $this->bindDBPrefix($this->tableName);
$this->db->where($tableName . '.' . $field . ' ' . self::OPERATOR_LESS_THAN_OR_EQUAL_TO, $id);

return $this->db;
Expand All @@ -768,7 +763,7 @@ public function build_operator_less_than_or_equal_to($id, $field = 'id', $table
*/
public function build_operator_greater_than_or_equal_to($id, $field = 'id', $table = '')
{
$tableName = !empty($table) ? trim($table) : $this->db->dbprefix($this->tableName);
$tableName = !empty($table) ? $this->bindDBPrefix(trim($table)) : $this->bindDBPrefix($this->tableName);
$this->db->where($tableName . '.' . $field . ' ' . self::OPERATOR_GREATER_THAN_OR_EQUAL_TO, $id);

return $this->db;
Expand All @@ -788,7 +783,7 @@ public function build_operator_greater_than_or_equal_to($id, $field = 'id', $tab
*/
public function build_operator_space_ship_to($id, $field = 'id', $table = '')
{
$tableName = !empty($table) ? trim($table) : $this->db->dbprefix($this->tableName);
$tableName = !empty($table) ? $this->bindDBPrefix(trim($table)) : $this->bindDBPrefix($this->tableName);
$this->db->where($tableName . '.' . $field . ' ' . self::OPERATOR_IS_SPACESHIP, $id);

return $this->db;
Expand Down Expand Up @@ -1120,7 +1115,7 @@ public function get_list_distinct($field = '*')
*/
public function get_data_simple_result($select = '*', $wheres = array(), $size = 75, $page = 0, $orderBy = array('id' => 'DESC'))
{
$tableName = $this->db->dbprefix($this->tableName);
$tableName = $this->bindDBPrefix($this->tableName);
$this->db->select($select);
$this->db->from($this->tableName);
if (count($wheres) > 0) {
Expand All @@ -1133,8 +1128,8 @@ public function get_data_simple_result($select = '*', $wheres = array(), $size =
}
}
$this->page_limit($size, $page);
foreach ($orderBy as $key => $val) {
$this->db->order_by($tableName . '.' . $key, $val);
foreach ($orderBy as $orderField => $orderDirection) {
$this->db->order_by($tableName . '.' . $orderField, $orderDirection);
}

return $this->db->get()->result();
Expand Down Expand Up @@ -1305,7 +1300,7 @@ public function where_update($wheres, $data, $table = '')

return 0;
}
$tableName = !empty($table) ? trim($table) : $this->db->dbprefix($this->tableName);
$tableName = !empty($table) ? $this->bindDBPrefix(trim($table)) : $this->bindDBPrefix($this->tableName);
$this->prepare_wheres_statement($wheres);
$this->db->update($tableName, $data);

Expand Down Expand Up @@ -1354,7 +1349,7 @@ public function where_delete($wheres, $data, $table = '')

return 0;
}
$tableName = !empty($table) ? trim($table) : $this->db->dbprefix($this->tableName);
$tableName = !empty($table) ? $this->bindDBPrefix(trim($table)) : $this->bindDBPrefix($this->tableName);
$this->prepare_wheres_statement($wheres);
$this->db->delete($tableName, $data);

Expand All @@ -1373,7 +1368,7 @@ public function where_delete($wheres, $data, $table = '')
*/
public function request_builder($search, $table = '')
{
$tableName = !empty($table) ? trim($table) : $this->db->dbprefix($this->tableName);
$tableName = !empty($table) ? $this->bindDBPrefix(trim($table)) : $this->bindDBPrefix($this->tableName);
if (!empty($search)) {
foreach ($search as $field => $value) {
if (!empty($value) && $this->db->field_exists($field, $tableName)) {
Expand Down

0 comments on commit 163a6a8

Please sign in to comment.