Welcome Guest, Not a member yet? Register   Sign In
question on MVC
#1

[eluser]Unknown[/eluser]
Hello


i'm quite new to CI, but i've a question regarding my first site i'm writing.


Basically, I've got a USER and a TEMPLATE class

so i wrote a Model 'User' and a model 'Template'.
In my User model, i have a field 'template' which in the db is stored as the template_id.
Now, what i want, is for the 'template' field to not contain the template_id but contain an instance of the template class with all my fields.

Now, what i did, is in my 'User' model, in the 'load_entry' method,
Code:
$template_id = $row->template;
            $this->load->model('Model_template', 'template' . $template_id);
            $this->('template' . $template_id)->load_entry($template_id);
            $this->template = $this->('template' . $template_id)->getInstance();

My little issue, concerns the CodeIgniter way of loading a model ...
personnaly i would have done so that the load->model method returns an instance of the model, ...
So what i did was add a getInstance() method in all my models (i should modify the model class ^^ so they inherit it ^^)

it basically does a return $this;




I haven't found much info about my type of issue, maybe i've done what everyone else is doing ? or is there a better CI way to do this ?

Thank you.
#2

[eluser]Pygon[/eluser]
Maybe I'm confused, but I'm not exactly sure what it is that you're asking?

Objects can be passed to functions, by reference or copy, the same way that variables are. Is that what you're asking?
#3

[eluser]Unknown[/eluser]
not really no.

Sorry if my english isn't very clear Smile i'm from a country you probably never heard off ^^

Basically, i've got 2 classes : User and Template.

in my "normal code" (read: without CI)

i read a given user_id, read it from db, read the template_id in the user's row.
then i create a new Template object with that template_id and i store the object's reference in the user object.

so from my main app, i can do $currUser->template->getVar();



Now my problem with CI and MVC, is that i now have a User Model and a Template model.


inside the user model object, i should create a new template object and store the reference in the user model object.
so i would have to do a :

$this->template = new Template($template_id);

but that's not very CI .. how would i go about doing that ?

Only way i found is doing :
$this->load->library('TemplateLib');
$this->template = TemplateLib;
$this->template->load($template_id);


But then if i get a 1-to-many relationship ? do i do these three lines in a loop ?
Is there no other way to instantiate objects in CI ???
#4

[eluser]Pygon[/eluser]
Well, I think your issue lies more with what a "library" is, than loading/instantiating objects. It seems to me that instead of instantiating template "libraries" (which I assume is really just a container for template information and may have some functions), you should use it as it is intended -- a library, being a collection of functions that manipulate/return data and data holders.

I would either:

1) Convert your Template class into a Model which creates and manages an array of template objects, including the manipulation of their data. This is more in-line with MVC and the way it should be done.

2) Seperate any logic from your Template class into an actual library, declare the class within the library file (or as an include), and simply load the library once using it to instantiate object. Example:
Code:
$this->load->library('TemplateLib','template');
$tpl1 = $this->template->new_template($user_tpl_id); //Returns a new template object

-- OR --

3) Keep trying to write non-MVC code and stick it into code igniter.


Personally, I would go with number one.




Theme © iAndrew 2016 - Forum software by © MyBB