Using CI with traditional PHP Objects |
[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(); 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 ![]()
[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.
[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.
[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( ) so in my controller I use: Code: function mycats 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.
[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{ and then you can call it like: Code: $cat = new Cat(); 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.
[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
[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);
[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 And then play with the cats: Code: // An awful example... And as a side note, this piece of code: Code: foreach( $query->result_array() as $row ) It's merely copying an array: Code: $result = $query->result_array(); |
Welcome Guest, Not a member yet? Register Sign In |