Skip to content

Commit 163a6a8

Browse files
authored
Merge pull request #28 from nguyenanhung/v3.2.0-develop
V3.2.0 develop
2 parents 6e2603b + a38d6fb commit 163a6a8

File tree

1 file changed

+50
-55
lines changed

1 file changed

+50
-55
lines changed

hungng/HungNG_Custom_Based_model.php

Lines changed: 50 additions & 55 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);
@@ -469,21 +488,6 @@ public function prepare_wheres_statement($wheres)
469488
return $this->db;
470489
}
471490

472-
/**
473-
* Function apply_wheres
474-
*
475-
* @param $wheres
476-
*
477-
* @return bool|\CI_DB_query_builder|object
478-
* @author : 713uk13m <dev@nguyenanhung.com>
479-
* @copyright: 713uk13m <dev@nguyenanhung.com>
480-
* @time : 27/03/2023 07:52
481-
*/
482-
public function apply_wheres($wheres)
483-
{
484-
return $this->prepare_wheres_statement($wheres);
485-
}
486-
487491
/**
488492
* Function prepare_wheres_not_statement
489493
*
@@ -517,21 +521,6 @@ public function prepare_wheres_not_statement($wheres)
517521
return $this->db;
518522
}
519523

520-
/**
521-
* Function apply_not_wheres
522-
*
523-
* @param $wheres
524-
*
525-
* @return bool|\CI_DB_query_builder|object
526-
* @author : 713uk13m <dev@nguyenanhung.com>
527-
* @copyright: 713uk13m <dev@nguyenanhung.com>
528-
* @time : 27/03/2023 08:21
529-
*/
530-
public function apply_not_wheres($wheres)
531-
{
532-
return $this->prepare_wheres_not_statement($wheres);
533-
}
534-
535524
/**
536525
* Function only_status_is_active
537526
*
@@ -547,15 +536,18 @@ public function apply_not_wheres($wheres)
547536
public function only_status_is_active($act = true, $field = 'status', $table = '')
548537
{
549538
if ($act === true) {
550-
$useTable = !empty($table) ? trim($table) : $this->tableName;
539+
$tableName = !empty($table) ? $this->bindDBPrefix(trim($table)) : $this->bindDBPrefix($this->tableName);
551540
$useField = !empty($field) ? trim($field) : 'status';
552-
$tableExists = $this->db->table_exists($useTable);
553-
$fieldExists = $this->db->field_exists($useField, $useTable);
554-
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) {
555547
return $this->db;
556548
}
557549

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

561553
return $this->db;
@@ -576,11 +568,14 @@ public function only_status_is_active($act = true, $field = 'status', $table = '
576568
public function only_status_is_de_active($act = true, $field = 'status', $table = '')
577569
{
578570
if ($act === true) {
579-
$tableName = !empty($table) ? trim($table) : $this->db->dbprefix($this->tableName);
571+
$tableName = !empty($table) ? $this->bindDBPrefix(trim($table)) : $this->bindDBPrefix($this->tableName);
580572
$useField = !empty($field) ? trim($field) : 'status';
581573
$tableExists = $this->db->table_exists($tableName);
574+
if ($tableExists === false) {
575+
return $this->db;
576+
}
582577
$fieldExists = $this->db->field_exists($useField, $tableName);
583-
if ($tableExists === false || $fieldExists === false) {
578+
if ($fieldExists === false) {
584579
return $this->db;
585580
}
586581

@@ -605,7 +600,7 @@ public function only_status_is_de_active($act = true, $field = 'status', $table
605600
*/
606601
public function bind_recursive_from_category($allSubId, $parentId, $field = 'categoryId', $table = '')
607602
{
608-
$tableName = !empty($table) ? trim($table) : $this->db->dbprefix($this->tableName);
603+
$tableName = !empty($table) ? $this->bindDBPrefix(trim($table)) : $this->bindDBPrefix($this->tableName);
609604
$listID = $this->build_list_id_with_parent_id($allSubId, $parentId);
610605
if (is_array($listID)) {
611606
$this->db->where_in($tableName . '.' . $field, $listID);
@@ -630,7 +625,7 @@ public function bind_recursive_from_category($allSubId, $parentId, $field = 'cat
630625
*/
631626
public function filter_by_primary_id($id, $field = 'id', $table = '')
632627
{
633-
$tableName = !empty($table) ? trim($table) : $this->db->dbprefix($this->tableName);
628+
$tableName = !empty($table) ? $this->bindDBPrefix(trim($table)) : $this->bindDBPrefix($this->tableName);
634629
if ($id !== null) {
635630
if (is_array($id)) {
636631
$this->db->where_in($tableName . '.' . $field, $id);
@@ -656,7 +651,7 @@ public function filter_by_primary_id($id, $field = 'id', $table = '')
656651
*/
657652
public function build_operator_equal_to($id, $field = 'id', $table = '')
658653
{
659-
$tableName = !empty($table) ? trim($table) : $this->db->dbprefix($this->tableName);
654+
$tableName = !empty($table) ? $this->bindDBPrefix(trim($table)) : $this->bindDBPrefix($this->tableName);
660655
if ($id !== null) {
661656
if (is_array($id)) {
662657
$this->db->where_in($tableName . '.' . $field, $id);
@@ -682,7 +677,7 @@ public function build_operator_equal_to($id, $field = 'id', $table = '')
682677
*/
683678
public function build_operator_not_equal_to($id, $field = 'id', $table = '')
684679
{
685-
$tableName = !empty($table) ? trim($table) : $this->db->dbprefix($this->tableName);
680+
$tableName = !empty($table) ? $this->bindDBPrefix(trim($table)) : $this->bindDBPrefix($this->tableName);
686681
if ($id !== null) {
687682
if (is_array($id)) {
688683
$this->db->where_not_in($tableName . '.' . $field, $id);
@@ -708,7 +703,7 @@ public function build_operator_not_equal_to($id, $field = 'id', $table = '')
708703
*/
709704
public function build_operator_less_than_to($id, $field = 'id', $table = '')
710705
{
711-
$tableName = !empty($table) ? trim($table) : $this->db->dbprefix($this->tableName);
706+
$tableName = !empty($table) ? $this->bindDBPrefix(trim($table)) : $this->bindDBPrefix($this->tableName);
712707
$this->db->where($tableName . '.' . $field . ' ' . self::OPERATOR_LESS_THAN, $id);
713708

714709
return $this->db;
@@ -728,7 +723,7 @@ public function build_operator_less_than_to($id, $field = 'id', $table = '')
728723
*/
729724
public function build_operator_greater_than_to($id, $field = 'id', $table = '')
730725
{
731-
$tableName = !empty($table) ? trim($table) : $this->db->dbprefix($this->tableName);
726+
$tableName = !empty($table) ? $this->bindDBPrefix(trim($table)) : $this->bindDBPrefix($this->tableName);
732727
$this->db->where($tableName . '.' . $field . ' ' . self::OPERATOR_GREATER_THAN, $id);
733728

734729
return $this->db;
@@ -748,7 +743,7 @@ public function build_operator_greater_than_to($id, $field = 'id', $table = '')
748743
*/
749744
public function build_operator_less_than_or_equal_to($id, $field = 'id', $table = '')
750745
{
751-
$tableName = !empty($table) ? trim($table) : $this->db->dbprefix($this->tableName);
746+
$tableName = !empty($table) ? $this->bindDBPrefix(trim($table)) : $this->bindDBPrefix($this->tableName);
752747
$this->db->where($tableName . '.' . $field . ' ' . self::OPERATOR_LESS_THAN_OR_EQUAL_TO, $id);
753748

754749
return $this->db;
@@ -768,7 +763,7 @@ public function build_operator_less_than_or_equal_to($id, $field = 'id', $table
768763
*/
769764
public function build_operator_greater_than_or_equal_to($id, $field = 'id', $table = '')
770765
{
771-
$tableName = !empty($table) ? trim($table) : $this->db->dbprefix($this->tableName);
766+
$tableName = !empty($table) ? $this->bindDBPrefix(trim($table)) : $this->bindDBPrefix($this->tableName);
772767
$this->db->where($tableName . '.' . $field . ' ' . self::OPERATOR_GREATER_THAN_OR_EQUAL_TO, $id);
773768

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

794789
return $this->db;
@@ -1120,7 +1115,7 @@ public function get_list_distinct($field = '*')
11201115
*/
11211116
public function get_data_simple_result($select = '*', $wheres = array(), $size = 75, $page = 0, $orderBy = array('id' => 'DESC'))
11221117
{
1123-
$tableName = $this->db->dbprefix($this->tableName);
1118+
$tableName = $this->bindDBPrefix($this->tableName);
11241119
$this->db->select($select);
11251120
$this->db->from($this->tableName);
11261121
if (count($wheres) > 0) {
@@ -1133,8 +1128,8 @@ public function get_data_simple_result($select = '*', $wheres = array(), $size =
11331128
}
11341129
}
11351130
$this->page_limit($size, $page);
1136-
foreach ($orderBy as $key => $val) {
1137-
$this->db->order_by($tableName . '.' . $key, $val);
1131+
foreach ($orderBy as $orderField => $orderDirection) {
1132+
$this->db->order_by($tableName . '.' . $orderField, $orderDirection);
11381133
}
11391134

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

13061301
return 0;
13071302
}
1308-
$tableName = !empty($table) ? trim($table) : $this->db->dbprefix($this->tableName);
1303+
$tableName = !empty($table) ? $this->bindDBPrefix(trim($table)) : $this->bindDBPrefix($this->tableName);
13091304
$this->prepare_wheres_statement($wheres);
13101305
$this->db->update($tableName, $data);
13111306

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

13551350
return 0;
13561351
}
1357-
$tableName = !empty($table) ? trim($table) : $this->db->dbprefix($this->tableName);
1352+
$tableName = !empty($table) ? $this->bindDBPrefix(trim($table)) : $this->bindDBPrefix($this->tableName);
13581353
$this->prepare_wheres_statement($wheres);
13591354
$this->db->delete($tableName, $data);
13601355

@@ -1373,7 +1368,7 @@ public function where_delete($wheres, $data, $table = '')
13731368
*/
13741369
public function request_builder($search, $table = '')
13751370
{
1376-
$tableName = !empty($table) ? trim($table) : $this->db->dbprefix($this->tableName);
1371+
$tableName = !empty($table) ? $this->bindDBPrefix(trim($table)) : $this->bindDBPrefix($this->tableName);
13771372
if (!empty($search)) {
13781373
foreach ($search as $field => $value) {
13791374
if (!empty($value) && $this->db->field_exists($field, $tableName)) {

0 commit comments

Comments
 (0)