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

[eluser]prezet[/eluser]
How is best to handle traditional PHP objects in CI?

For example, say I have a database table full of information on cats (or dogs, whatever), and when I pull out that data I populate it as objects like so:

Code:
$cats = array();

foreach( $cats_data as $cat_data ){
    $cats[] = new Cat($cat_data['cat_id']);
}

So a very basic example of traditional PHP objects. Should I work with CI in the same way? Or is there another, preferred method?

Please go easy, I'm a front end developer trying to tackle this stuff Wink
#2

[eluser]Rey Philip Regis[/eluser]
Im new here in CodeIgniter, I want also to know what's the answer to that question. But I think its still the same there's no CI way on how to do that.
#3

[eluser]dcunited08[/eluser]
How are you getting the data from the database? What does your Cat object do? Where are you using this code? The 'CI way' would be to use the builtin database drivers and load the objects from that.
#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.
#5

[eluser]dcunited08[/eluser]
You may want to look at ADODB's ActiveRecord

It would allow you to define as follows:
Code:
class Cat extends ADODB_active_record{
    var $_table = 'Cats';
    var $_prik = array('id');

}

and then you can call it like:
Code:
$cat = new Cat();
$cats = $cat->find();
$cats[1]->name = 'fifi';
$cats[1]->save();
OR
$cat2 = new Cat();
$cat2->load('Sam');
//do stuff with $cat2

The ActiveRecord portion of CI has a lot of different functionality and I wish there was a more blended solution, like being able to define what the SQL statement was to be for the classes in ADODB or being able to define classes and not use the standard class in CI.

You would want to make sure that the object uses the data you loaded, not reload all the data.
#6

[eluser]prezet[/eluser]
OK, thanks, I'll look into that...

as for your mention of :
Quote:The ‘CI way’ would be to use the builtin database drivers and load the objects from that.

Could you expand on what you meant by that?

Thanks
#7

[eluser]dcunited08[/eluser]
[quote author="prezet" date="1225415031"]OK, thanks, I'll look into that...

as for your mention of :
Quote:The ‘CI way’ would be to use the builtin database drivers and load the objects from that.

Could you expand on what you meant by that?

Thanks[/quote]

By that I simply meant that you would do the
Code:
$this->db->query($SQL);
and then work through it as you showed in your code. You may also take a look at DataMapper or IgnitedRecord. I just started looking into these two after answering your request. The reason to look at these is they use the same database drivers so you are not needing a totally different db library, just an extension of it.
#8

[eluser]Pascal Kriete[/eluser]
Can you clarify what you're trying to achieve? I don't get the whole 'abstract to another class' thing. Most of the time one request won't do multiple things to a cat, will it?

I would personally just modify the cats directly with model calls:
Code:
// Model
// ...
return $query->result();

And then play with the cats:
Code:
// An awful example...
$cats = $this->cat_model->get_cats();

foreach($cats as $cat)
{
    if ($cat->name == 'Old Crumples')
    {
        $moto = 'I can haz ...';
        $this->cat_model->update_moto($cat->id, $moto);
    }
}

And as a side note, this piece of code:
Code:
foreach( $query->result_array() as $row )
{
    $result[] = $row;
}

It's merely copying an array:
Code:
$result = $query->result_array();




Theme © iAndrew 2016 - Forum software by © MyBB