# Models, Views and Controllers

## What is MVC?

MVC (Model-View-Controller) is a design pattern used in software development, especially for web applications. It separates an application into three main components:

* Model – Manages data and business logic.
* View – Handles the user interface (UI) and presentation.
* Controller – Acts as a bridge between the Model and View, processing user inputs.

## How it works?

1. **User requests a page (URL):** The request is sent to a **Controller**.
2. **Controller processes the request:** It may interact with a **Model** to get or update data.
3. **Model handles data:** The Model retrieves, updates, or processes data from the database.
4. **Controller passes data to the View:** The View formats and displays the data to the user.
5. **User sees the updated page:** The final output (HTML, JSON, etc.) is sent to the browser                                                                   &#x20;

<figure><img src="/files/ExQds0ateXN94Pc4Fcr8" alt=""><figcaption></figcaption></figure>

## The Components

### Models

* Represents data and defines how it is processed.
* Communicates with the database.
* Does not directly interact with the View.

Functions in [BaseModel](/documents/core/basemodel.md)

**`__construct()`**: Automatically called when an object is instantiated. It is typically used to establish a database connection or initialize important class properties.

**`_schema()`**: Defines the structure of the database table by specifying columns and their data types.

**`_table()`**: Returns the name of the database table that the model corresponds to, making queries more flexible and maintainable.

**`_columns()`**: Specifies the list of columns that can accept data during insert or update operations.

**`row()`**: Fetches a single record from the specified table based on the given search conditions.

**`list()`**: Fetches multiple records from the table based on specified conditions, with options for sorting and limiting the number of returned records.

**`listpaging()`**: Fetches paginated data, allowing large datasets to be divided into manageable pages for better control and display.

**`listfieldpaging()`**: Obtains data from specific table columns with pagination support, optimizing query performance and data display.

**`add()`**: Inserts a new record into the specified table with the provided data, ensuring validation before storage.

**`set()`**: Updates one or more records in the table based on specified conditions, ensuring that only valid fields are modified.

**`del()`**: Deletes one or more records from the table based on the provided conditions.

**`query()`**: Executes a custom SQL query with provided parameters, allowing for extended query capabilities beyond the default CRUD operations.

**`lastInsertId()`**: Gets the ID of the most recently inserted record in the table.

**`count()`**: Counts the number of records in the specified table based on given conditions.

Explore more about [Models](/documents/models.md)

### **Views**

* Displays the data provided by the Controller.
* Contains HTML, CSS, JavaScript, and minimal PHP.
* Should not contain business logic.

Explore more about  [Views & Templates](/documents/views-and-templates.md)

### **Controllers**

* Processes user input and interacts with the Model.
* Sends the processed data to the View.
* Acts as the middleman between Model and View.

Functions in [BaseController](/documents/core/basecontroller.md)

* **`data()`**: Stores and retrieves internal class data. It is used to temporarily store information for use in other methods.
* **`render()`**: Displays content using a predefined layout. This method is useful for managing webpage rendering in an MVC framework.
* **`json()`**: Returns data in JSON format. It is commonly used to provide data for APIs or AJAX requests.
* **`success()`**: Returns a JSON response for successful operations. This is typically used for API responses when a request is successfully completed.
* **`error()`**: Returns a JSON response for errors. It helps send API error responses when an issue occurs in the system.
* **`get_success()`**: Returns a success response as an array. This method is useful for processing API responses internally before sending them to the client.
* **`get_error()`**: Returns an error response as an array. It is used to check errors during processing before sending a response.

Explore more about [Controllers](/documents/controllers.md)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.cmsfullform.com/framework-overview/models-views-and-controllers.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
