Search code examples
phpmodel-view-controllercodeignitersurvey

A Survey Model or Controller?


I am very new to MVC.

I am using CodeIgniter to create a platform upon people can submit a survey which is then placed in a database for use later on inside a dashboard for users.

Now I am confused a little, how would I create this survey? Would I make a controller or a model? Would I use both, a controller to create the form and then a model to submit the data to the database?

Can I interact with my database directly in the controller?


Solution

  • Model Usually, a database table is represented by a model. For example, if you have a table named 'surveys', you'll want to have a surveys_model class that allows you to perform table-level operations on that table (select, insert, update, delete, etc).

    Then, you can create another model to represent a single row in that table, named survey_model (notice it's singular). This class will usually have properties that match up 1-to-1 with columns in the table. In this model, you can enforce business rules. For example, if you don't want a survey to contain the same question twice, you can add logic to check if a question already exists when adding a new question.

    The process of taking a database record and creating an object is called 'mapping', and there is software that does this automatically (ORM software). You can also roll-your-own simple mapping, as most ORM has a bit of a learning curve.

    You can also skip the mapping and work with the db result directly (for reads/output). When you want to write data, you just pass in the data as parameters, or as an array, to the table-level model.

    View The view contains the front end code (HTML/CSS/JS) to display the form to the user. If the user is looking at an existing survey (say, for editing purposes), then the model must be made available to view (or, at least some of the data from the model).

    The only logic here should be for the purposes of display. For example, show one block of text under a certain condition, and another block of text under a different condition (but if the entire output is conditional, it should be split into 2 views and the logic should be moved to the controller to load the correct view).

    Controller This is what controls which view to show, which models are loaded, and what data is passed to the view. You can pass the entire model, or just some data from the model.

    It also controls the interaction between the view and the model. For example, if you post a form, the controller will get the form data, validate it, load the model, update the data in the model with the post data, and save the model. Then it can display a success or error message (or redirect to an entirely new page).

    Sometimes you'll find that it's hard to group related functionality together within a controller because its class and method names are used in routing, or you'll find that controllers become too long and hard manage. This is where libraries come in.

    You can move logic out of controllers and into libraries. This way, you can keep your controllers lean and group your logic without worrying about the effect on routing.

    For functions that you'll need globally that don't require a class, you can put these in helpers. You can look through the built-in CI helpers to see the types of functions that belong in helper files.