Download the PHP package amtgard/active-record-orm without Composer

On this page you can find all versions of the php package amtgard/active-record-orm. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package active-record-orm

Amtgard Active Record ORM

A modern Active Record ORM for PHP 8.3+ designed for the ORK4 system. This library provides a clean, intuitive interface for database operations with support for both traditional table-based queries and entity-based object mapping.

The library includes a command-line tool (repository.php) for generating Repository and RepositoryEntity classes from existing MySQL schemas, creating database migration files, and managing audit logging infrastructure.

Introduction

Amtgard Active Record ORM (Aaro) is an active record data access layer, in the vein of PorqDB - a completely dead ORM project from the dawn of time.

Aaro focuses on two basic use cases: CRUD operations with basic constraints and SQL record sets. Aaro does not offer facilities for modeling relationships or a DSL over SQL - the concept is that SQL is already the most robust language for this purpose.

Aaro provides four operating modes for database access:

  1. Low level database - Direct active record operations and SQL queries using the Database class
  2. Table level access - Active record operations and queries using the Table class
  3. Entity level - Object mapping with automatic persistence using EntityMapper and Entity classes
  4. RepositoryEntity abstraction level - High-level repository pattern with Repository and RepositoryEntity classes for type-safe, ergonomic data access

Each mode builds upon the previous, offering increasing levels of abstraction and convenience while maintaining the flexibility to drop down to lower levels when needed.

Installation

Install via Composer:

Requirements

Schema Requirements

Aaro is not overly opinionated about schema design, in the sense that it allows for a "Bring Your Own Schema" design - it does not try to enforce schemas based on object models.

However, by convention it expects exactly one primary key field per table.

Aaro works best with primary keys defined as auto-sequencing integers.

Basic Usage

Setting Up Database Connection

Aaro assumes connection by convention and works with Dotenv for configuration.

.env

Aaro is designed to work natively with aggressive caching policies, including Amtgard Redis SetQueues, which provides eventually-consistent persistence.

Basic usage can used uncached policies, including the UncachedDataAccessPolicy below.

Basic CRUD Operations

The core of Aaro are basic CRUD operations, specifically find() (aka SQL select) and save() (a contextually-aware mnemonic for SQL insert or update).

Access fields of a table is done by magic setters and getters. Every field in the table will be exposed public members of table object when instantiated.

For instance, if the table items below has the fields id and string_value, then those fields will be exposed as public properties of the itemTable object.

The values of the fields of the records can be accessed by accessing the fields on the object. For instance $total = $invoiceLine->quantity * $invoiceLine->amount;.

Assigning values to a field ($itemTable->string_value = "my value";) will update the local object in memory and can be persisted to the repository by calling $itemTalbe->save() or persisting via the Entity Manager.

Update vs Insert Context

When performing updates vs inserts, the equals operator is contextually sensitive to either set a value or constraint mode.

Aaro determines the context by checking for the existence of a primary key value on the given record. If a primary key is set, then the equals operations ($itemTable->name = "Bob") assumes the current context is either an update operations when save() is called, or an additional constraint when find() is called. If there is no primary key, then the context is assumed to be the insert mode when save() is called.

Finding Records

Query Operations

Inserting Records

If the underlying database supports it and is properly configured, then save()s will automatically propagate auto-generated primary key values. For non-autosequencing primary keys, you will have to perform a clear() then a find() using suitable constraints to fetch the record.

Updating Records

Deleting Records

Pagination and Limits

Counting Records

Working with SQL

Aaro will work with record sets using the same general principles. Aaro relies on direct SQL statements rather than a DSL wrapper over SQL. The tradeoff is that the SQL is not portable between RDMSes, however:

  1. Right now, Aaro only uses MariaDB/MySQL as a backend
  2. DSLs are not terribly portable and result in a lot of their own headaches
  3. The likelihood of RDMS swapping in a project is vanishingly low. Implementing a DSL over SQL is edge-casing an RDMS swap that should be given considerably more attention than just switching the RDMS. The chance of your code surviving such a swap intact is very, very low.

Basic Query Usage

Direct database queries:

All fields returned by a query will show up as public properties of the resulting record set:

Field name collisions are a function of your RDMS and underlying database driver. If there are field collisions (such as select a.*, b* ...), Aaro makes no attempt to disambiguate, and the field names and table column associations are left up to the RDMS and database driver selected.

