Database Architecture

Table of Contents

  • Introduction

  • Database Connection System

  • Medoo ORM Overview

  • Database Schema

  • DB Operations

  • Advanced Queries

  • Best Practices

  • Troubleshooting


Introduction

PHPTRAVELS v10+ uses a robust database architecture built on MySQL/MariaDB with the Medoo PHP database framework. This lightweight ORM (Object-Relational Mapping) library provides a simple, secure, and efficient way to interact with the database while maintaining clean, readable code.

Why Medoo?

Lightweight & Fast

  • Only ~60KB in size with zero dependencies

  • PDO-based for optimal performance

  • Minimal overhead compared to heavy ORMs

Security First

  • Built-in SQL injection protection through prepared statements

  • Automatic parameter binding and escaping

  • No raw SQL strings in production code

Developer Friendly

  • Intuitive array-based syntax

  • Chainable methods for complex queries

  • Comprehensive error handling

  • Multi-database support (MySQL, MariaDB, PostgreSQL, SQLite)


Database Connection System

Environment Configuration (.env)

PHPTRAVELS v10 uses environment variables for database configuration, ensuring security and portability across different environments (development, staging, production).

Location: /v10/.env

Configuration Parameters:

Parameter
Description
Default

DB_TYPE

Database engine (mysql, mariadb, sqlite, pgsql)

mysql

DB_HOST

Database server hostname or IP address

localhost

DB_DATABASE

Name of the database

v10

DB_USERNAME

Database user with appropriate permissions

root

DB_PASSWORD

User password (use strong passwords in production)

-

Connection Initialization (config.php)

The central configuration file loads environment variables and establishes the database connection globally.

Location: /config.php

Connection Flow Diagram:

Global Accessibility

Once initialized in config.php, the $db variable is globally accessible throughout the application:

Connection Error Handling


Medoo ORM Overview

Medoo provides an elegant, array-based API for database operations. Instead of writing raw SQL, you work with PHP arrays and objects.

Core Concepts

1. Array-Based Syntax

2. Automatic Parameter Binding All values are automatically escaped and bound as prepared statement parameters, preventing SQL injection.

3. Method Chaining Complex queries can be built incrementally using method chaining for readability.

Medoo Methods Reference

Method
Purpose
Returns

select()

Retrieve multiple rows

Array of rows

get()

Retrieve single row

Associative array

insert()

Insert new record

PDOStatement

update()

Update existing records

PDOStatement

delete()

Delete records

PDOStatement

replace()

Replace record (INSERT or UPDATE)

PDOStatement

has()

Check if record exists

Boolean

count()

Count rows matching criteria

Integer

sum()

Calculate sum of column

Number

avg()

Calculate average

Number

max()

Find maximum value

Mixed

min()

Find minimum value

Mixed

query()

Execute raw SQL

PDOStatement


Database Schema

PHPTRAVELS v10 uses a comprehensive relational database schema with 30+ tables organized into logical groups.

Schema Overview

Core Tables

1. users - User authentication and profiles

Key Fields:

  • user_id: Unique external identifier (UUID format)

  • password: Hashed using password_hash() with PASSWORD_BCRYPT

  • user_type: Role-based access control (customer/agent/admin)

  • status: Active (1) or inactive (0) account

  • email_verified: Email confirmation status

2. settings - Global application configuration

3. modules - Enabled/disabled booking modules

4. currencies - Supported currencies with exchange rates

5. languages - Multi-language support

Booking Tables

bookings - Master booking records

Logging Tables

System maintains comprehensive audit logs:

  • logs_bookings - Booking lifecycle events

  • logs_searches - User search analytics

  • logs_users - Authentication and profile changes

  • logs_webhooks - External API webhook events

Scalability Design

The database schema is designed for horizontal scalability:

Current Tables: 30+ core tables Expandable: Schema supports unlimited growth through:

  • Modular table design (add new booking types)

  • Polymorphic relationships (flexible module system)

  • JSON columns for dynamic data (booking_data, user_data)

  • Partitioning support for large tables


DB Operations

SELECT - Retrieving Data

Basic Select - All Columns

Select Specific Columns

Select with WHERE Conditions

Select with Multiple Conditions (AND)

Select with OR Conditions

Select with Comparison Operators

Select with LIKE (Pattern Matching)

Select with LIMIT and ORDER

Select with Pagination

Get Single Row

INSERT - Creating Records

Basic Insert

Insert with Current Timestamp

Bulk Insert

UPDATE - Modifying Records

Basic Update

Update with Multiple Conditions

Increment/Decrement Values

Update with Raw SQL

DELETE - Removing Records

Basic Delete

Delete with Multiple Conditions

Soft Delete (Recommended)

EXISTS - Check Record Existence

Check if User Exists

COUNT - Counting Records

Simple Count

Count with Conditions


Advanced Queries

JOIN Operations

INNER JOIN

LEFT JOIN

Multiple JOINS

Aggregation Functions

SUM

AVERAGE

MAX/MIN

GROUP BY and HAVING

Group By with Count

Raw SQL Queries

For complex queries not covered by Medoo methods:


Best Practices

1. Security

Always Use Prepared Statements

Hash Passwords Properly

Validate Input

2. Performance Optimization

Use Indexes

Select Only Required Columns

Use LIMIT for Large Tables

Cache Frequent Queries

3. Error Handling

Always Use Try-Catch

Check for Errors

4. Data Integrity

Use Transactions

5. Code Organization

Create Repository Classes


Troubleshooting

Connection Issues

Problem: "Access denied" Error

Problem: "Database not found"

Problem: PDO Extension Not Loaded

Query Issues

Problem: Empty Results

Problem: Slow Queries

Data Integrity

Problem: Duplicate Entries

Problem: Foreign Key Constraints

Debugging Tools

View Generated SQL

Check Connection Status

Error Information


Additional Resources

Official Medoo Documentation https://medoo.in/doc

PHPTRAVELS Database Migrations Location: /install/db.sql

Database Backup Command

Database Restore Command


Summary

The PHPTRAVELS v10 database architecture provides:

  • Secure connection management via environment variables

  • Efficient ORM operations through Medoo framework

  • Scalable schema design supporting 30+ tables

  • Comprehensive DB operations with array-based syntax

  • Advanced query capabilities (JOINs, aggregations, transactions)

  • Built-in security through prepared statements

  • Performance optimization through indexing and caching

  • Robust error handling and debugging tools

This architecture ensures data integrity, security, and performance while maintaining developer productivity through clean, readable code.

Last updated