PHPackages                             hegentopf/easy-orm - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. hegentopf/easy-orm

ActiveLibrary[Database &amp; ORM](/categories/database)

hegentopf/easy-orm
==================

Lightweight ORM for PHP with automatic model generation

0.1.3(4mo ago)011MITPHPPHP &gt;=8.1

Since Dec 28Pushed 4mo agoCompare

[ Source](https://github.com/hegentopf/easy-orm)[ Packagist](https://packagist.org/packages/hegentopf/easy-orm)[ RSS](/packages/hegentopf-easy-orm/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (3)Versions (2)Used By (0)

EasyORM Usage Guide
===================

[](#easyorm-usage-guide)

This document provides examples and explanations for using EasyORM, including model creation, query building, and fetching data.

Installation
------------

[](#installation)

Install via Composer:

```
composer require hegentopf/easy-orm

```

Basic Usage
-----------

[](#basic-usage)

### Initial Setup for Creation and Usage

[](#initial-setup-for-creation-and-usage)

```
use Hegentopf\EasyOrm\connection\ConnectionManager;
use Hegentopf\EasyOrm\connection\MySQLConnection;

// Set up the database connection, adjust parameters as needed.
// Use .env or config files in production
$default = new MySQLConnection( 'dbName', 'user', 'passwd', 'localhost', 3306 );
ConnectionManager::setConnection( $default );

// For multiple connections, you can set and get connections by name
$replication = new MySQLConnection( 'dbName', 'user', 'passwd', 'localhost', 3307 );
ConnectionManager::setConnection( $replication, 'replication' );

```

### Model Creation

[](#model-creation)

```
use Hegentopf\EasyOrm\modelCreator\DbModelCreator;

$dbModelCreator = new DbModelCreator();
$dbModelCreator
    ->setNamespace( 'App\\dbModels' )
    ->setPath( __DIR__ . '/../src/dbModels' )
    ->createAllDbModels( true ); // true = override existing models

```

This will generate models for all tables in your current database.

### Using Models

[](#using-models)

```
use App\dbModels\test\ProductModel;

// Create a new model
$productModel = new ProductModel();
$productModel->setName( 'Screwdriver' )->setPrice( 15.40 )->save();

// Fetch all models
$productModel = ProductModel::getQueryBuilder()->get();

// Fetch a single model by primary key
$productModel = ProductModel::fetchById( 1 );

// Update a model
$productModel->setName( 'Nail' )->setPrice( 0.22 )->save();

// Delete a model
$productModel->delete();

```

### QueryBuilder Examples

[](#querybuilder-examples)

#### Simple Select

[](#simple-select)

```
use App\dbModels\test\ProductModel;

$productModels = ProductModel::getQueryBuilder()
    ->select( ProductModel::name(), ProductModel::price() )
    ->limit( 10 )
    ->get();

```

#### Select with Where, GroupBy, OrderBy

[](#select-with-where-groupby-orderby)

```
use App\dbModels\test\ProductModel;
use Hegentopf\EasyOrm\queryBuilder\OrderBy;

$products = ProductModel::getQueryBuilder()
                        ->select(
                                ProductModel::name(),
                                ProductModel::price(),
                                ProductModel::timestamp_created()
                            )
                        ->where( ProductModel::name(), '=', 'Screwdriver' )
                        ->groupBy( ProductModel::name() )
                        ->orderBy( ProductModel::name(), OrderBy::DESC )
                        ->limit( 3 )
                        ->get();

```

#### Joins

[](#joins)

```
use App\dbModels\test\OrderModel;
use App\dbModels\test\ProductModel;

$orders = OrderModel::getQueryBuilder()
                    ->select(
                        OrderModel::id(),
                        OrderModel::order_date(),
                        ProductModel::name(),
                        ProductModel::price()
                    )
                    ->leftJoin(
                        ProductModel::getTable(),
                        ProductModel::order_id(),
                        OrderModel::id()
                    )
                    ->orderBy( OrderModel::order_date() )
                    ->get();

```

#### Subqueries

[](#subqueries)

```
use App\dbModels\shop\OrderModel;
use App\dbModels\shop\ProductModel;

$subQuery = ProductModel::getQueryBuilder()
                        ->select( ProductModel::order_id() )
                        ->where( ProductModel::price(), '>', 100 )
                        ->groupBy( ProductModel::order_id() );

$orders = OrderModel::getQueryBuilder()
                    ->select(
                        OrderModel::id(),
                        OrderModel::order_date()
                    )
                    ->whereIn( OrderModel::id(), $subQuery )
                    ->get();

```

### Notes and Best Practices

[](#notes-and-best-practices)

- SQL-Injections are prevented by using prepared statements.
- Joined Data can be accessed via magic getters, e.g., `$model->getJoinedColumnName()`.
- Date, DateTime, and Timestamp columns are automatically converted to `DateTime` objects. Note that the DateTime object is a copy, so modifying it does not change the model's value. To change the value, use the setter method.
- Use `new DbExpression( 'NOW()' )` for raw SQL expressions when needed (⚠️ be aware of SQL-Injections).
- Use `take()` and `skip()` as alternatives to `limit()` and `offset()`.
- To fetch only one model, use `first()` instead of `get()` and you will receive a single model or `null`.
- Columns are automatically mapped to protected properties.
- Only changed Columns are updated in the database.
- Generated models follow camelCase conversion from table and column names. `order_items` → `orderItems`

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance74

Regular maintenance activity

Popularity5

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity33

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

141d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/b4e855e7f1d3a134331cd2821a79cb58f92219088bd600e67d8f9f1a76a6e7b2?d=identicon)[hegentopf](/maintainers/hegentopf)

---

Top Contributors

[![hegentopf](https://avatars.githubusercontent.com/u/113784801?v=4)](https://github.com/hegentopf "hegentopf (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/hegentopf-easy-orm/health.svg)

```
[![Health](https://phpackages.com/badges/hegentopf-easy-orm/health.svg)](https://phpackages.com/packages/hegentopf-easy-orm)
```

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[mongodb/mongodb

MongoDB driver library

1.6k64.0M546](/packages/mongodb-mongodb)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)[wildside/userstamps

Laravel Userstamps provides an Eloquent trait which automatically maintains `created\_by` and `updated\_by` columns on your model, populated by the currently authenticated user in your application.

7511.7M13](/packages/wildside-userstamps)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
