Advertisement
FebimaHermawan

Contoh Model CI 4.1.3

Jan 10th, 2022
1,352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.07 KB | None | 0 0
  1. <?php
  2. namespace App\Models\CMS\Application;
  3.  
  4. use CodeIgniter\Model;
  5.  
  6. class Investment_model extends Model {
  7.  
  8.     // -------------------------------
  9.     // -------------------------------
  10.     //           Construct
  11.     // -------------------------------
  12.     // ---------------------------- //
  13.    
  14.     protected $table = "tb_investment";
  15.  
  16.     // -------------------------------
  17.     // ----------- Selesai -----------
  18.     // ---------------------------- //
  19.  
  20.  
  21.     // -------------------------------
  22.     // -------------------------------
  23.     //           Main Model
  24.     // -------------------------------
  25.     // ---------------------------- //
  26.  
  27.     public function select_table($parameter, $search, $limit, $start_limit, $order, $sort_type, $num_rows=TRUE){
  28.         $this->select("*");
  29.         $this->table($this->table);
  30.         if ($parameter != FALSE) {
  31.             if ($parameter['id'] != FALSE) {
  32.                 $this->where('md5(id) =', $parameter['id']);
  33.             }
  34.             if ($parameter['is_deleted'] > -1) {
  35.                 $this->where('is_deleted', $parameter['is_deleted']);
  36.             }
  37.         }
  38.         if ($search != FALSE) {
  39.             $this->like('(LOWER(name)', strtolower($search));
  40.             $this->orLike('LOWER(name_sektor_sub)', strtolower($search));
  41.             $this->orLike('LOWER(name_kecamatan)', strtolower($search));
  42.             $this->orLike('price', strtolower($search));
  43.             $this->orLike('date_update', strtolower($search));
  44.             $this->orLike('LOWER(is_deleted)', strtolower($search));
  45.             $this->where('1 = 1 )');
  46.         }
  47.         $this->orderBy("id", "ASC");
  48.         if ($limit != FALSE) {
  49.             $this->limit($limit, $start_limit);
  50.         }
  51.  
  52.         // Result
  53.         if ($num_rows == FALSE) {
  54.             $return = $this->get()->getResult();
  55.         }
  56.         else{
  57.             $return = $this->countAllResults();
  58.         }
  59.  
  60.         return $return;
  61.     }
  62.  
  63.     public function select_table2($parameter, $limit, $num_rows=TRUE){
  64.         $this->select("*");
  65.         $this->table($this->table);
  66.         if ($parameter != FALSE) {
  67.             if ($parameter['id'] != FALSE) {
  68.                 $this->where("md5(id) =", $parameter['id']);
  69.             }
  70.         }
  71.         $this->where('is_deleted', 0);
  72.         if ($limit != FALSE) {
  73.             $this->limit($limit);
  74.         }
  75.         $this->orderBy('id', 'ASC');
  76.  
  77.         // Result
  78.         if ($num_rows === FALSE) {
  79.             $result = $this->get()->getResult();
  80.         }
  81.         else{
  82.             $result = $this->countAllResults();
  83.         }
  84.         return $result;
  85.     }
  86.  
  87.     public function select_table3($parameter, $limit, $num_rows=TRUE){
  88.         $this->select("*");
  89.         $this->table($this->table);
  90.         if ($parameter != FALSE) {
  91.             if ($parameter['id'] != FALSE) {
  92.                 $this->where("md5(id) =", $parameter['id']);
  93.             }
  94.             if ($parameter['is_deleted'] > -1) {
  95.                 $this->where("is_deleted", $parameter['is_deleted']);
  96.             }
  97.         }
  98.         if ($limit != FALSE) {
  99.             $this->limit($limit);
  100.         }
  101.         $this->orderBy('id', 'ASC');
  102.  
  103.         // Result
  104.         if ($num_rows === FALSE) {
  105.             $result = $this->get()->getResult();
  106.         }
  107.         else{
  108.             $result = $this->countAllResults();
  109.         }
  110.         return $result;
  111.     }
  112.  
  113.     public function select_table4($parameter){
  114.         $this->select("
  115.            COALESCE(
  116.                (
  117.                    SELECT
  118.                    images
  119.                    FROM
  120.                    tb_investment_images
  121.                    WHERE
  122.                    id_investment = tb_investment.id
  123.                    AND
  124.                    `tb_investment_images`.`is_deleted` = 0
  125.                    LIMIT 1
  126.                ), NULL
  127.            ) AS images,
  128.            tb_investment.*
  129.        ");
  130.         $this->table($this->table);
  131.         if ($parameter != FALSE) {
  132.             if ($parameter['tahun'] != FALSE) {
  133.                 $this->where("tahun", $parameter["tahun"]);
  134.             }
  135.             if ($parameter['id_sub_sektor'] != FALSE) {
  136.                 $this->where("md5(tb_investment.id_sektor_sub) =", $parameter["id_sub_sektor"]);
  137.             }
  138.             if ($parameter['id_kecamatan'] != FALSE) {
  139.                 $this->where("md5(tb_investment.id_kecamatan) =", $parameter["id_kecamatan"]);
  140.             }
  141.             if ($parameter['minValue'] != FALSE) {
  142.                 $this->where("tb_investment.price >=", $parameter["minValue"]);
  143.             }
  144.             if ($parameter['maxValue'] != FALSE) {
  145.                 $this->where("tb_investment.price <=", $parameter["maxValue"]);
  146.             }
  147.             if ($parameter['search'] != FALSE) {
  148.                 $this->like('(LOWER(tb_investment.name)', strtolower($parameter['search']));
  149.                 $this->orLike('LOWER(tb_investment.name_sektor)', strtolower($parameter['search']));
  150.                 $this->orLike('LOWER(tb_investment.name_sektor_sub)', strtolower($parameter['search']));
  151.                 $this->orLike('LOWER(tb_investment.name_kecamatan)', strtolower($parameter['search']));
  152.                 $this->orLike('LOWER(tb_investment.name_kelurahan)', strtolower($parameter['search']));
  153.                 $this->where('1 = 1 )');
  154.             }
  155.         }
  156.         $this->where('tb_investment.is_deleted', 0);
  157.         $this->groupBy('tb_investment.id');
  158.         $this->orderBy('tb_investment.id', 'ASC');
  159.         $data   = array(
  160.             'data'  => $this->paginate(8, 'investment'),
  161.             "pager" => $this->pager,
  162.         );
  163.         return $data;
  164.     }
  165.  
  166.     public function select_table5($parameter){
  167.         $this->select("COUNT(tb_investment.id) AS count");
  168.         $this->table($this->table);
  169.         if ($parameter != FALSE) {
  170.             if ($parameter['tahun'] != FALSE) {
  171.                 $this->where("tahun", $parameter["tahun"]);
  172.             }
  173.             if ($parameter['id_sub_sektor'] != FALSE) {
  174.                 $this->where("md5(tb_investment.id_sektor_sub) =", $parameter["id_sub_sektor"]);
  175.             }
  176.             if ($parameter['id_kecamatan'] != FALSE) {
  177.                 $this->where("md5(tb_investment.id_kecamatan) =", $parameter["id_kecamatan"]);
  178.             }
  179.             if ($parameter['minValue'] != FALSE) {
  180.                 $this->where("tb_investment.price >=", $parameter["minValue"]);
  181.             }
  182.             if ($parameter['maxValue'] != FALSE) {
  183.                 $this->where("tb_investment.price <=", $parameter["maxValue"]);
  184.             }
  185.             if ($parameter['search'] != FALSE) {
  186.                 $this->like('(LOWER(tb_investment.name)', strtolower($parameter['search']));
  187.                 $this->orLike('LOWER(tb_investment.name_sektor)', strtolower($parameter['search']));
  188.                 $this->orLike('LOWER(tb_investment.name_sektor_sub)', strtolower($parameter['search']));
  189.                 $this->orLike('LOWER(tb_investment.name_kecamatan)', strtolower($parameter['search']));
  190.                 $this->orLike('LOWER(tb_investment.name_kelurahan)', strtolower($parameter['search']));
  191.                 $this->where('1 = 1 )');
  192.             }
  193.         }
  194.         $this->where('tb_investment.is_deleted', 0);
  195.         $this->orderBy('tb_investment.id', 'ASC');
  196.         $data   = $this->get()->getResult();
  197.         return $data;
  198.     }
  199.  
  200.     public function select_table6($parameter, $limit, $order=FALSE, $num_rows=TRUE){
  201.         $this->select("*");
  202.         $this->table($this->table);
  203.         if ($parameter != FALSE) {
  204.             if ($parameter['id'] != FALSE) {
  205.                 $this->where("md5(id) =", $parameter['id']);
  206.             }
  207.             if ($parameter['tahun'] != FALSE) {
  208.                 $this->where("tahun", $parameter['tahun']);
  209.             }
  210.             if ($parameter['is_deleted'] > -1) {
  211.                 $this->where("is_deleted", $parameter['is_deleted']);
  212.             }
  213.         }
  214.         if ($limit != FALSE) {
  215.             $this->limit($limit);
  216.         }
  217.         if ($order != FALSE) {
  218.             $this->orderBy('id', $order);
  219.         }
  220.  
  221.         // Result
  222.         if ($num_rows === FALSE) {
  223.             $result = $this->get()->getResult();
  224.         }
  225.         else{
  226.             $result = $this->countAllResults();
  227.         }
  228.         return $result;
  229.     }
  230.  
  231.     // -------------------------------
  232.     // ----------- Selesai -----------
  233.     // ---------------------------- //
  234.    
  235.  
  236.     // -------------------------------
  237.     // -------------------------------
  238.     //        Serverside Model
  239.     // -------------------------------
  240.     // ---------------------------- //
  241.  
  242.     public function inserts($data = NULL){
  243.         $process = $this->db->table($this->table)->insert($data);
  244.         if ($process) {
  245.             return array("status" => TRUE, "id" => $this->insertID());
  246.         }
  247.         else{
  248.             return array("status" => FALSE);
  249.         }
  250.     }
  251.  
  252.     public function updates($id = NULL, $data = NULL){
  253.         $process = $this->db->table($this->table)->update($data, ['md5(id)' => $id]);
  254.         if ($process) {
  255.             return TRUE;
  256.         }
  257.         else{
  258.             return FALSE;
  259.         }
  260.     }
  261.  
  262.     // -------------------------------
  263.     // ----------- Selesai -----------
  264.     // ---------------------------- //
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement