PHPackages                             mattvb91/lightmodel - 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. mattvb91/lightmodel

ActivePackage

mattvb91/lightmodel
===================

Lightweight DB model

0.3(7y ago)135[1 issues](https://github.com/mattvb91/LightModel/issues)1MITPHPPHP &gt;=7.1

Since Jun 16Pushed 7y ago1 watchersCompare

[ Source](https://github.com/mattvb91/LightModel)[ Packagist](https://packagist.org/packages/mattvb91/lightmodel)[ RSS](/packages/mattvb91-lightmodel/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (3)Dependencies (2)Versions (5)Used By (1)

**LightModel**

 [![](https://camo.githubusercontent.com/b1e75df7f26d4bc4ef360691c14aa56aec2f0071e1bf2646f0be1c050f116eb0/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6d617474766239312f4c696768744d6f64656c2f62616467652e7376673f6272616e63683d6d6173746572)](https://camo.githubusercontent.com/b1e75df7f26d4bc4ef360691c14aa56aec2f0071e1bf2646f0be1c050f116eb0/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6d617474766239312f4c696768744d6f64656c2f62616467652e7376673f6272616e63683d6d6173746572) [![](https://camo.githubusercontent.com/f0c6fde119804bfa6e0d29bdd91fe4b5cda52af7d55b03058cfb4ea7437eb2ba/68747470733a2f2f7472617669732d63692e6f72672f6d617474766239312f4c696768744d6f64656c2e7376673f6272616e63683d6d6173746572)](https://camo.githubusercontent.com/f0c6fde119804bfa6e0d29bdd91fe4b5cda52af7d55b03058cfb4ea7437eb2ba/68747470733a2f2f7472617669732d63692e6f72672f6d617474766239312f4c696768744d6f64656c2e7376673f6272616e63683d6d6173746572) [![](https://camo.githubusercontent.com/a047e2889109a2bf1490f4622166b752c5c29537d102fa5c6c9288f11b81e61c/68747470733a2f2f706f7365722e707567782e6f72672f6d617474766239312f6c696768746d6f64656c2f762f737461626c65)](https://camo.githubusercontent.com/a047e2889109a2bf1490f4622166b752c5c29537d102fa5c6c9288f11b81e61c/68747470733a2f2f706f7365722e707567782e6f72672f6d617474766239312f6c696768746d6f64656c2f762f737461626c65) [![](https://camo.githubusercontent.com/ba65db23725058ab135724e68a7532375ad9e76fa7cc23af97852d0c900f2df8/68747470733a2f2f706f7365722e707567782e6f72672f6d617474766239312f6c696768746d6f64656c2f646f776e6c6f616473)](https://camo.githubusercontent.com/ba65db23725058ab135724e68a7532375ad9e76fa7cc23af97852d0c900f2df8/68747470733a2f2f706f7365722e707567782e6f72672f6d617474766239312f6c696768746d6f64656c2f646f776e6c6f616473) [![](https://camo.githubusercontent.com/8171c9c049aaacc44eb7f4948c76fad4767f067cc831df289ff1d8fb5fa6c08d/68747470733a2f2f706f7365722e707567782e6f72672f6d617474766239312f6c696768746d6f64656c2f762f756e737461626c65)](https://camo.githubusercontent.com/8171c9c049aaacc44eb7f4948c76fad4767f067cc831df289ff1d8fb5fa6c08d/68747470733a2f2f706f7365722e707567782e6f72672f6d617474766239312f6c696768746d6f64656c2f762f756e737461626c65) [![](https://camo.githubusercontent.com/f0bb4305ccdb7de23b8c7144419fcafdee0b344a6ed809a17c9deb25f80743f9/68747470733a2f2f706f7365722e707567782e6f72672f6d617474766239312f6c696768746d6f64656c2f6c6963656e7365)](https://camo.githubusercontent.com/f0bb4305ccdb7de23b8c7144419fcafdee0b344a6ed809a17c9deb25f80743f9/68747470733a2f2f706f7365722e707567782e6f72672f6d617474766239312f6c696768746d6f64656c2f6c6963656e7365)

What is LightModel?
-------------------

[](#what-is-lightmodel)

The LightModel ORM is test project to build an ActiveRecord style implementation from scratch.

Using this in a live project is not recommended. Please look at Eloquent or Zend Model for a robust and highly tested solution.

Usage
-----

[](#usage)

To initialize LightModel you need to call `LightModel::init();` and pass your instance of PDO to it.

```
$pdo = new PDO(/* Your PDO params*/);
LightModel::init($pdo);

```

You can also pass in an optional array for further configuration. For example:

```
$options = [
    LightModel::LightModel::OPTIONS_TYPECAST,
];

LightModel::init($pdo, $options);
```

Currently the typecast option is the only option available. If used this will typecast Integer columns defined in your MySQL database to be an integer attribute on your model.

To get started with a model, your class needs to extend `mattvb91\LightModel\LightModel`

### Creating a Model

[](#creating-a-model)

```
namespace Project;

use mattvb91\LightModel\LightModel;

class User extends LightModel
{

    //
}
```

You will need to implement the `getValues()` function in which you define a key value array for the values associated with your columns.

For example a basic `User` table with with the following structure could be represented like this:

```
| id (int) | username (varchar) |
```

```
namespace Project;

use mattvb91\LightModel\LightModel;

class User extends LightModel
{
    public $username;

    public function getValues()
    {
        return [
            'username' => $this->username,
        ];
    }
}
```

You do not need to manually bind the primary key column if it is set up as an auto increment value in your DB.

You can of course use your normal getters to access your column values too inside the `getValues()` method as long as you bind it to your correct column.

To create a new row on your user table you would use the following:

```
$user = new User();
$user->username = 'Name';
$user->save();
```

To override the table name or table key simply implement the following in your class:

```
    protected $tableName = 'new_name';
    protected $key = 'new_key';
```

### Fetch a row by key

[](#fetch-a-row-by-key)

To fetch a row by its key use the static `Class::getOneByKey($key)` on the class you want to fetch. For example:

```
$user = User::getOneByKey(1);
//$user now loaded with values from DB
```

### Check a model exists

[](#check-a-model-exists)

To check if a model actually exists in your database you can use the `exists()` method.

```
$user->exists(); //Returns true or false
```

### Refresh a model

[](#refresh-a-model)

You may run into situations where you need to fetch the latest version of your row again. Use the `refresh()` method to update your current model.

Keep in mind this will set your model back to whats currently in your DB.

```
$user->refresh();
```

### Delete a model

[](#delete-a-model)

To delete a model simply call the `delete()` method:

```
$user->delete();
```

Relationships
-------------

[](#relationships)

### BelongsTo

[](#belongsto)

To define a Belongs to relationship use the `belongsTo($class, $foreignKey)` method in your model. For example if our User is associated with a Department you could do the following inside your User class.

Once a relationship has been queried once any further accesses to not hit the database again.

```
public function department()
{
    return $this->belongsTo(Department::class, 'department_id');
    //returns a loaded Department::class instance
}
```

Fetching multiple rows
----------------------

[](#fetching-multiple-rows)

To fetch multiple rows simply use the `static::getItems()` method.

```
$users = User::getItems();
```

Filtering
---------

[](#filtering)

You can also filter data sets by passing an optional array int the `static::getItems()`method. You must pass the correct table column name.

```
$filter = [
    'username' => 'joe'
];

$allJoeUsers = User::getItems($filter);
```

Optionally you can also pass in the operator you want to perform. The order MUST be Table\_Column =&gt; \['Operator', 'Value'\]

```
$filter = [
    'username' => ['>=', 'joe']
];

$allJoeUsers = User::getItems($filter);
```

To set the order or limit the results returned you can make use of the `LightModel::FILTER_ORDER` and `LightModel::FILTER_LIMIT` constants and pass them in your options array:

```
$filter = [
    LightModel::FILTER_ORDER => 'id DESC',
    LightModel::FILTER_LIMIT => 100;
];

$filteredUsers = User::getItems($filter);
```

Fetching keys
-------------

[](#fetching-keys)

You may sometimes run into situations where performing a `static::getItems()` brings back too large of a resultset. You can instead perform the same filters using `static::getKeys()` which instead of returning an array of fully loaded Models it will now only return the unique column ID's that fit your filtered criteria. You can then use that ID to individually load the full model manually:

```
$filter = [
    LightModel::FILTER_ORDER => 'id DESC',
];

$userKeys = User::getKeys($filter);
//We now have an array consisting of all the primary keys that
//match our criteria

foreach($userKeys as $primaryKey)
{
    //Load the full individual record for further processing
    $user = User::getOneByKey($primaryKey);
}
```

###  Health Score

19

—

LowBetter than 10% of packages

Maintenance0

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity51

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 ~243 days

Total

3

Last Release

2769d ago

### Community

Maintainers

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

---

Top Contributors

[![mattvb91](https://avatars.githubusercontent.com/u/11991564?v=4)](https://github.com/mattvb91 "mattvb91 (25 commits)")

---

Tags

mysqlpdopdo-mysqlpdo-wrapperphpphp-libraryphp7

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/mattvb91-lightmodel/health.svg)

```
[![Health](https://phpackages.com/badges/mattvb91-lightmodel/health.svg)](https://phpackages.com/packages/mattvb91-lightmodel)
```

PHPackages © 2026

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