Welcome Guest, Not a member yet? Register   Sign In
load model multiple times?
#1

[eluser]zoran119[/eluser]
hi everyone,

i don't know much about ci, so i ran into the following problem. i have a model called 'subject' and a model called 'assessment'. now, a subject can have more than one assessment... but i don't know how to load the assessment model multiple times. what i would normal do is something like this (pseudo code):

$assessment_1 = new Assessment(1);
$assessment_2 = new Assessment(2);

where the assessment constructor takes the assessment id and fetches the data from the database into the instance. so then, i can access the methods from the assessment object. for example:

$assessment_1->set_complete();
$assessment_2->delete();

now, when it comes to ci, i have no idea on how to model this subject-assessment relationship.


any help will be appreciated.
#2

[eluser]WanWizard[/eluser]
CI's models are Models in a MVC context, they are not data objects. All CI models are loaded as singletons.

You can fake it, by using
Code:
// load the model class
$this->load->model('assesment');

// we don't need the singleton
unset($this->assesment);

// create the objects
$assessment_1 = new Assessment(1);
$assessment_2 = new Assessment(2);

That will solve your first issue. It will not do anything towards relationships. If you want that, you'll have to look at an ORM. I suggest you check out Datamapper (but he, I'm biased Wink).
#3

[eluser]zoran119[/eluser]
[quote author="WanWizard" date="1295391277"]
You can fake it[/quote]

is this frowned upon? how 'bad' is it to do this?
#4

[eluser]WanWizard[/eluser]
I don't know. If you need it, why not use it?

Alternatively, you can store these models in a folder separate from CI's normal models, and setup an autoloader so the models are loaded automatically when you use new.




Theme © iAndrew 2016 - Forum software by © MyBB