Welcome Guest, Not a member yet? Register   Sign In
3 useful method extend CI DB
#1

[eluser]vps4[/eluser]
feature & usage:

get_var() get_row() get_col()
these method like wordpress DB.

Code:
$id = $this->db->get_var('SELECT COUNT(id) FROM table');
echo $id; // result: 100

$row = $this->db->get_row('SELECT * FROM table WHERE id = 1');
print_r($row); // result: object(1, andy, 2009-00-00)

$col = $this->db->get_col('SELECT id FROM table');
print_r($col); // result: array(1,2,3,4,5....)


just open /* Location: ./system/database/DB_driver.php */

add this codes:
Code:
// --------------------------------------------------------------------

    /**
     * Returns an object of row
     *
     * @author  http://www.21andy.com/
     * @access  public
     * @return  object
     */
    function get_row($sql = null, $binds = FALSE) {
        $query = $this->query($sql, $binds);
        $r = null;
        if ($query->num_rows() > 0) {
            $r = $query->row();
        }
        $query->free_result();
        return $r;
    }

    // --------------------------------------------------------------------

    /**
     * Returns an value of field
     *
     * @author  http://www.21andy.com/
     * @access  public
     * @return  string
     */
    function get_var($sql = null, $binds = FALSE, $x = 0) {
        $query = $this->query($sql, $binds);
        $r = null;
        if ($query->num_rows() > 0) {
            $row = $query->row();
            $values = array_values(get_object_vars($row));
            $r = (isset($values[$x]) && $values[$x]!=='') ? $values[$x] : null;
        }
        $query->free_result();
        return $r;
    }

    // --------------------------------------------------------------------

    /**
     * Returns an array of col value
     *
     * @author  http://www.21andy.com/
     * @access  public
     * @return  array
     */
    function get_col($sql = null, $binds = FALSE, $x = 0) {
        $query = $this->query($sql, $binds);
        $r = array();
        if ($query->num_rows() > 0) {
            $i = 0;
            foreach ($query->result() AS $row) {
                $values = array_values(get_object_vars($row));
                $r[$i] = $values[$x];
                ++$i;
            }
        }
        $query->free_result();
        return empty($r) ? null : $r;
    }

enjoy!
#2

[eluser]sophistry[/eluser]
seems useful to have these "macro" functions. however, rather than putting them in the db class i would put these in your own base Model class and extend all models from there.

cheers.

EDIT: changed for clarity on base model name
#3

[eluser]Dan Horrigan[/eluser]
You should NEVER add code into the base classes. You should, instead, EXTEND them. The User Guide says you cannot extend the database classes. However, there is a way. Here is the article explaning how to do it: http://codeigniter.com/wiki/Extending_Database_Drivers/

If you edited your base files as you have above, then anytime you upgrade CI, you will have to go in and re-implement them. Also, extending the classes makes it much easier to use them on other projects.




Theme © iAndrew 2016 - Forum software by © MyBB