Welcome Guest, Not a member yet? Register   Sign In
Variables in Model Classes
#1

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!
Reply
#2

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

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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB