PHPackages                             crimanne/abstract-repo - 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. crimanne/abstract-repo

ActiveLibrary

crimanne/abstract-repo
======================

A small, lightweight library for abstracting repositories and avoiding writing a lot of repository logic.

0.3.3(1y ago)180MITPHPPHP ^8.3

Since Jul 7Pushed 1y ago1 watchersCompare

[ Source](https://github.com/CriManne/AbstractRepo)[ Packagist](https://packagist.org/packages/crimanne/abstract-repo)[ RSS](/packages/crimanne-abstract-repo/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (2)Versions (15)Used By (0)

AbstractRepo
============

[](#abstractrepo)

About
-----

[](#about)

This project is a small, lightweight library for abstracting repositories and avoiding writing a lot of repository logic. It uses [Reflection](https://www.php.net/manual/en/book.reflection.php) and [PHP 8 Attributes](https://www.php.net/manual/en/language.attributes.overview.php) on the abstract class (*AbstractRepository*) that provides basic [CRUD](https://en.wikipedia.org/wiki/Create,_read,_update_and_delete) methods.

### Methods available:

[](#methods-available)

- *find* : retrieves every record
- *findFirst* : retrieves the first record
- *findByQuery* : retrieves every record that matches the given query
- *findById* : retrieves a specific record
- *findWhere* : retrieves every record matching the where clause
- *save* : saves the model passed
- *update* : updates the model passed
- *delete* : deletes the model from the id
- *getMappedObject* : returns a new instance of the model passed as an array

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

[](#installation)

You can install this library using [composer](https://getcomposer.org/) with the following command:

```
composer require crimanne/abstract-repo

```

Usage
-----

[](#usage)

Basically, you need to define the ***Entity*** and the repository that will use it.

### Entity

[](#entity)

You need to define a class which is the entity already created in the database and give it the ***Entity*** attribute. The Entity attributes requires the name of the database table as parameter. The model must implement the ***IModel*** interface, as shown below:

E.g.

```
#[Entity(tableName: 'Foo')]
class Foo implements IModel{

}

```

Then you need to define the fields that must match the ones in the database.

### Primary Key

[](#primary-key)

To define a primary key, you have to use the ***PrimaryKey*** attribute on the field. The attribute accepts a boolean in the constructor indicating whether the primary key is **auto increment**.

E.g.

```
...
#[PrimaryKey(autoIncrement: true)]
public int $id,
...

```

### Foreign Key (ManyToOne, OneToOne, OneToMany)

[](#foreign-key-manytoone-onetoone-onetomany)

When working with a foreign key in an entity, you can specify one of the following attributes:

- ManyToOne: This attribute enables you to have a child object directly within the parent class.
- OneToOne: Similar to ManyToOne, this attribute adds a constraint check when attempting to insert another record with the same foreign key.
- OneToMany: This attribute allows you to specify an array containing all the related entity IDs when fetching the data.

Both ManyToOne and OneToOne attributes require the **columnName** parameter to be provided in the constructor. This parameter is used to map the foreign key with the column name in the database.

E.g.

```
...
#[ManyToOne(columnName: 'bar_id')
public Bar $bar,
...

```

To have a OneToMany relation you need to declare a nullable array, this will contain all the related ids. The attribute requires two params:

- referencedColumn: the database column name in the referenced table.
- referenceClass: the referenced class

In the example you can see a OneToMany relationship between Author and Book. In the table book there must be the column author\_id in order to complete the relationship.

```
#[Entity('Author')
class Author implements IModel
    ...
    #[OneToMany(
        referencedColumn: 'author_id',
        referencedClass: Book::class
    )]
    public ?array $books = null
    ...

```

### Searchable

[](#searchable)

The repository offers an utility method called **findByQuery** that will accept a query search term, and it will retrieve all the records that match (even partially) that query. In order to do that you need to specify in the model which fields can be included in the research by using the attribute **Searchable**. If the attribute is put on a OneToOne or ManyToOne relationship property, the method will also look for all the searchable fields in the related model. Please note that this process will get only the searchable fields of the direct related entity, and it will not go further than one level of nesting.

E.g.

```
...
#[Searchable]
public string $field,
...

```

### Repository

[](#repository)

After defining the entity, you have to create the repository that must extends the ***AbstractRepository*** class. Then you need to define the ***getModel*** method to return the classname of the entity that you want to handle in that repository.

E.g.

```
class FooRepository extends AbstractRepository implements IRepository{

      ...
      static public function getModel():string{
	      return Foo::class;
      }
	  ...

}

```

The repository needs a PDO instance in construction, this can also be done with [Dependency Injection](https://php-di.org/doc/understanding-di.html).

Demo
----

[](#demo)

A small demo project can be found in the ***[demo/](demo/)*** folder

Copyright 2024 by [Cristian Mannella](https://cristianmannella.vercel.app/)

[AbstractRepo](https://github.com/CriManne/AbstractRepo) by [Cristian Mannella](https://cristianmannella.vercel.app/) is licensed under [Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International](https://creativecommons.org/licenses/by-nc-nd/4.0/?ref=chooser-v1)

[![CC](https://camo.githubusercontent.com/333c4e037d0e34b1f6e40a01fa339d5cbc58fe48a991555fb1024b11c32890c1/68747470733a2f2f6d6972726f72732e6372656174697665636f6d6d6f6e732e6f72672f70726573736b69742f69636f6e732f63632e7376673f7265663d63686f6f7365722d7631)](https://camo.githubusercontent.com/333c4e037d0e34b1f6e40a01fa339d5cbc58fe48a991555fb1024b11c32890c1/68747470733a2f2f6d6972726f72732e6372656174697665636f6d6d6f6e732e6f72672f70726573736b69742f69636f6e732f63632e7376673f7265663d63686f6f7365722d7631) [![BY](https://camo.githubusercontent.com/74168e3b629c6af60738544e209b6df90d4c160d90843066cbaa06f1a83ecfd2/68747470733a2f2f6d6972726f72732e6372656174697665636f6d6d6f6e732e6f72672f70726573736b69742f69636f6e732f62792e7376673f7265663d63686f6f7365722d7631)](https://camo.githubusercontent.com/74168e3b629c6af60738544e209b6df90d4c160d90843066cbaa06f1a83ecfd2/68747470733a2f2f6d6972726f72732e6372656174697665636f6d6d6f6e732e6f72672f70726573736b69742f69636f6e732f62792e7376673f7265663d63686f6f7365722d7631) [![NC](https://camo.githubusercontent.com/3f6dab230574ce2a05ecbc469d80d33391823346c3ead7593466924964046925/68747470733a2f2f6d6972726f72732e6372656174697665636f6d6d6f6e732e6f72672f70726573736b69742f69636f6e732f6e632e7376673f7265663d63686f6f7365722d7631)](https://camo.githubusercontent.com/3f6dab230574ce2a05ecbc469d80d33391823346c3ead7593466924964046925/68747470733a2f2f6d6972726f72732e6372656174697665636f6d6d6f6e732e6f72672f70726573736b69742f69636f6e732f6e632e7376673f7265663d63686f6f7365722d7631) [![ND](https://camo.githubusercontent.com/7515c6b453dd531bdf105834b0835cff28a43ecbaf5ff1986a3b90a34ca3737b/68747470733a2f2f6d6972726f72732e6372656174697665636f6d6d6f6e732e6f72672f70726573736b69742f69636f6e732f6e642e7376673f7265663d63686f6f7365722d7631)](https://camo.githubusercontent.com/7515c6b453dd531bdf105834b0835cff28a43ecbaf5ff1986a3b90a34ca3737b/68747470733a2f2f6d6972726f72732e6372656174697665636f6d6d6f6e732e6f72672f70726573736b69742f69636f6e732f6e642e7376673f7265663d63686f6f7365722d7631)

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance33

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity56

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

Recently: every ~18 days

Total

14

Last Release

669d ago

PHP version history (3 changes)0.1.0PHP ^8.0

0.1.2PHP ^8.2

0.2.5PHP ^8.3

### Community

Maintainers

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

---

Top Contributors

[![CriManne](https://avatars.githubusercontent.com/u/58364950?v=4)](https://github.com/CriManne "CriManne (161 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/crimanne-abstract-repo/health.svg)

```
[![Health](https://phpackages.com/badges/crimanne-abstract-repo/health.svg)](https://phpackages.com/packages/crimanne-abstract-repo)
```

PHPackages © 2026

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