Welcome Guest, Not a member yet? Register   Sign In
Best model practise

Hey Smile
I developed until now a more or less huge project with CI. I really love CI but when I look at my code (models) then I see one problem which occurs ever and ever.

For example: the controller "Articles" has the function "view_article", which uses the model "Articles_model". In the model, I call the function "get_article", which selects data from the articles table. Now, the articles table has the field "author_id", a number. I want to return an array to my controller which contains all of the article data, but: i dont want just the number of the author. I want to get the authors name into the array, so i wrote another function in my model: get_auhtor_name(by_id). Everything is fine BUT: the is not the only controller / model which needs the authors name, so I wrote the same (or a bit different) function in many other models which completely is not the DRY method, or?

So here is the question: should I create a model for each table to decrease repetition? I really dont know the best practise.

I hope you'd understand my question (and sorry for the english, german here Wink)


if you have an author model, then you could have one method to return the author name based on id,
and then you can call that wherever you want. Not sure if its considered cool, but Models can call other model methods
- its exactly the same as the controller, just load the model in the constructor and then call like,
$authorname = $this->author_model->getName($id) ;

You should start using Jamie Rumbelow's MY_Model, found here: https://github.com/jamierumbelow/codeigniter-base-model

Also, for best practices check out his book series (3 volumes, 2 published so far, 3rd vol out next month), seen here: https://efendibooks.com/books/codeignite...book/vol-1

MY_Model's documentation is also on the github page, just scroll down. Convention over configuration :o)

Theme © iAndrew 2016 - Forum software by © MyBB