Welcome Guest, Not a member yet? Register   Sign In
Using CI with traditional PHP Objects
#4

[eluser]prezet[/eluser]
As this is a simplified example assume I have a model called cats_model.php with the following method, which returns an associate array of the cats data

Code:
function get_cats( )
{
    $result = FALSE;
                
    $query = $this->db->query('SELECT * FROM cats');
        
    foreach( $query->result_array() as $row )
    {
        $result[] = $row;
    }
        
    return $result;
}

so in my controller I use:

Code:
function mycats
{
   // Load Model
    $this->load->model('cats_model');
    
    // Get Class
    require_once APPPATH . '/classes/Cat.php';

    $cats = array();

    if( $cats_data = $this->cats_model->get_cats() )
    {                
        foreach($cats_data as $cat_data)
    {            
        $cats[] = new Cat($cat_data['cat_id']);
    }
    }

    // Do something with the cats array of objects etc...
    
}

So I pass each ID into the Cat class, which then grabs the data from the database related to that ID. Or at least thats the plan. I haven't actually written that class yet.

The Cat object just has simple methods like get_cat_name(), set_cat_name(), get_cat_colour() etc... nothing elaborate. But assume I have multiple cat entries in my database i.e. one for a ginger cat called Molly and one for a black cat called Sam...

I'm assuming I'm not doing it the correct CI way, so any advice on how to structure this would be helpful.


Messages In This Thread
Using CI with traditional PHP Objects - by El Forum - 10-29-2008, 01:19 PM
Using CI with traditional PHP Objects - by El Forum - 10-29-2008, 02:16 PM
Using CI with traditional PHP Objects - by El Forum - 10-30-2008, 12:10 PM
Using CI with traditional PHP Objects - by El Forum - 10-30-2008, 12:44 PM
Using CI with traditional PHP Objects - by El Forum - 10-30-2008, 01:18 PM
Using CI with traditional PHP Objects - by El Forum - 10-30-2008, 02:03 PM
Using CI with traditional PHP Objects - by El Forum - 10-30-2008, 02:14 PM
Using CI with traditional PHP Objects - by El Forum - 10-30-2008, 02:35 PM



Theme © iAndrew 2016 - Forum software by © MyBB