Welcome Guest, Not a member yet? Register   Sign In
how to use model?
#1

[eluser]syntaxerror[/eluser]
hi,
how to use model in developing application?
i know how to use controller and view, and my script is all controller and view.

any tutorial or just basic tutorial in using model?

thanks
#2

[eluser]Colin Williams[/eluser]
The secret to my success was reading the User Guide.
#3

[eluser]Frank Berger[/eluser]
Quoting from the MVC entry on Wikipedia:

Quote:[MVC] As a design pattern
MVC encompasses more of the architecture of an application than is typical for a design pattern.

Model
The domain-specific representation of the information on which the application operates. Domain logic adds meaning to raw data (e.g., calculating whether today is the user's birthday, or the totals, taxes, and shipping charges for shopping cart items).
Many applications use a persistent storage mechanism (such as a database) to store data. MVC does not specifically mention the data access layer because it is understood to be underneath or encapsulated by the Model.

View
Renders the model into a form suitable for interaction, typically a user interface element. Multiple views can exist for a single model for different purposes.

Controller
Processes and responds to events, typically user actions, and may invoke changes on the model.

Or in other words: The model has all the functionality, intelligence and integrity to handle all the (business) logic and data flow. All of that is now in your controllers (as you're not using models). Strictly spoken, a controller in CI wouldn't even need a database connection. I see this as commodity to the developers.

Your controllers will get very small, most of the data-handling stuff is done in the model. Advantages are that you have more portable code (if you agree on a common interface for your models, like for example always having a get, getlist, save, exists method) your controllers will all become pretty much the same, just changing names and model-calls, enabling you to create a controller much faster. As well with using models the Model-intelligence can be shared across several Controllers, without implementing a function twice.

Or as an alternative view: if you ever needed to copy-paste a piece of code (or logic) from one controller to another without changing it, you should have implemented that piece of code (or logic) as a model or as a library

The MVC pattern enables you to think in separated Development steps. You can think about the Presentation, the data and the interaction logic in separate Files, Logic, Steps and People/Teams if you must.

I usually start with the model, and then quickly implementing a test-controller to move on to implementing the needed controller(s) with dummy views, doing the real views last, as the html-design is usually done in my case by somebody else. If I have a lot of ajax/javascript interaction then I develop that in parallel to the controller/view development.

Frank
#4

[eluser]syntaxerror[/eluser]
thanks guys, but that too technical, i mean technical terms.
how can i call model in the controller and how can i call controller in a model?

thanks
#5

[eluser]Frank Berger[/eluser]
[quote author="syntaxerror" date="1223894528"]thanks guys, but that too technical, i mean technical terms.
how can i call model in the controller and how can i call controller in a model?

thanks[/quote]

Uhm.. well.. this is a rather technical topic, but on calling a model from the controller:

Code:
class myController Extends Controller {

function myController() {
  parent::Controller();
  $this->load->model('myModel');
}

function index() {
  $data = $this->mymodel->getdata();
}

}

as on calling a Controller from a Model, that is not really an advisable Procedure, as usually only the browser/user interacts with the controller, but something like this:

Code:
$CL = &get;_instance();

will get you the currently loaded Controller object referenced in your library or model code, and you can access the Controllers methods.

you really should read and understand the documentation on this from here:
http://ellislab.com/codeigniter/user-gui...odels.html

Frank
#6

[eluser]crumpet[/eluser]
frank maybe you can answer a question for me too..
I am writing a function to display a view of a product right now. In the controller i have code to determine what privileges the user has to that product so it can decide what to display. Should this be in the model? My instinct was to put it there however then it makes my database calls less efficient, becuase I have already fetched the information about the product and stored it in a variable $product. I can do tests like $this->session->userdata('USER_ID') == $product['author']. If i make a model function called getUserAccess() or something then it will have to make an extra query to get information I have already queried... hope this makes sense
#7

[eluser]Frank Berger[/eluser]
[quote author="crumpet" date="1223896000"]frank maybe you can answer a question for me too..
I am writing a function to display a view of a product right now. In the controller i have code to determine what privileges the user has to that product so it can decide what to display. Should this be in the model? My instinct was to put it there however then it makes my database calls less efficient, becuase I have already fetched the information about the product and stored it in a variable $product. I can do tests like $this->session->userdata('USER_ID') == $product['author']. If i make a model function called getUserAccess() or something then it will have to make an extra query to get information I have already queried... hope this makes sense[/quote]

makes sense to me, and I myself stand before similar situations quite often.

The thing is, permissions are either tied to the data-access model (for example that it incorporates the user-id when querying and not returning the dataset at all if no match - meaning access/read/write permissions ON the dataset) or they are tied to actions, which are in the view/controller domain.

So, if the question is if you should display lets say an 'edit' button, then it is definitely view/controller, as those two are responsible for drawing an edit-button or not. If it is a question of low-level access or read/write permissions, I would tend towards implementing it in the model, but carefully so that the model doesn't need to use or know how the user-name/id got there (means, the model should not access the session, the controller should access the session and pass the user-id/name to the model, which then either creates a private user object or uses the passed value directly.)

I am one of the many who posted an acl implementation (in my case for the controller) here, which I plan to extend to models too, but which is at this point not really production material.

Hope this makes sense to you, if not ask again. and of course this is highly imho and in my expirience Wink

Frank
#8

[eluser]syntaxerror[/eluser]
hi,
how about returning more variables in a model.
e.g.
Code:
<?php
class Process_model extends Model{
    function Process_model()
        {
            parent::Model();
        }
    function compareBatch()
    {
        $kebase = "//asecasianas2/DS_Keying/STATS/KE-DIR/";
        $qcbase = "//asecasianas2/DS_Keying/STATS/QC-DIR/";
        $dir = $this->input->post('directory');
        $ke = directory_map($kebase.$dir, TRUE);
        $qc = directory_map($qcbase.$dir, TRUE);
        $kelist = count($ke) - 1;
        $kelist2 = $kelist + 1;
        $qclist = count($qc) - 1;
        $qclist2 = $qclist+ 1;
        $count_comp = strcmp($kelist2, $qclist2);
        $row_comp = strcmp($kelist,$qclist);
            for($x = 0; $x <= $qclist; $x++)
            {
                $file1 = ($kebase . $dir . $ke[$x]);
                $file2 = ($qcbase . $dir . $qc[$x]);
                $file1 = file($file1);
                $file2 = file($file2);
                $file2_count = count($file2);

            }
                
    }    
        
}

?&gt;
how can i return all my variables in the controller?

thanks
#9

[eluser]xwero[/eluser]
php functions are limited to one output but it can be an array or an object. In a class you can use class variables to assign values to.

But you should work with arrays in your for loop too. The code as it is will overwrite each file with the next file content until the files end.
Code:
$kefiles = array();
$qcfiles = array();
$qccount = array();
for($x = 0; $x <= $qclist; $x++)
            {
                $kename = ($kebase . $dir . $ke[$x]);
                $qcname = ($qcbase . $dir . $qc[$x]);
                $file1 = file($kename);
                $file2 = file($qcname);
                $qccount[] = count($file2);
                $kefiles[] = $file1;
                $qcfiles[] = $file2;
            }
return array($keyfiles,$qcfiles,$qccount);
#10

[eluser]syntaxerror[/eluser]
ah...ok....
but when it comes to controller, how can i call these array($keyfiles,$qcfiles,$qccount);




Theme © iAndrew 2016 - Forum software by © MyBB