Download the PHP package webfiori/database without Composer
On this page you can find all versions of the php package webfiori/database. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package database
Webfiori Database Abstraction Layer
Database abstraction layer of WebFiori framework.
Content
- Supported PHP Versions
- Supported Databases
- Features
- Installation
- Usage
- Connecting to Database
- Running Basic SQL Queries
- Building Database Structure
- Repository Pattern
- Active Record Pattern
- Entity Generation
- Database Migrations
- Database Seeders
- Performance Monitoring
- Transactions
Supported PHP Versions
| Build Status |
|---|
Supported Databases
- MySQL
- MSSQL
- SQLite
Features
- Building your database structure within PHP
- Fast and easy to use query builder
- Database abstraction which makes it easy to migrate your system to different DBMS
- Repository pattern with
AbstractRepositoryfor clean data access - Active Record pattern support for rapid development
- PHP 8 attributes for table definitions
- Database migrations and seeders
- Performance monitoring and query analysis
- Entity generation for object-relational mapping
- Transaction support with automatic rollback
Installation
To install the library using composer, add following dependency to composer.json: "webfiori/database":"*"
Usage
Connecting to Database
Connecting to a database is simple. First step is to define database connection information using the class ConnectionInfo. Later, the instance can be used to establish a connection to the database using the class Database.
Running Basic SQL Queries
For every query, the table must be specified using Database::table(string $tblName). The method returns an AbstractQuery instance with methods for building queries:
insert(array $cols): Construct an insert query.select(array $cols): Construct a select query.update(array $cols): Construct an update query.delete(): Construct a delete query.where($col, $val): Adds a condition to the query.
After building the query, call execute() to run it.
Building Database Structure
Define database structure in PHP code using blueprints:
You can also register tables from classes that use #[Table]/#[Column] attributes or extend Table:
This works with both attribute-based classes and Table subclasses (MySQLTable/MSSQLTable). If the class engine differs from the connection, it is converted automatically.
Repository Pattern
The AbstractRepository class provides a clean way to handle data access with separation between entities and database logic.
Creating an Entity
Creating a Repository
Using the Repository
Active Record Pattern
For rapid development, you can merge entity and repository into a single model class:
Usage:
Entity Generation
Generate entity classes from table blueprints:
Database Migrations
Version control your database schema changes:
Run migrations:
Connection-Targeted Migrations
In multi-database architectures, you can restrict a migration to specific named connections. This is useful when different databases serve different purposes (e.g., one for user data, another for reporting):
Migrations with an empty getTargetConnections() (the default) run on all connections. The connection name comes from ConnectionInfo::getName().
Database Seeders
Populate your database with sample data:
Performance Monitoring
Track and analyze query performance:
Transactions
Execute multiple operations as a single unit:
Examples
See the examples directory for complete working examples:
- 01-basic-connection - Database connections
- 02-basic-queries - CRUD operations
- 03-table-blueprints - Table structures
- 04-entity-mapping - Entity generation
- 05-transactions - Transaction handling
- 06-migrations - Schema migrations
- 07-seeders - Data seeding
- 08-performance-monitoring - Query analysis
- 09-multi-result-queries - Stored procedures
- 10-attribute-based-tables - PHP 8 attributes
- 11-repository-pattern - Repository pattern
- 12-clean-architecture - Domain separation
- 13-pagination - Pagination techniques
- 14-active-record-model - Active Record pattern
- 17-sqlite - SQLite database usage