CodeIgniter Forums
Loading objects in model, how do I do this? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Loading objects in model, how do I do this? (/thread-41707.html)



Loading objects in model, how do I do this? - El Forum - 05-14-2011

[eluser]rvbd[/eluser]
Hi All,

I have a design question for the more experienced of you. I'm about to build a software using codeigniter and wanting use full object oriented way of doing it. I previously used codeigniter's 1.9.x MVC framework to some extend, pretty simple just using controller as the logic and models to persist into database.

What I can't find in the forums and Google is a clear explanation of how to load objects in the models. If I have a Car model for argument sake, and it should have multiple Tire objects which I may store in an array, what's the best way of doing so?

Should I include the Tire object directly from the Car model?
I can also do cross loading with the Tire model if I create one, but it won't help with generating multiple instances of Tire.

Forgive me if this has been asked over and over again, I just need a bit of pointer with this.

Thanks in advance.


Loading objects in model, how do I do this? - El Forum - 05-15-2011

[eluser]rvbd[/eluser]
Actually im considering autoloading the model classes so it can be instantiated within any model classes.
Maybe this is a viable solution, I'm open to suggestions though.


Loading objects in model, how do I do this? - El Forum - 05-15-2011

[eluser]InsiteFX[/eluser]
Unless they have changed it you can not do it the normal way, the old way was like this
Code:
class Model_name extends CI_Model {

    // Class Variables.
    // --------------------------------------------------------------------
    private $params = array();

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

    /**
     * __construct()
     *
     * PHP 5+    Constructor.
     *
     * @access    public
     * @return    void
     */
    public function __construct()
    {
        parent::__construct();
    }

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

    /**
     * Initialize()
     *
     * Description:
     *
     * @access    public
     * @return    void
     */
    public function initialize($param1 = NULL)
    {
        // etc.
        $this->params = $param1;
    }

}
You would then need to call the intialize method to intilailize everything.

InsiteFX


Loading objects in model, how do I do this? - El Forum - 05-15-2011

[eluser]darrentaytay[/eluser]
If you want to include external classes, you can just do something like this inside your model:

Code:
require_once(APPPATH . "classes/tire.class.php");

You can then create multiple instances of the object;

Code:
$tires = array(
   new Tire(), new Tire(), new Tire(), new Tire()
);

And there's an array of Tire objects! :-)


Loading objects in model, how do I do this? - El Forum - 05-15-2011

[eluser]rvbd[/eluser]
Ah that helps, thanks! I thought there would be some CI specific way of doing this things in version 2 and above.

I'll assume that php5 autoloading should work as well to require the other models, correct me if I'm wrong.

My aim for this thing is of course to be able to loop the model objects then save it so something like:

foreach ($tires as $tire) {
$tire->do_something_useful(params); //eg. check if tire is broken otherwise don't fix
$tire->save();
}


Loading objects in model, how do I do this? - El Forum - 05-15-2011

[eluser]darrentaytay[/eluser]
If your Tire Class extends the CI Model, I don't see why you couldn't Autoload them ( someone correct me if that's wrong! )