Welcome Guest, Not a member yet? Register   Sign In
Models and Libraries
#1

[eluser]foyer[/eluser]
Hey guys, quick question. I'm trying to understand the difference between a Library and a Model and when I should use them respectively. (As far as my own custom Models and Libraries go)
#2

[eluser]Sbioko[/eluser]
Models are needed for interaction with database. Models allow you to not work with database in controllers. For example:
Code:
<?php
class SomeController extends Controller {
//...
    public function test() {
        $this->load->model('Posts');
        $posts = $this->posts->get_posts();
        $this->load->view('posts', $posts);
    }

}
I hope you understood it :-) If not, write me.
#3

[eluser]foyer[/eluser]
That makes sense. And libraries can be used for say, frequently used functions that are be used between controllers. If I wanted to connect to my DB in a library can I use a model?
#4

[eluser]Sbioko[/eluser]
Hm, CI automatically connects to DB. It is a bad example :-) Usually, models are used only in controllers. In other words, models are created to interact with the database. Still have questions? Ask! :-)
#5

[eluser]foyer[/eluser]
oops not connect to DB, I mean run queries
#6

[eluser]Sbioko[/eluser]
In library? No, you should not use models in libraries.
#7

[eluser]foyer[/eluser]
Ok so what is the difference between a Library and a Model? I guess I'm still not clear on this
#8

[eluser]must[/eluser]
[quote author="Sbioko" date="1268963905"]In library? No, you should not use models in libraries.[/quote]
how come? a lot of libraries that are developed by the codeigniter community uses models to interact with the database (even if they were not, why shouldn't we use models in libraries? bad practice or what? )
#9

[eluser]Ben Edmunds[/eluser]
It is perfectly fine to use a model from a library, even recommended. I've also seen queries preformed directly from libraries but personally prefer to still use a model from my libraries as IMHO it's better practice.




Theme © iAndrew 2016 - Forum software by © MyBB