PHPackages                             tzachi/phalcon-repository - 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. tzachi/phalcon-repository

ActiveLibrary

tzachi/phalcon-repository
=========================

Phalcon repository is a library for implementing the repositories pattern in projects that use the Phalcon framework

011PHP

Since Jul 22Pushed 6y ago1 watchersCompare

[ Source](https://github.com/TimoZachi/phalcon-repository)[ Packagist](https://packagist.org/packages/tzachi/phalcon-repository)[ RSS](/packages/tzachi-phalcon-repository/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependenciesVersions (2)Used By (0)

Phalcon Repository
==================

[](#phalcon-repository)

[![License](https://camo.githubusercontent.com/e8c72e56e8f74c73ec538924e6b825de7e09c11f901e7def4d2c3a74e3dbd25d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f747a616368692f7068616c636f6e2d7265706f7369746f72792e737667)](https://packagist.org/packages/tzachi/phalcon-repository)[![Total Downloads](https://camo.githubusercontent.com/f2e70d8909023bdf868543f5db44cdb45eecb2d4b64ebbb97c50733b1519e0db/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f747a616368692f7068616c636f6e2d7265706f7369746f72792e737667)](https://packagist.org/packages/tzachi/phalcon-repository)[![Latest Stable Version](https://camo.githubusercontent.com/5f948c92d2603cd5b680c31ddba5e7dda90d59b01bc0e9366f49a339e617b35b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f747a616368692f7068616c636f6e2d7265706f7369746f72792e7376673f636f6c6f723d626c7565266c6162656c3d737461626c65)](https://packagist.org/packages/tzachi/phalcon-repository)[![Unstable Version](https://camo.githubusercontent.com/c9a7a8398d2913231fec69d6a12f2b2983709975ceb7625ab8fea7f9c1476cd7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f767072652f747a616368692f7068616c636f6e2d7265706f7369746f72792e7376673f6c6162656c3d756e737461626c65)](https://packagist.org/packages/tzachi/phalcon-repository)

[![Branch master](https://camo.githubusercontent.com/d13ff590538bd288fc9f75cbfe383af4eaaaa0c356f9e90aed2263bb322bd87f/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6272616e63682d6d61737465722d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/d13ff590538bd288fc9f75cbfe383af4eaaaa0c356f9e90aed2263bb322bd87f/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6272616e63682d6d61737465722d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)[![Build Status](https://camo.githubusercontent.com/fb93da6a4c2be78d520b047da0d43137dc81805914555fecec9fbb16df0e5f12/68747470733a2f2f7472617669732d63692e6f72672f54696d6f5a616368692f7068616c636f6e2d7265706f7369746f72792e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/TimoZachi/phalcon-repository)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/5a2a42e14064346610630636faee9aab314a37e44265e3e350e93e26e01a9035/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f54696d6f5a616368692f7068616c636f6e2d7265706f7369746f72792f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/TimoZachi/phalcon-repository/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/30598a9947667447948b657b54e16413e538a5d7b15252b59590dfeee1520312/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f54696d6f5a616368692f7068616c636f6e2d7265706f7369746f72792f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/TimoZachi/phalcon-repository/?branch=master)

Phalcon Repository is a library for implementing the repository pattern in projects that use the Phalcon PHP Framework

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

[](#installation)

Installation is done via composer: `composer require tzachi/phalcon-repository:0.1.x-dev`

Usage
-----

[](#usage)

Assuming you have a model (that extends Phalcon's Model) called `User`, you can use the repository like this:

```
use MyProject\Model\User;
use TZachi\PhalconRepository\ModelWrapper;
use TZachi\PhalconRepository\Repository;
use TZachi\PhalconRepository\Resolver\QueryParameter;

$userRepository = new Repository(new ModelWrapper(User::class));

// Simple usage, finds a user model by its id.
$user = $userRepository->findFirst(1);

// Finds a user model by its name, ordering by name ASC.
$user = $userRepository->findFirstBy('name', 'Test User Name', ['name' => 'ASC']);

// A bit more of complexity here, this will find a user using the following condition:
// id BETWEEN 15 AND 21 OR id IN (1, 2, 3) OR (name = 'Timo Zachi' AND created_at > '2019-01-01')
// ordering the results by id DESC.
$user = $userRepository->findFirstWhere(
    [
        '@type' => QueryParameter::TYPE_OR,
        [
            '@operator' => 'BETWEEN',
            'id' => [15, 21], // Between operator.
        ],
        'id' => [1, 2, 3], // In operator.
        [
            // Type AND is default, doesn't need to be specified. It's explicit here for sample purposes.
            '@type' => QueryParameter::TYPE_AND,
            'name' => 'Timo Zachi', // Equals operator (default).
            [
                '@operator' => '>',
                'createdAt' => '2019-01-01',
            ],
        ],
    ],
    ['id' => 'DESC'] // Order by.
);

// You can use the same parameters to query for multiple records, that will return a result set.
// You need only to use one of the methods 'findWhere' or 'findBy'. Notice that there is also a
// limit and offset parameter.
$resultSet = $userRepository->findWhere(
    ['email' => 'timo.zachi@timoteo.me'], // Conditions.
    ['id' => 'DESC'], // Order by.
    10, // Limit.
    5 // Offset.
);

// Aggregation methods

// Count number of users in table
$userRepository->count();

// Get the minimum value of the `createdAt` colum using a where condition
$userRepository->minimum('createdAt', ['id' => [100, 200], '@operator' => 'BETWEEN']);
// Get the maximum value of the `name` column in the entire table
$userRepository->maximum('name');

// Returns the sum of the balance column on users with id 40, 41 and 42
$userRepository->sum('balance', ['id' => [40, 41, 42]]);

// Returns the average of the balance column on users with id 40, 41 and 42
$userRepository->average('balance', ['id' => [40, 41, 42]]);
```

To set up the repository pattern inside a phalcon project, you can use the `RepositoryFactory` class:

```
use Phalcon\Annotations\AdapterInterface as AnnotationsAdapterInterface;
use TZachi\PhalconRepository\RepositoryFactory;

// Set the repository service as a shared service
$di->setShared(
    'repositoryManager',
    function (): RepositoryFactory {
        /**
         * @var AnnotationsAdapterInterface $annotations
         */
        $annotations = $this->get('annotations');

        // The repository factory reads the annotations of the Model class to determine which repository it should use,
        // that's why it needs the annotations parser. It falls back to the default repository class if one wasn't
        // specified in the model
        return new RepositoryFactory($annotations);
    }
);
```

Now if you want a specific Repository for a specific Model, first create the repository:

```
namespace MyApp\Repository;

use TZachi\PhalconRepository\Repository;

class UserRepository extends Repository
{
    public function findLastUserCreated(): User
    {
        return $this->findFirstWhere([], ['id' => 'DESC']);
    }
```

And then, in the model, add the annotation to specify which class should be its repository:

```
namespace MyApp\Model;

use MyApp\Model\User;
use Phalcon\Mvc\Model;

/**
 * @Repository(MyApp\Repository\UserRepository);
 */
class User extends Model
{
    ...
```

Now anywhere in your project you can easily get the model's repository:

```
public function userAction($id): void
{
    /**
     * @var \MyApp\Repository\UserRepository $userRepository
     */
    $userRepository = $this->repositoryManager->get(\MyApp\Model\User::class);
    $user = $userRepository->findLastUserCreated($id);
    ...
```

Contributing
------------

[](#contributing)

Pull requests are most certainly welcome. The code will be validated against the following checks:

- Code is validated against the [Coding Standard](https://github.com/timozachi/phalcon-repository)
- A static analysis tool (phpstan) will analyse your code
- Unit tests must have 100% code coverage (if you create a new feature, please ensure that there are enough tests to cover it)
- For some features, a functional test is required

To make things simpler, a Makefile was created. All you have to do is run:

```
make
```

The `make` command will run all available checks against your code and inform you if any of them fails

If you want to run specific checks:

```
# check code style
make cs-check
# static analysis on src directory
make phpstan-src
# static analysis on tests directory
make phpstan-tests
# run unit tests on php 7.3
make test-unit-php73
# run functional tests on php 7.3
make test-functional-php73
```

To fix cs automatically (not all rules can be fixed automatically):

```
make cs-fix
```

Notes
-----

[](#notes)

Please note that there is still a lot to be done in this project and it is still under development. Hopefully when its done, phalcon will have released its 4.0 version so that this project will be able to use it

###  Health Score

19

—

LowBetter than 10% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity38

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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/99606fac343834244166490ce828e394f2f0f7a516784b97916575b68248e7e0?d=identicon)[TZachi](/maintainers/TZachi)

---

Top Contributors

[![timozachi](https://avatars.githubusercontent.com/u/2420231?v=4)](https://github.com/timozachi "timozachi (26 commits)")

### Embed Badge

![Health badge](/badges/tzachi-phalcon-repository/health.svg)

```
[![Health](https://phpackages.com/badges/tzachi-phalcon-repository/health.svg)](https://phpackages.com/packages/tzachi-phalcon-repository)
```

PHPackages © 2026

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