Welcome Guest, Not a member yet? Register   Sign In
Simple MY_Model library for db methods
#13

[eluser]Shay Falador[/eluser]
I see now Phil, thanks for the comments Smile
Well, I made a big change in order to make it simpler, I guess I won't change it anymore.

Code:
<?php

class MY_Model extends Model {
    
    /**
     * Holds which database table are we handling
     *
     * @var    string
     */
    protected $table;
    
    /**
     * Holds the primary key of the table
     *
     * @var    string
     */
    protected $primary_key = 'id';
    
    /**
     * This is an array in which you may set which variables
     * you don't want to clean using the clean_data method
     * when calling update or delete functions
     *
     * @var    array
     */
    protected $dont_clean = array();
    
    function MY_Model() {
        parent::Model();
        $this->load->database();
    }
    
    /**
     * Cleans data from an array or string, including:
     * triming spaces, stripping slashes and html special chars
     *
     * @param    array/string $data
     * @return    array/string
     */
    function clean_data(&$data) {
        if(is_array($data)) {
            foreach($data as $key => &$val) {
                if(!in_array($key, $this->dont_clean)) {
                    $this->clean_data($val);
                }
            }
        }
        else {
            $data = htmlspecialchars(stripslashes(trim($data)), ENT_QUOTES);
        }
        return $data;
    }
    
    /**
     * Set the order by for the next query
     *
     * @param    string $order_by
     * @param    string $order_direction
     * @return    object $this
     */
    function order($order_by, $order_direction = 'ASC') {
        $this->db->order_by($order_by, $order_direction);
        return $this;
    }
    
    /**
     * Set the limit for the next query
     *
     * @param    int $value
     * @param    int $offset
     * @return    object $this
     */
    function limit($value, $offset = false) {
        $this->db->limit($value, $offset);
        return $this;
    }
    
    /**
     * Sets where data for the next query
     * If only the first parameter is set it sets it
     * as the value of primary key
     * Else it uses the first parameter as key and second as value
     *
     * @param    string $by
     * @param    string $value
     * @return    object $this
     */
    function where($by, $value = false) {
        if($value) {
            $this->db->where($by, $value);
        }
        else {
            $this->where($this->primary_key, $by);
        }
        return $this;
    }
    
    /**
     * Gets 1 row as an object from the database
     * It may also recive where data as the where method
     * Returns false if no row found
     *
     * @param    string $by
     * @param    string $value
     * @return    object/false
     */
    function get($where_key = false, $where_value = false) {
        if($where_key) {
            $this->where($where_key, $where_value);
        }
        $query = $this->db->get($this->table);
        if($query->num_rows > 0) {
            $result = $query->row();
            return $result;
        }
        return false;
    }
    
    /**
     * Gets all of the rows from a database
     * It may also recive where data as the where method
     *
     * @param    string $by
     * @param    string $value
     * @return    object
     */
    function all($where_key = false, $where_value = false) {
        if($where_key) {
            $this->where($where_key, $where_value);
        }
        return $this->db->get($this->table);
    }
    
    /**
     * Set the select for the next query
     *
     * @param    string $select
     * @param    string $escape
     * @return    object
     */
    function select($select = '*', $escape = NULL) {
        $this->db->select($select, $escape);
        return $this;
    }
    
    /**
     * Inserts a row into a database table
     * If the second parameter is true, uses clean_data method
     * on the first parameter data
     * Returns the inserted row id
     *
     * @param    array/object $data
     * @param    bool $clean
     * @return    int
     */
    function insert($data, $clean = true) {
        if($clean) {
            $this->clean_data($data);
        }
        
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }
    
    /**
     * Updates in a database table
     * If the second parameter is true, uses clean_data method
     * on the first parameter data
     * Returns the affected rows
     *
     * @param    array/object $data
     * @param    bool $clean
     * @return    int
     */
    function update($data, $clean = true) {
        if($clean) {
            $this->clean_data($data);
        }
        
        $this->db->update($this->table, $data);
        return $this->db->affected_rows();
    }
    
    /**
     * Deletes in a database table
     * It may also recive where data as the where method
     * Returns the affected rows
     *
     * @param    string $by
     * @param    string $value
     * @return    int
     */
    function delete($where_key = false, $where_value = false) {
        if($where_key) {
            $this->where($where_key, $where_value);
        }
        
        $this->db->delete($this->table);
        return $this->db->affected_rows();
    }
    
}

/* End of file MY_Model.php */
/* Location: ./application/libraries/MY_Model.php */


Messages In This Thread
Simple MY_Model library for db methods - by El Forum - 01-30-2010, 12:50 PM
Simple MY_Model library for db methods - by El Forum - 02-02-2010, 07:03 AM
Simple MY_Model library for db methods - by El Forum - 02-02-2010, 08:16 AM
Simple MY_Model library for db methods - by El Forum - 02-02-2010, 11:52 AM
Simple MY_Model library for db methods - by El Forum - 02-03-2010, 05:38 AM
Simple MY_Model library for db methods - by El Forum - 02-03-2010, 04:17 PM
Simple MY_Model library for db methods - by El Forum - 02-04-2010, 06:41 AM
Simple MY_Model library for db methods - by El Forum - 02-05-2010, 08:45 AM
Simple MY_Model library for db methods - by El Forum - 02-05-2010, 09:46 AM
Simple MY_Model library for db methods - by El Forum - 02-09-2010, 05:56 AM
Simple MY_Model library for db methods - by El Forum - 02-09-2010, 06:19 AM
Simple MY_Model library for db methods - by El Forum - 02-09-2010, 07:00 AM
Simple MY_Model library for db methods - by El Forum - 02-09-2010, 07:19 AM
Simple MY_Model library for db methods - by El Forum - 02-09-2010, 11:13 AM
Simple MY_Model library for db methods - by El Forum - 02-10-2010, 05:24 AM
Simple MY_Model library for db methods - by El Forum - 02-10-2010, 07:49 AM



Theme © iAndrew 2016 - Forum software by © MyBB