PHPackages                             kris-ro/php-database-model - 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. kris-ro/php-database-model

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

kris-ro/php-database-model
==========================

PHP Model for querying database

v1.0.3(1y ago)0151MITPHPPHP 8.\*

Since Sep 4Pushed 1y ago1 watchersCompare

[ Source](https://github.com/kris-ro/php-database-model)[ Packagist](https://packagist.org/packages/kris-ro/php-database-model)[ RSS](/packages/kris-ro-php-database-model/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (4)Dependencies (1)Versions (5)Used By (1)

PHP Database Modeling
=====================

[](#php-database-modeling)

The scope of this class is to streamline the development. If you need a well structured library or system like ORM or Active Record then this is not for you. My aim here was to have a way to make an easy semantic request and get a result set from database. PDO is working behind the scene.

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

[](#installation)

Use composer to install *PHP Database Modeling*.

```
composer require kris-ro/php-database-model
```

Usage
-----

[](#usage)

These are all magic calls (processed with \_\_call()) in the form:
`get(Assoc|Objects|Unique|Column|Indexed|Grouped){Table_name}ByCondition()->(all|next)()`
`get(Assoc|Objects|Unique|Column|Indexed|Grouped){Table_name}By{Column_name}()->(all|next)()`
`set{Table_name}()`
`set{Table_name}AndGetId()`
`update{Table_name}ByCondition()`
`update{Table_name}()`
`delete{Table_name}ByCondition()`
`delete{Table_name}()`
`count{Table_name}ByCondition()`\\

```
require YOUR_PATH . '/vendor/autoload.php';

use KrisRo\PhpDatabaseModel\Model;

# Ge the model instance
$model = new Model([
  'host' => 'localhost',
  'database' => 'db_name',
  'username' => 'db_user',
  'password' => 'db_password',
]);

# get the result set as a list of associative array
$rows = $model->getAssocUsersByCondition([
  'condition' => '`users_id` < :id',
  'params' => [':id' => 20]
])->all();

# fetching one by one
$query = $model->getAssocUsersByCondition([
  'select' => 'salt, user_name',
  'condition' => '`users_id` < :id',
  'params' => [':id' => 20]
]);
$oneRow = $query->next();
$oneRow = $query->next();
$oneRow = $query->next();
$oneRow = $query->next();

# get the result set as a list of stdClass objects
$rows = $model->getObjectsUsersByCondition([
  'select' => 'salt, user_name',
  'condition' => '`users_id` < :id',
  'params' => [':id' => 20]
])->all();

# fetch the result set indexed by table's id or another unique column
# first column (`salt` in this case) is used for indexing the result set
$rows = $model->getUniqueUsersByCondition([
  'select' => 'salt, user_name',
  'condition' => '`users_id` < :id',
  'params' => [':id' => 20]
])->all();

# get rows with key => values pairs
$rows = $model->getIndexedUsersByCondition([
  'select' => 'salt, user_name',
  'condition' => '`users_id` < :id',
  'params' => [':id' => 20]
])->all();

# get one column indexed numerically
$rows = $model->getColumnUsersByCondition([
  'select' => 'user_name',
  'condition' => '`users_id` < :id',
  'params' => [':id' => 20]
])->all();

# group and index the result set by the first column specified in the query (`user_profiles_id` in this case)
$rows = $model->getGroupedUsersByCondition([
  'select' => 'user_profiles_id, user_name, email',
  'condition' => '`users_id` < :id',
  'params' => [':id' => 100]
])->all();

# get users with email equal to this value
$model->getAssocUsersByEmail('some-mailbox@some-non-domain.con')->all();

# get users with email containing this value (like %%)
$model->getAssocUsersLikeEmail('some-mailbox')->all();

# get users as stdClass objects with email containing this value (like %%)
$rows = $model->getObjectsUsersLikeEmail('kris_ro')->all();

# this will get you the whole table
$rows = $model->getallUsers();

# update values
$updateCount = $model->updateUsersByCondition([
  'condition' => '`users_id` = :id',
  'params' => [':id' => 60],
  'values' => [
    'email' => 'tralala@some-domain.toto',
  ],
]);

# update a row by its primary key determined as {table_name}_id
$updated = $model->updateUsers([
  'users_id' => 1200,
  'email' => 'tralala-again@some-domain.toto',
]);

# delete users
$deleted = $model->deleteUsersByCondition([
  'condition' => '`users_id` = :id',
  'params' => [':id' => 101],
]);

# delete row by its primary key determined as {table_name}_id
$deleted = $model->deleteUsers(102);

# counting
$count = $model->countUsersByCondition([
  'select' => 'salt, user_name',
  'condition' => '`users_id` < :id',
  'params' => [':id' => 20]
]);

# adding new records
$count = $model->setUsers([
  'user_profiles_id' => '4',
  'salt' => 'ecvfr56765ty',
  'user_name' => 'Test Test',
  'email' => 'test.test@some-domain.toto',
]);

# save the record and return the last inserted ID
$id = $model->setUsersAndGetId([
  'user_profiles_id' => '4',
  'salt' => 'cx6uykuy',
  'user_name' => 'Another Test',
  'email' => 'another.test@some-domain.toto',
]);

# batch insert; returns the number of rows inserted
$count = $model->setUsers([
  [
    'user_profiles_id' => '4',
    'salt' => 'ecvfr56765ty',
    'user_name' => 'Test Test',
    'email' => bin2hex(random_bytes(8)) . '-testy.test@some-domain.toto',
  ],
  [
    'user_profiles_id' => '4',
    'salt' => 'cx6uykuy',
    'user_name' => 'Another Test',
    'email' => bin2hex(random_bytes(8)) . '-another.testy@some-domain.toto',
  ]
]);
```

License
-------

[](#license)

[MIT](https://choosealicense.com/licenses/mit/)

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance36

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity42

Maturing project, gaining track record

 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

Every ~8 days

Total

4

Last Release

593d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/37bd75c9e8017bb2ffd7fc58e0e9b9aea5f335df3d2d7ca84805fd6436a1ace4?d=identicon)[kris-ro](/maintainers/kris-ro)

---

Top Contributors

[![kris-ro](https://avatars.githubusercontent.com/u/56273996?v=4)](https://github.com/kris-ro "kris-ro (6 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/kris-ro-php-database-model/health.svg)

```
[![Health](https://phpackages.com/badges/kris-ro-php-database-model/health.svg)](https://phpackages.com/packages/kris-ro-php-database-model)
```

###  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)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90340.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

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

PHPackages © 2026

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