# thingengineer/php-mysqli-database-class architecture

> Architecture diagram of the GitHub repository thingengineer/php-mysqli-database-class, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/thingengineer/php-mysqli-database-class
- Repository: https://github.com/thingengineer/php-mysqli-database-class

## Overview

##### Project Type and Purpose
- This is an open-source PHP library (composer package) providing:
- A lightweight MySQLi wrapper with query-builder, prepared-statement support (MysqliDb.php)
- An optional object mapping layer on top of it (dbObject.php)
- It’s meant to be imported into PHP applications to simplify database access, data import (CSV/XML), and object mapping.

##### File Structure
- Top-level files:
- MysqliDb.php – core library
- dbObject.php + dbObject.md – object-mapping extension
- composer.json – package metadata and dependencies
- index.php / readme.md – examples and documentation
- tests/ – unit and integration tests
- tests/dbObjectTests.php, tests/mysqliDbTests.php – core functionality
- tests/dataimport/ – CSV/XML import tests
- tests/models/ – example model tests
- No frontend/UI; the project is a backend library.

##### README Insights
- Detailed usage of connection initialization, CRUD operations, transactions, subqueries, pagination, raw SQL, data import (CSV/XML), and advanced query options.
- Explains multi-connection support, object builders, map/array/json output, and helper methods.

##### Main Components
a. Application Layer
- PHP client code or third-party application requiring the library (via require or composer)
b. Library Layer
- MysqliDb core class – query builder and MySQLi wrapper
- dbObject extension – object mapping on top of MysqliDb
c. Database Driver & Server
- PHP MySQLi extension
- MySQL Server(s) – primary and optional slave connections
d. Data Import Subsystem
- CSV/XML input files
- loadData() / loadXML() routines in MysqliDb
e. Testing Harness
- tests/ directory – PHPUnit or custom scripts invoking core and import features

##### Relationships and Data Flow
- Application → (calls) → MysqliDb
- Application → (optional calls) → dbObject → MysqliDb
- MysqliDb → (invokes) → PHP MySQLi extension → MySQL Server
- MysqliDb.loadData()/loadXML() → (reads) → CSV/XML files → MySQL bulk-load queries
- tests/ → (executes) → library methods → MySQL Server (using test schema)

##### Architectural Patterns & Design Principles
- Query Builder pattern encapsulating SQL generation
- Active Record / Data Mapper via dbObject
- Singleton / Registry (getInstance) for shared DB object
- Fluent Interface for method chaining
- Support for multiple connections (master/slave)
- Extension points: rawQuery, custom functions, subquery builder

##### Essential Elements to Capture
- Clear labels on components (MysqliDb, dbObject, MySQLi, MySQL Server(s), CSV/XML, Application, Tests)
- Arrowheads indicating call or data flow direction
- Notes on multi-connection support (master/slave)
- Annotation of key patterns (Fluent API, Singleton, Data Mapper)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Application Layer
    subgraph "Application Layer"
        index(("index.php")):::app
    end

    %% Documentation
    readme(["readme.md"]):::docs
    composer(["composer.json"]):::docs
    dbObjectDoc(["dbObject.md"]):::docs

    %% Library Layer
    subgraph "Library Layer"
        mdb["MysqliDb"]:::library
        dbo["dbObject"]:::library
    end

    %% Data Import Subsystem
    subgraph "Data Import Subsystem"
        csv(["data.csv"]):::docs
        xml(["data.xml"]):::docs
    end

    %% Test Harness
    subgraph "Test Harness"
        testCore(["mysqliDbTests.php"]):::docs
        testDbObj(["dbObjectTests.php"]):::docs
        importTest(["test.import.php"]):::docs
        modelProduct(["product.php"]):::docs
        modelUser(["user.php"]):::docs
    end

    %% External Systems
    mysqliExt(["PHP MySQLi extension"]):::ext
    mysqlMaster[(MySQL Master)]:::dbserver
    mysqlSlave[(MySQL Slave)]:::dbserver

    %% Relationships
    index -->|"require/composer"| mdb
    index -->|"optional require"| dbo
    dbo -->|"extends / maps via"| mdb
    mdb -->|"prepareQuery()/execute()"| mysqliExt
    mysqliExt -->|"connect/query"| mysqlMaster
    mysqliExt -->|"connect/query"| mysqlSlave

    csv -->|"loadData()"| mdb
    xml -->|"loadXML()"| mdb
    mdb -->|"bulk-load"| mysqlMaster

    testCore -->|"tests core API"| mdb
    testDbObj -->|"tests object mapping"| dbo
    importTest -->|"tests import routines"| mdb
    modelProduct -->|"model mapping"| dbo
    modelUser -->|"model mapping"| dbo

    %% Click Events
    click mdb "https://github.com/thingengineer/php-mysqli-database-class/blob/master/MysqliDb.php"
    click dbo "https://github.com/thingengineer/php-mysqli-database-class/blob/master/dbObject.php"
    click dbObjectDoc "https://github.com/thingengineer/php-mysqli-database-class/blob/master/dbObject.md"
    click composer "https://github.com/thingengineer/php-mysqli-database-class/blob/master/composer.json"
    click index "https://github.com/thingengineer/php-mysqli-database-class/blob/master/index.php"
    click readme "https://github.com/thingengineer/php-mysqli-database-class/blob/master/readme.md"
    click testCore "https://github.com/thingengineer/php-mysqli-database-class/blob/master/tests/mysqliDbTests.php"
    click testDbObj "https://github.com/thingengineer/php-mysqli-database-class/blob/master/tests/dbObjectTests.php"
    click importTest "https://github.com/thingengineer/php-mysqli-database-class/blob/master/tests/dataimport/test.import.php"
    click csv "https://github.com/thingengineer/php-mysqli-database-class/blob/master/tests/dataimport/data.csv"
    click xml "https://github.com/thingengineer/php-mysqli-database-class/blob/master/tests/dataimport/data.xml"
    click usersSql "https://github.com/thingengineer/php-mysqli-database-class/blob/master/tests/dataimport/users.sql"
    click modelProduct "https://github.com/thingengineer/php-mysqli-database-class/blob/master/tests/models/product.php"
    click modelUser "https://github.com/thingengineer/php-mysqli-database-class/blob/master/tests/models/user.php"

    %% Styles
    classDef library fill:#ADD8E6,stroke:#0000FF,color:#000
    classDef app fill:#90EE90,stroke:#008000,color:#000,stroke-width:2px
    classDef dbserver fill:#FFA500,stroke:#CC8400,color:#000
    classDef docs fill:#D3D3D3,stroke:#888888,color:#000,stroke-dasharray: 2 2
    classDef ext fill:#FFD700,stroke:#DAA520,color:#000
```
