Skip to content

Model

Model layer is responsible for handling database operations using PDO with prepared statements, providing a secure and consistent way to interact with your database. Each model includes a dedicated function that executes raw or parameterized SQL queries and returns the result directly to the controller. This design keeps your business logic clean by separating data access into its own layer, while also improving security and performance through the use of prepared statements. The controller simply calls the model when it needs to fetch, insert, update, or delete data—making your application logic easy to follow and maintain.

Creating model

How to create model:

Manual

  1. Define namespace Model
  2. Define use Core\Model
  3. Create class name using PascalCase and extends your class from Model
  4. Save it with the same class name using PascalCase under model directory

CLI

bash
php snappy generate model:YourModelName

Example:

php
// model/YourModelName.php

namespace Model
use Core\Model

class YourModelName extends Model{

}

Methods

Snappy’s Model class provides a streamlined entry point for executing SQL queries along with their parameters.

💡 Usage

execute sql query string

php
$this->execute( string $sql, array $parameter = [] ): array

sql

  Your raw sql query

parameter (optional)

  A parameter that its structure follows PDO array named values.

Example:

php
$sql = "select * from table_name where id = :id";
$parameter = [":id" => "10"];
return $this->execute($sql, $parameter);

output structure

php
[
  status: int $responseStatus,
  message: string $message,
  result: array $fetchedResult
]

Multiple parameters

The execute method in Snappy is designed to accept either a single set or multiple sets of parameters. This makes it particularly useful when you need to perform the same query repeatedly with different values — such as fetching data for multiple users, filtering records by varying conditions, or processing batch queries. By reusing the prepared statement for each execution, it maintains performance and security while offering flexibility for complex, multi-parameter operations.

ℹ️ NOTE

When provided with an array of parameter arrays, it loops PARAMETER ONLY through each set and executes the prepared SQL statement ONE TIME but with different parameters.

Example:

php
$sql = "update user_table set email = :email, status = :status where id = :id";
$params = [
    [ ":id" => 1, ":email" => "alice@example.com", ":status" => "active" ],
    [ ":id" => 2, ":email" => "bob@example.com",   ":status" => "inactive" ],
    [ ":id" => 3, ":email" => "carol@example.com", ":status" => "active" ]
];
return $this->execute($sql, $params);