CodeIgniter Forums
Variables in Model Classes - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: Variables in Model Classes (/showthread.php?tid=1187)



Variables in Model Classes - olivebean - 02-17-2015

Hey there! New to CodeIgniter and trying my best to keep this project streamlined and within the "best practices" parameters as much as possible.

My question: Is is ok to have variables in a model class that are not in the database? I Category and Page model classes, and in my controller I'd like to create an array of Page objects in the Category class to use in the View.

Thanks!


RE: Variables in Model Classes - colonelclick - 02-18-2015

There is no hard and fast rule, as far as CI is concerned.

My own practice is to use a Library for derived or run-time variables, with the Library calling the Model, which only contains variables that correspond to the database.

I did this for logical separation, but also convenience. It's very easy to save or load a Model that has a 1:1 between variables and db fields. From a programming standpoint I just call the Library from my Controller and it is up to the Library to know whether the variable comes from the Library or the Model.

I took inspiration for this methodology from the Ion-Auth library. You'll find some helpful magic methods in his Library files for doing this.


RE: Variables in Model Classes - casa - 04-05-2015

Hello,
I advise you for your var to declare its like that :
PHP Code:
private $var1 
and then, to make the getter or setter that you need. You will improve your security if you just need read some variables.
Have a good day.