Welcome Guest, Not a member yet? Register   Sign In
Loading objects in model, how do I do this?
#1

[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.
#2

[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.
#3

[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
#4

[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! :-)
#5

[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();
}
#6

[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! )




Theme © iAndrew 2016 - Forum software by © MyBB