Welcome Guest, Not a member yet? Register   Sign In
why we used Model
#1

[eluser]BD-CI-Programmer[/eluser]
I am using a Controller like this

Code:
<?php
class Bangladesh extends Controller
{
    function Bangladesh ()
    {
        parent::Controller();
    }

function my_function()
{

$this->load->database();
$query = $this->db->query("Query")
if($query->num_rows()>0)
   {
return $query->result();
   }
else
return 0;

   }

}
?>

this code is working. But my questions, why we used Model
#2

[eluser]KeyStroke[/eluser]
Sure it would work, because using Models isn't required in CodeIgniter. Models are used to represent your data structure and deal with the database directly. I prefer to use them because it makes my code more manageable and re-usable.

Right now I can think of 2 big benefits for using Models:

1. Increased re-usability: you can use your models in different applications with minimal modifications. If your queries were in the Controller, you would either re-write them in your new application or have a hard time copying them.

2. In case you change your database, change your tables' structures or do anything else that requires modifying your queries, you would only modify it them in one place (the Model), and the change will be reflected in all controllers that use it.


Therefore, I highly recommend using Models, even if CodeIgniter doesn't require them.
#3

[eluser]BD-CI-Programmer[/eluser]
[quote author="KeyStroke" date="1208786823"]Sure it would work, because using Models isn't required in CodeIgniter. Models are used to represent your data structure and deal with the database directly. I prefer to use them because it makes my code more manageable and re-usable.

Right now I can think of 2 big benefits for using Models:

1. Increased re-usability: you can use your models in different applications with minimal modifications. If your queries were in the Controller, you would either re-write them in your new application or have a hard time copying them.

2. In case you change your database, change your tables' structures or do anything else that requires modifying your queries, you would only modify it them in one place (the Model), and the change will be reflected in all controllers that use it.


Therefore, I highly recommend using Models, even if CodeIgniter doesn't require them.[/quote]

Thanks for your nice reply. If CodeIgniter doesn't require Models then i say it is not perfect MVC pattern. Am i right??
#4

[eluser]KeyStroke[/eluser]
[quote author="Nahid" date="1208787140"]Thanks for your nice reply. If CodeIgniter doesn't require Models then i say it is not perfect MVC pattern. Am i right??[/quote]
I wouldn't say that. Here's a quite from the user guide:
Quote:CodeIgniter has a fairly loose approach to MVC since Models are not required.

So, in my opinion, just because it doesn't force you to use Models, doesn't mean it's an incorrect implementation. It's only incorrect when YOU don't use Models.
#5

[eluser]BD-CI-Programmer[/eluser]
Thanks for your nice reply..




Theme © iAndrew 2016 - Forum software by © MyBB