Part of the EllisLab Network
   
2 of 2
2
Simple MY_Model library for db methods
Posted: 09 February 2010 09:00 AM   [ Ignore ]   [ # 11 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3183
Joined  06-11-2007

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.

 Signature 

————————
Blog | Twitter | GitHub | BitBucket
————————-
PyroCMS - open source modular CMS built with CodeIgniter
PancakeApp - Simple, hosted invoicing/w project management

Profile
 
 
Posted: 09 February 2010 09:19 AM   [ Ignore ]   [ # 12 ]  
Grad Student
Avatar
Rank
Total Posts:  44
Joined  11-22-2009

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.

<?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 */ 
 Signature 

Shay Ben Moshe - Web Developer.
Exalted Web (my portfolio - Hebrew)
Zend Certified Engineer

Profile
 
 
Posted: 09 February 2010 01:13 PM   [ Ignore ]   [ # 13 ]  
Lab Assistant
RankRank
Total Posts:  191
Joined  11-09-2007

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:

/**
     * 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

Profile
 
 
Posted: 10 February 2010 07:24 AM   [ Ignore ]   [ # 14 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3183
Joined  06-11-2007
gh0st - 09 February 2010 06:13 PM

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:

/**
     * 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

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().

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) == AND $this->auto_heading == FALSE) ? FALSE TRUE;
                
$this->_set_from_array($table_data$set_heading);
            
}
        } 

Should be fine. Try debugging a little. grin

 Signature 

————————
Blog | Twitter | GitHub | BitBucket
————————-
PyroCMS - open source modular CMS built with CodeIgniter
PancakeApp - Simple, hosted invoicing/w project management

Profile
 
 
Posted: 10 February 2010 09:49 AM   [ Ignore ]   [ # 15 ]  
Lab Assistant
RankRank
Total Posts:  191
Joined  11-09-2007

Ah! Thanks.

Profile
 
 
   
2 of 2
2