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

[eluser]Shay Falador[/eluser]
You are completely wrong!
I used return false ONLY in 1-row data.
Both in get_all and get_all_where I return the query.
#12

[eluser]Phil Sturgeon[/eluser]
Right, so one returns false or data (as I said) and rest return the query object - which should not exist outside of the model.

I meant to "I strongly suggest you use Jamies..." to gh0st as for him it will be more logical. For you it is different as you wrote it so for you it makes the most sense.
#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 */
#14

[eluser]gh0st[/eluser]
Hi.

@phil -- Thanks for the answer, I appreciate it. I'll just use sizeof to find out the num_rows() on a query.

There's another problem. When I try to run the this->Table->generate($Query_object_goes_here) using the MY_Model, you can't use query.

There are several other instances where I need direct access to the query object, the problem is that MY_Model always returns an array, never an object.

If we look at Jamie Rumbelow's MY_Model get_all() method:

Code:
/**
     * Get all records in the database
     *
     * @return array
     * @author Jamie Rumbelow
     */
    public function get_all() {
        return $this->db->get($this->table)
            ->result();
    }

It runs the result() method, which then returns an array.

There are times when I need to use the object, not an array; for example -- the Table->generate() needs an object, and it won't work on an array.

Is there a way where I can grab the result object, without physically hacking Jamie's code?

Thanks
#15

[eluser]Phil Sturgeon[/eluser]
[quote author="gh0st" date="1265757223"]Hi.

@phil -- Thanks for the answer, I appreciate it. I'll just use sizeof to find out the num_rows() on a query.

There's another problem. When I try to run the this->Table->generate($Query_object_goes_here) using the MY_Model, you can't use query.

There are several other instances where I need direct access to the query object, the problem is that MY_Model always returns an array, never an object.

If we look at Jamie Rumbelow's MY_Model get_all() method:

Code:
/**
     * Get all records in the database
     *
     * @return array
     * @author Jamie Rumbelow
     */
    public function get_all() {
        return $this->db->get($this->table)
            ->result();
    }

It runs the result() method, which then returns an array.

There are times when I need to use the object, not an array; for example -- the Table->generate() needs an object, and it won't work on an array.

Is there a way where I can grab the result object, without physically hacking Jamie's code?

Thanks[/quote]

You are getting a little confused here. We are talking about 3 different things:

$query: this is the usual name for the database object. Table library really does not want this!

$query->result(): This is an array of objects, this is what MY_Model returns.

$query->result_array(): This is an array of arrays (a multi-dimensional array) which works with Table in the exact same way as $query->result().

Code:
function generate($table_data = NULL)
    {
        // The table data can optionally be passed to this function
        // either as a database result object or an array
        if ( ! is_null($table_data))
        {
            if (is_object($table_data))
            {
                $this->_set_from_object($table_data);
            }
            elseif (is_array($table_data))
            {
                $set_heading = (count($this->heading) == 0 AND $this->auto_heading == FALSE) ? FALSE : TRUE;
                $this->_set_from_array($table_data, $set_heading);
            }
        }

Should be fine. Try debugging a little. :-)
#16

[eluser]gh0st[/eluser]
Ah! Thanks.




Theme © iAndrew 2016 - Forum software by © MyBB