Building Models

MVC & Backend Development

Lesson 3 30 min Free Preview

Building Models

Creating data models with Medoo ORM

Building Models with Medoo ORM

Models handle your data layer. We use Medoo - a lightweight yet powerful PHP database framework.

💡 Why Medoo?

Simple API, prevents SQL injection, supports MySQL/PostgreSQL/SQLite, and is natively integrated in Ginto AI.

Installing Medoo

# Install via Composer
composer require catfan/medoo

Database Connection

<?php
// config/database.php

use Medoo\Medoo;

return new Medoo([
    'type' => 'mysql',
    'host' => $_ENV['DB_HOST'],
    'database' => $_ENV['DB_DATABASE'],
    'username' => $_ENV['DB_USERNAME'],
    'password' => $_ENV['DB_PASSWORD'],
    'charset' => 'utf8mb4'
]);

Basic Model Structure

<?php
// src/Models/User.php

namespace App\Models;

use Medoo\Medoo;

class User {
    private Medoo $db;
    protected string $table = 'users';
    
    public function __construct(Medoo $db) {
        $this->db = $db;
    }
    
    public function find(int $id): ?array {
        return $this->db->get($this->table, '*', ['id' => $id]);
    }
    
    public function all(): array {
        return $this->db->select($this->table, '*', [
            'ORDER' => ['created_at' => 'DESC']
        ]);
    }
    
    public function create(array $data): int {
        $this->db->insert($this->table, [
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => password_hash($data['password'], PASSWORD_DEFAULT),
            'created_at' => date('Y-m-d H:i:s')
        ]);
        return (int) $this->db->id();
    }
    
    public function update(int $id, array $data): bool {
        $result = $this->db->update($this->table, $data, ['id' => $id]);
        return $result->rowCount() > 0;
    }
    
    public function delete(int $id): bool {
        $result = $this->db->delete($this->table, ['id' => $id]);
        return $result->rowCount() > 0;
    }
}

CRUD Operations Reference

Operation Medoo Method Example
Create insert() $db->insert('users', $data)
Read One get() $db->get('users', '*', ['id' => 1])
Read Many select() $db->select('users', '*')
Update update() $db->update('users', $data, ['id' => 1])
Delete delete() $db->delete('users', ['id' => 1])

Advanced Queries

// Where conditions
$activeUsers = $db->select('users', '*', [
    'status' => 'active',
    'role' => ['admin', 'moderator'],  // IN clause
    'age[>=]' => 18,                   // Comparison
    'name[~]' => '%john%'              // LIKE
]);

// Ordering and limiting
$latestUsers = $db->select('users', '*', [
    'ORDER' => ['created_at' => 'DESC'],
    'LIMIT' => 10
]);

// Joins
$posts = $db->select('posts', [
    '[>]users' => ['user_id' => 'id']  // LEFT JOIN
], [
    'posts.id',
    'posts.title',
    'users.name(author)'
]);

// Aggregations
$count = $db->count('users', ['status' => 'active']);
$maxId = $db->max('users', 'id');

Useful Resources

🚀 Next Steps

In the next lesson, you'll learn how to create Controllers and set up routing.