Phalcon Batch Insert Update

phalcon view into variable

kalo bisa di dalam loop jgn ada sql agar eksekusi query cepet dan koneksi g bolak balik DB - PHP tiap loop nya, (it's killer algoritm dude). istilahnya batch insert/update. berikut caranya (udah pernah saya pake)...


Library model phalcon
// taruh di model/Batchquery.php
  class Batchquery extends \Phalcon\Mvc\Model
  { 
    public $table = null;
      public $columns = array();
      public $data = array();
      public $insert_string = '';
      public $update_string = '';

      public function onConstruct($table = false)
      {
          if ($table) {
              $this->table = $table;
          }
          $di = Phalcon\DI::getDefault();
          $this->db = $di->get('db');
      }



      public function insert()
      {
          $this->_validate();
          $this->_stringify();
          //echo $this->insert_string;
          try {
              $result = $this->db->execute($this->insert_string);
          } catch (Exception $e) {
              echo 'ngeror:'.$e->getMessage();
          }
          $this->insert_string = '';
          return $result;
      }
      
      public function updates()
      {
          // $this->_validate();
          $this->_stringify_update();
          //echo $this->insert_string;
          try {
              $result = $this->db->execute($this->update_string);
          } catch (Exception $e) {
              echo 'ngeror:'.$e->getMessage();
          }
          $this->update_string = '';
          return $result;
      }

      private function _stringify_update(){
          $this->update_string=$this->data;
      }
      
      private function _stringify()
      {
          $columns = sprintf('`%s`', implode('`,`', $this->columns));
          $str = '';
          foreach ($this->data as $values) {
              foreach ($values as &$val) {
                  if (is_null($val)) {
                      $val = 'NULL';
                      continue;
                  }
                  if (is_string($val)) {
                      $val = "'".$val."'";
                  }
              }
              $str .= sprintf('(%s),', implode(',', $values));
          }
          $str = rtrim($str, ',');
          $query = sprintf("INSERT IGNORE INTO `%s` (%s) VALUES %s",
              $this->table,
              $columns,
              $str
          );
          $this->insert_string = $query;
      }

      private function _makeValues($item)
      {
          print_r($item);
      }
      
      private function _validate()
      {
          if ($this->table == null) {
              throw new \Exception('Batch Table must be defined');
          }
          if (count($this->columns) == 0) {
              throw new \Exception('Batch Columns cannot be empty');
          }
          $required_count = count($this->columns);
          foreach ($this->data as $value) {
              if (count($value) !== $required_count) {
                  throw new \Exception('Batch Data must match the same column count of ' . $required_count);
              }
          }
      }
  }

  
  

Implementasi di controller
// taruh di controller/BankController.php   
class BankController extends \Phalcon\Mvc\Controller
{
    public function updateSaldoAction($bank,$bln){
      $batch = new Batchquery(); //penting
      $bank=SaldoBank::find("bank='mandiri' and tgl like '$bln%' order by tgl asc");
      foreach ($bank as $k => $v) {
        $saldo=$v->nominal;

        //ini yang penting
        $batch->data="update saldo_bank set saldo='$saldo' where tgl='$v->tgl' and keterangan='$v->keterangan' and nominal='$v->nominal' and bank='mandiri'";
        $batch->updates();
        //sampai sini
      }
      die();
    }

    public function parsingAction($bank,$bln='',$saldo=''){
      $batch = new Batchquery(); //penting
      $bri_arr=SaldoBank::find("bank='bri' and tgl like '$bln%' order by tgl asc");
    foreach ($bri_arr as $k => $v) {
      //ini yang penting
      $batch->columns = ['tgl', 'keterangan','in_out','nominal','saldo','bank'];
          $obj=[$tgl,$keterangan,$in_out,$nominal,$saldo,$bank];
      $batch->data = [$obj];
          $batch->insert();
          //sampai sini
    }
    die();
    }
}
  
  

masih bingung boleh kontak ane gan... 08975835238
Shorten URL :
Share this Story: