Update AS Method

The update_as method is like update method but with unescaped key value pair. This method suitable for updating column to column in a table or suitable for another advanced SQL UPDATE operation. This method is available from SEME Framework version 3.1.0.

Caution

The escape method ($this->db->esc('VALUE')) maybe required for preventing SQL injection .

Basic Usage

Here is the basic usage update_as method from $db property on SENE_Model class.

$this->db->update_as(string $table_name, array $data_update, [bool $is_debug=0]): bool

Parameters

This method has 2 required parameters and 1 optional parameter.

$table_name

The $table_name refers to the name of the table to which the data is to be updated.

$data_update

The $data_update value can contain key value pair in array and automatically escaped. The key refer to column name of the table and the value refer to value that will be inserted. This value supported MySQL builtin functions and values, such as:

  • NOW()
  • NULL

$is_debug

The $is_debug parameter is a marker (flag) to enable debug mode. The value of this parameter can be filled with int 1 to enable debug mode and display the query to be processed. Fill it with another value to not enable debug mode. In debug mode, there will be no query execution process to the database system.

Example

On this example, we want to update value a column named revision_count with it is own increment. Here is the examples usage update_as in a model class.

<?php
class Blog_Model extends SENE_Model{
  var $tbl = 'blog';
  var $tbl_as = 'b';
  public function __construct(){
    parent::__construct();
  }
  public function update($id,$du){
    $du['revision_count'] = '`revision_count`+1';
    $this->db->where("id", $id);
    $this->db->update_as($ths->tbl,$du);
  }
}

Controller class example

And then this is controller class example using the model class example

class Blog extends SENE_Controller{
  public function __construct(){
    parent::__construct();
    $this->load('blog_model','bm'); #class scope model
  }
  public function index(){
    $id = 1;
    $du = array();
    $du['title'] = "This is new title of this blog!";
    $res = $this->bm->update($id,$du); //call the method on the model
    if($res){
      echo 'Success';
    }else{
      echo 'failed';
    }
  }
}

Generated SQL

Here is the generated SQL command from method update_as.

UPDATE
  `d_order`
SET
  title = "This is new title of this blog!",
  revision_count = `revision_count`+1
WHERE
  `id` = 1;