Skip to content

Commit a38d6fb

Browse files
committed
Add method Binding DB Prefix
1 parent 4524ca5 commit a38d6fb

File tree

1 file changed

+50
-25
lines changed

1 file changed

+50
-25
lines changed

hungng/HungNG_Custom_Based_model.php

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,25 @@ public function getTableName()
209209
return $this->tableName;
210210
}
211211

212+
/**
213+
* Function bindDBPrefix
214+
*
215+
* @param $table
216+
*
217+
* @return string
218+
* @author : 713uk13m <dev@nguyenanhung.com>
219+
* @copyright: 713uk13m <dev@nguyenanhung.com>
220+
* @time : 27/03/2023 53:42
221+
*/
222+
public function bindDBPrefix($table)
223+
{
224+
if (strpos($table, $this->db->dbprefix)) {
225+
return $table;
226+
}
227+
228+
return $this->db->dbprefix($table);
229+
}
230+
212231
/**
213232
* Function close
214233
*
@@ -331,7 +350,7 @@ public function build_order_result($order_by_field, $direction = 'desc', $field
331350
if (!empty($table)) {
332351
$tableName = trim($table) . '.';
333352
} else {
334-
$tableName = $this->db->dbprefix($this->tableName) . '.';
353+
$tableName = $this->bindDBPrefix($this->tableName) . '.';
335354
}
336355
if ($table === 'order_by_field') {
337356
$tableName = '';
@@ -398,7 +417,7 @@ public function build_list_id_with_parent_id($allSubId, $parentId)
398417
*/
399418
public function prepare_simple_wheres_not_statement($value, $field = 'id', $table = '')
400419
{
401-
$tableName = !empty($table) ? trim($table) : $this->db->dbprefix($this->tableName);
420+
$tableName = !empty($table) ? $this->bindDBPrefix(trim($table)) : $this->bindDBPrefix($this->tableName);
402421
if ($value !== null) {
403422
if (is_array($value)) {
404423
$this->db->where_not_in($tableName . '.' . $field, $value);
@@ -424,7 +443,7 @@ public function prepare_simple_wheres_not_statement($value, $field = 'id', $tabl
424443
*/
425444
public function prepare_simple_wheres_statement($value, $field = 'id', $table = '')
426445
{
427-
$tableName = !empty($table) ? trim($table) : $this->db->dbprefix($this->tableName);
446+
$tableName = !empty($table) ? $this->bindDBPrefix(trim($table)) : $this->bindDBPrefix($this->tableName);
428447
if ($value !== null) {
429448
if (is_array($value)) {
430449
$this->db->where_in($tableName . '.' . $field, $value);
@@ -517,15 +536,18 @@ public function prepare_wheres_not_statement($wheres)
517536
public function only_status_is_active($act = true, $field = 'status', $table = '')
518537
{
519538
if ($act === true) {
520-
$useTable = !empty($table) ? trim($table) : $this->tableName;
539+
$tableName = !empty($table) ? $this->bindDBPrefix(trim($table)) : $this->bindDBPrefix($this->tableName);
521540
$useField = !empty($field) ? trim($field) : 'status';
522-
$tableExists = $this->db->table_exists($useTable);
523-
$fieldExists = $this->db->field_exists($useField, $useTable);
524-
if ($tableExists === false || $fieldExists === false) {
541+
$tableExists = $this->db->table_exists($tableName);
542+
if ($tableExists === false) {
543+
return $this->db;
544+
}
545+
$fieldExists = $this->db->field_exists($useField, $tableName);
546+
if ($fieldExists === false) {
525547
return $this->db;
526548
}
527549

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

531553
return $this->db;
@@ -546,11 +568,14 @@ public function only_status_is_active($act = true, $field = 'status', $table = '
546568
public function only_status_is_de_active($act = true, $field = 'status', $table = '')
547569
{
548570
if ($act === true) {
549-
$tableName = !empty($table) ? trim($table) : $this->db->dbprefix($this->tableName);
571+
$tableName = !empty($table) ? $this->bindDBPrefix(trim($table)) : $this->bindDBPrefix($this->tableName);
550572
$useField = !empty($field) ? trim($field) : 'status';
551573
$tableExists = $this->db->table_exists($tableName);
574+
if ($tableExists === false) {
575+
return $this->db;
576+
}
552577
$fieldExists = $this->db->field_exists($useField, $tableName);
553-
if ($tableExists === false || $fieldExists === false) {
578+
if ($fieldExists === false) {
554579
return $this->db;
555580
}
556581

@@ -575,7 +600,7 @@ public function only_status_is_de_active($act = true, $field = 'status', $table
575600
*/
576601
public function bind_recursive_from_category($allSubId, $parentId, $field = 'categoryId', $table = '')
577602
{
578-
$tableName = !empty($table) ? trim($table) : $this->db->dbprefix($this->tableName);
603+
$tableName = !empty($table) ? $this->bindDBPrefix(trim($table)) : $this->bindDBPrefix($this->tableName);
579604
$listID = $this->build_list_id_with_parent_id($allSubId, $parentId);
580605
if (is_array($listID)) {
581606
$this->db->where_in($tableName . '.' . $field, $listID);
@@ -600,7 +625,7 @@ public function bind_recursive_from_category($allSubId, $parentId, $field = 'cat
600625
*/
601626
public function filter_by_primary_id($id, $field = 'id', $table = '')
602627
{
603-
$tableName = !empty($table) ? trim($table) : $this->db->dbprefix($this->tableName);
628+
$tableName = !empty($table) ? $this->bindDBPrefix(trim($table)) : $this->bindDBPrefix($this->tableName);
604629
if ($id !== null) {
605630
if (is_array($id)) {
606631
$this->db->where_in($tableName . '.' . $field, $id);
@@ -626,7 +651,7 @@ public function filter_by_primary_id($id, $field = 'id', $table = '')
626651
*/
627652
public function build_operator_equal_to($id, $field = 'id', $table = '')
628653
{
629-
$tableName = !empty($table) ? trim($table) : $this->db->dbprefix($this->tableName);
654+
$tableName = !empty($table) ? $this->bindDBPrefix(trim($table)) : $this->bindDBPrefix($this->tableName);
630655
if ($id !== null) {
631656
if (is_array($id)) {
632657
$this->db->where_in($tableName . '.' . $field, $id);
@@ -652,7 +677,7 @@ public function build_operator_equal_to($id, $field = 'id', $table = '')
652677
*/
653678
public function build_operator_not_equal_to($id, $field = 'id', $table = '')
654679
{
655-
$tableName = !empty($table) ? trim($table) : $this->db->dbprefix($this->tableName);
680+
$tableName = !empty($table) ? $this->bindDBPrefix(trim($table)) : $this->bindDBPrefix($this->tableName);
656681
if ($id !== null) {
657682
if (is_array($id)) {
658683
$this->db->where_not_in($tableName . '.' . $field, $id);
@@ -678,7 +703,7 @@ public function build_operator_not_equal_to($id, $field = 'id', $table = '')
678703
*/
679704
public function build_operator_less_than_to($id, $field = 'id', $table = '')
680705
{
681-
$tableName = !empty($table) ? trim($table) : $this->db->dbprefix($this->tableName);
706+
$tableName = !empty($table) ? $this->bindDBPrefix(trim($table)) : $this->bindDBPrefix($this->tableName);
682707
$this->db->where($tableName . '.' . $field . ' ' . self::OPERATOR_LESS_THAN, $id);
683708

684709
return $this->db;
@@ -698,7 +723,7 @@ public function build_operator_less_than_to($id, $field = 'id', $table = '')
698723
*/
699724
public function build_operator_greater_than_to($id, $field = 'id', $table = '')
700725
{
701-
$tableName = !empty($table) ? trim($table) : $this->db->dbprefix($this->tableName);
726+
$tableName = !empty($table) ? $this->bindDBPrefix(trim($table)) : $this->bindDBPrefix($this->tableName);
702727
$this->db->where($tableName . '.' . $field . ' ' . self::OPERATOR_GREATER_THAN, $id);
703728

704729
return $this->db;
@@ -718,7 +743,7 @@ public function build_operator_greater_than_to($id, $field = 'id', $table = '')
718743
*/
719744
public function build_operator_less_than_or_equal_to($id, $field = 'id', $table = '')
720745
{
721-
$tableName = !empty($table) ? trim($table) : $this->db->dbprefix($this->tableName);
746+
$tableName = !empty($table) ? $this->bindDBPrefix(trim($table)) : $this->bindDBPrefix($this->tableName);
722747
$this->db->where($tableName . '.' . $field . ' ' . self::OPERATOR_LESS_THAN_OR_EQUAL_TO, $id);
723748

724749
return $this->db;
@@ -738,7 +763,7 @@ public function build_operator_less_than_or_equal_to($id, $field = 'id', $table
738763
*/
739764
public function build_operator_greater_than_or_equal_to($id, $field = 'id', $table = '')
740765
{
741-
$tableName = !empty($table) ? trim($table) : $this->db->dbprefix($this->tableName);
766+
$tableName = !empty($table) ? $this->bindDBPrefix(trim($table)) : $this->bindDBPrefix($this->tableName);
742767
$this->db->where($tableName . '.' . $field . ' ' . self::OPERATOR_GREATER_THAN_OR_EQUAL_TO, $id);
743768

744769
return $this->db;
@@ -758,7 +783,7 @@ public function build_operator_greater_than_or_equal_to($id, $field = 'id', $tab
758783
*/
759784
public function build_operator_space_ship_to($id, $field = 'id', $table = '')
760785
{
761-
$tableName = !empty($table) ? trim($table) : $this->db->dbprefix($this->tableName);
786+
$tableName = !empty($table) ? $this->bindDBPrefix(trim($table)) : $this->bindDBPrefix($this->tableName);
762787
$this->db->where($tableName . '.' . $field . ' ' . self::OPERATOR_IS_SPACESHIP, $id);
763788

764789
return $this->db;
@@ -1090,7 +1115,7 @@ public function get_list_distinct($field = '*')
10901115
*/
10911116
public function get_data_simple_result($select = '*', $wheres = array(), $size = 75, $page = 0, $orderBy = array('id' => 'DESC'))
10921117
{
1093-
$tableName = $this->db->dbprefix($this->tableName);
1118+
$tableName = $this->bindDBPrefix($this->tableName);
10941119
$this->db->select($select);
10951120
$this->db->from($this->tableName);
10961121
if (count($wheres) > 0) {
@@ -1103,8 +1128,8 @@ public function get_data_simple_result($select = '*', $wheres = array(), $size =
11031128
}
11041129
}
11051130
$this->page_limit($size, $page);
1106-
foreach ($orderBy as $key => $val) {
1107-
$this->db->order_by($tableName . '.' . $key, $val);
1131+
foreach ($orderBy as $orderField => $orderDirection) {
1132+
$this->db->order_by($tableName . '.' . $orderField, $orderDirection);
11081133
}
11091134

11101135
return $this->db->get()->result();
@@ -1275,7 +1300,7 @@ public function where_update($wheres, $data, $table = '')
12751300

12761301
return 0;
12771302
}
1278-
$tableName = !empty($table) ? trim($table) : $this->db->dbprefix($this->tableName);
1303+
$tableName = !empty($table) ? $this->bindDBPrefix(trim($table)) : $this->bindDBPrefix($this->tableName);
12791304
$this->prepare_wheres_statement($wheres);
12801305
$this->db->update($tableName, $data);
12811306

@@ -1324,7 +1349,7 @@ public function where_delete($wheres, $data, $table = '')
13241349

13251350
return 0;
13261351
}
1327-
$tableName = !empty($table) ? trim($table) : $this->db->dbprefix($this->tableName);
1352+
$tableName = !empty($table) ? $this->bindDBPrefix(trim($table)) : $this->bindDBPrefix($this->tableName);
13281353
$this->prepare_wheres_statement($wheres);
13291354
$this->db->delete($tableName, $data);
13301355

@@ -1343,7 +1368,7 @@ public function where_delete($wheres, $data, $table = '')
13431368
*/
13441369
public function request_builder($search, $table = '')
13451370
{
1346-
$tableName = !empty($table) ? trim($table) : $this->db->dbprefix($this->tableName);
1371+
$tableName = !empty($table) ? $this->bindDBPrefix(trim($table)) : $this->bindDBPrefix($this->tableName);
13471372
if (!empty($search)) {
13481373
foreach ($search as $field => $value) {
13491374
if (!empty($value) && $this->db->field_exists($field, $tableName)) {

0 commit comments

Comments
 (0)