• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
deadly easy model for sql- caholic

#1
[eluser]jegbagus[/eluser]
hi there,
actually i don't really like to learn more about active record, data mapper etc.
yes, i'am a sql caholic. love to use sql to access database.

here i present you the most easiest way to use your query.
hope this will make your codeigniter more easy to use.

explanation :
there only 3 command you need to remember (and you should can do almost everything with your database)
- fetch_single_row (select only one row record)
- fetch_multi_row (select more than one row record)
- and execute_dml
then map your parameter into array (if there is parameter), and choose above command
Code:
function insert_info($title, $desc_english, $desc_indonesia){        
        $sql = "insert into information values (null,?,?,?)";
        $data = array($title,$desc_english,$desc_indonesia);
        
        $this->execute_dml($sql,$data);
}

Example how to use :
Code:
<?php if(!defined('BASEPATH')) exit('Hack attemp?');

class Info_Model extends Ext_Model{
    
    function __construct(){
        parent::__construct();
    }
    
    function get_info(){
        $sql = "select * from information order by infoid desc";        
        return $this->fetch_multi_row($sql);
    }
    
    function get_detail_info($id){
        $sql = "select * from information where infoid like ? ";
        $data = array($id);        
        return $this->fetch_single_row($sql, $data);
    }
    
    function insert_info($title, $desc_english, $desc_indonesia){        
        $sql = "insert into information values (null,?,?,?)";
        $data = array($title,$desc_english,$desc_indonesia);
        
        $this->execute_dml($sql,$data);
    }
    
    function update_info($title, $desc_english, $desc_indonesia,$id){
        $sql = "update information set title = ? , desc_english = ?, desc_indonesia    = ? where infoid = ?";
        $data = array($title,$desc_english,$desc_indonesia,$id);
        
        $this->execute_dml($sql, $data);
    }
    
    function delete_info($id){
        $sql = "delete from information where infoid = ? ";
        $data = array($id);
        
        $this->execute_dml($sql, $data);
    }
}

Ext_Model.php (extend of model library (place at your library folder))
Code:
<?php if(!defined('BASEPATH')) exit("Hack attempt?");

/**
*
*  @author Jeg_bagus
*
*  @property CI_DB_driver $db
*  @property CI_Loader $load
*
*/

class Ext_Model extends Model{

    function __construct(){
        parent::__construct();
    }

    /*
     *     fetch single row from database
     *
     *     @return    array
     */
    protected function fetch_single_row($sql, $bind_value = FALSE, $dbcache = FALSE){
        if($dbcache) $this->db->cache_on();

                $query = ($bind_value) ? $this->db->query($sql,$bind_value) : $this->db->query($sql);

        if ($query->num_rows() == 0) return NULL;
        else return $query->row_array();
    }

    /*
     *     fetch multi row from database
     *
     *     @return    array : array of array
     */
    protected function fetch_multi_row($sql, $bind_value = FALSE, $dbcache = FALSE){
        if($dbcache) $this->db->cache_on();

        $query = ($bind_value) ? $this->db->query($sql,$bind_value) : $this->db->query($sql);

        if ($query->num_rows() == 0) return NULL;
        return $query->result_array();
    }

    /*
     *     Execute DML (insert, update, delete)
     *
     *     @return    int : number of affected rows
     */
    protected function execute_dml($sql, $bind_value = FALSE){
        $query = ($bind_value) ? $this->db->query($sql,$bind_value) : $this->db->query($sql);

        return $this->db->affected_rows();
    }

    /*
     *     Determine command type
     *
     *     @return    string : (select, insert, update, delete)
     */
    protected function type_command($command)
    {
        $result = explode(' ', trim($command));
        return  strtolower($result[0]);
    }
}


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.