EntityManager Usage

The EntityManager provides a higher-level abstraction for working with entities and managing object state.

Setting Up EntityManager

Working with Entities

An EntityMapper wraps a given table or record set and provides manual and automatic persistence.

Working with Custom SQL Queries

Entity State Management

Entities may be manually persisted using various persist*() methods.

RepositoryEntity Mode

The RepositoryEntity abstraction level provides a high-level, type-safe interface for working with database entities. This mode uses the Repository pattern with attribute-based configuration.

Setting Up RepositoryEntity Mode

First, create a Repository class that extends Repository and implements EntityRepositoryInterface:

Then, create a RepositoryEntity class that extends RepositoryEntity:

Working with RepositoryEntity

RepositoryEntity Features

AuditRepository Feature

The AuditRepository feature provides automatic audit logging for RepositoryEntity classes. When enabled, all insert, update, and delete operations are automatically logged to an audit log table.

Setting Up AuditRepository

To enable audit logging for a RepositoryEntity, simply use the AuditRepositoryEntityTrait:

Audit Log Table Structure

The audit log table is automatically created with the name {table_name}_audit_log and includes the following fields:

How It Works

When you use AuditRepositoryEntityTrait, the entity automatically:

  1. Creates a separate EntityManager that uses AuditTableFactory for mapper creation
  2. Wraps all table operations in an AuditTable that intercepts save() and delete() operations
  3. Logs all changes to the audit log table with metadata about what was changed

Example Usage

Accessing Audit Logs

You can query the audit log table directly:

The AuditRepository feature is particularly useful for compliance requirements, debugging, and maintaining a complete history of data changes.

Advanced Features

Custom Data Access Policies

You can implement custom data access policies by extending the base policy classes:

Builder Pattern

Most classes in this ORM use the builder pattern for configuration:

Comparison Operators

Testing

The library includes comprehensive unit and integration tests. Run tests with:

License

This project is licensed under the MIT License - see the LICENSE file for details.

Repository Generator Tool

The repository.php command-line tool automates the generation of Repository and RepositoryEntity classes, database schemas, and Phinx migrations. This tool helps you quickly scaffold your data access layer from existing MySQL databases or generate migration files from your entity classes.

Basic Usage

The tool can be run directly (if executable) or via PHP:

Commands Overview

Classes Command

The classes command generates Repository and RepositoryEntity classes by inspecting an existing MySQL table schema.

Basic Usage:

Options:

Examples:

Output:

Schema Command

The schema command generates MySQL CREATE TABLE SQL from existing RepositoryEntity class definitions.

Basic Usage:

Options:

Examples:

Output:

Phinx Command

The phinx command generates Phinx migration code from existing RepositoryEntity class definitions.

Basic Usage:

Options:

Examples:

Output:

Audit Command

The audit command provides sub-commands for generating audit-related infrastructure.

Audit Classes Sub-command

Generates Repository and RepositoryEntity classes with audit support (includes AuditRepositoryEntityTrait).

Usage:

Options:

Examples:

Audit Schema Sub-command

Generates MySQL CREATE TABLE SQL for audit log tables.

Usage:

Options:

Output:

Audit Phinx Sub-command

Generates Phinx migration files for audit log tables.

Usage:

Options:

Output:

Audit Migrate Sub-command

Runs both --classes and --phinx commands sequentially to generate complete audit infrastructure.

Usage:

Options:

Examples:

What it does:

  1. Generates Repository and RepositoryEntity classes with AuditRepositoryEntityTrait
  2. Generates Phinx migration files for audit log tables

Exclusions File

The tool supports excluding tables from batch operations using a .exclusions file located in the bin/ directory. This file supports:

Example .exclusions file:

Tables matching entries in .exclusions are automatically excluded when:

Environment File Handling

For commands that require --env, you can specify either:

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Support

For support and questions, please open an issue on the GitHub repository.


All versions of active-record-orm with dependencies

PHP Build Version
Package Version
Requires php Version ^8.3
ext-json Version *
monolog/monolog Version ^3.7
ext-pdo Version *
vlucas/phpdotenv Version ^5.6
amtgard/builder-traits Version ^2.1
jedibc/optional Version ^1.0
amtgard/phpunit-extensions Version ^1.0
amtgard/fuzzywuzzy Version ^1.0.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package amtgard/active-record-orm contains the following files

Loading the files please wait ...