How should a model be structured in MVC?

How should a model be structured in MVC?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

The Boundary Between Data and Logic in MVC

Determining where to draw the line between your model and your data access layer is one of the earliest architectural decisions that defines the health of an application. When you look at a method like CheckUsername that contains raw SQL strings, connection handling, and exception logic inside what looks like a model class, you are effectively collapsing distinct responsibilities into a single unit. This creates a fragile coupling between your business domain and your database implementation details.

In a strict MVC interpretation, the Model represents the entity or the state of the data, while the Data Access Layer (DAL) handles the mechanics of retrieving that state from storage. Your example shows a model performing a query execution. While this works technically, it conflates what the data is with how to get it. If your database schema changes, every method in your model containing SQL strings becomes a liability. You are hard-coding persistence logic inside classes that should primarily define business rules or entity shapes.

Separating Concerns Without Adding Layers

The fear of creating "four layers" often stems from over-engineering rather than a genuine need for separation. You do not need four distinct namespaces or physical directories to maintain clean architecture; you need logical boundaries.

Consider the structure where your Model acts as an active record (or entity) mapping directly to a table, and your DAL acts as a repository or service that orchestrates queries. In this setup, the CheckUsername logic belongs in the DAL, perhaps within a UserRepository class. The Model simply exposes properties like $username and methods that manipulate state.

// Model: User.php
class User {
    public $id;
    public $username;
    
    // Business logic lives here
    public function isValidEmail($email) {
        return filter_var($email, FILTER_VALIDATE_EMAIL);
    }
}

// DAL: UserRepository.php
class UserRepository {
    private $usersTableName = 'users';
    
    public function findByUsername(Connection $connection, string $username) {
        try {
            $stmt = $connection->prepare("SELECT * FROM {$this->usersTableName} WHERE Username = :Username");
            $stmt->execute(['Username' => $username]);
            
            return $stmt->fetch(PDO::FETCH_OBJ);
        } catch (Exception $e) {
            throw new RuntimeException("Database query failed", 0, $e);
        }
    }
}

This separation allows you to swap database engines or refactor your ORM strategy without touching the entity classes. The Model remains agnostic to whether the data comes from a SQL table, an API, or a flat file. The DAL owns the connection management and the query construction.

The Active Record Trap

It is tempting to keep everything inside the model class because it feels convenient. You might ask, "Why not just call $model->CheckUsername()?" This approach works fine for trivial applications but introduces significant technical debt as complexity grows. When logic bloats the model, you end up with classes that are hundreds of lines long, mixing validation rules, SQL queries, and business workflow.

The trade-off is between convenience and maintainability. Convenience yields immediate gratification; maintainability pays off over months or years of development. If you move your CheckUsername logic into a dedicated repository, you gain the ability to unit test that specific piece of logic in isolation. You can mock the database connection easily, ensuring that your data access layer functions correctly regardless of the environment.

When to Keep It Simple

However, this separation is not a mandate for every single operation. If you are building a prototype or a small utility script, the overhead of splitting layers might outweigh the benefits. The key indicator for refactoring is dependency complexity. If your model starts depending on specific database connection objects or error handling strategies from your DAL, you have crossed the line.

In frameworks like Laravel, which often popularizes this pattern, there is a dedicated Repository interface and implementation that sits between the Eloquent models and the database, specifically designed to handle these queries without polluting the model class itself. For more complex enterprise applications, introducing a repository pattern explicitly prevents the "God Model" anti-pattern where every business rule and SQL query lives inside the entity.

Ultimately, the goal is not to create layers for the sake of it, but to ensure that changes in your database implementation do not ripple uncontrollably through your business logic. By keeping SQL execution and connection handling in a dedicated layer, you preserve the purity of your model as a representation of data rather than a factory for database calls.

Our network