PHPackages                             rugolinifr/enhanced-find-by - 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. rugolinifr/enhanced-find-by

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

rugolinifr/enhanced-find-by
===========================

An API providing an enhanced `findBy()` method to fetch entities from a Doctrine ORM repository.

v1.2.0(2w ago)06MITPHP ^8.1

Since Jul 5Compare

[ Source](https://github.com/rugolinifr/enhanced-find-by)[ Packagist](https://packagist.org/packages/rugolinifr/enhanced-find-by)[ RSS](/packages/rugolinifr-enhanced-find-by/feed)WikiDiscussions Synced 1w ago

READMEChangelog (2)Dependencies (8)Versions (4)Used By (0)

Enhanced findBy() method
========================

[](#enhanced-findby-method)

This package provides an API similar to the famous Doctrine `findBy()` method, but offering more capabilities to execute more complex (but still simple) queries.

Context
-------

[](#context)

The original Doctrine `findBy()` method is pretty limited:

- it provides only two SQL operators: `=` and `IN ()`,
- it filters only on the properties owned by the target entity,

whereas the *enhanced* `findBy()` method:

- offers many SQL operators: `=`, `IN ()`, `!=`, `NOT IN()`, `=`, `LIKE`, `NOT LIKE`, `IS NULL`, `IS NOT NULL`,
- filters on properties from other entities (see below),
- sorts the result set when needed,
- limits the number of fetched entities on demand.

When the enhanced `findBy()` method filters on properties from other entities, it handles by itself every mandatory `INNER JOIN` clause, removing the need for the developer to use the DQL or the `QueryBuilder` API.

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

[](#installation)

Run `composer require rugolinifr/enhanced-find-by`.

Usage
-----

[](#usage)

Developers should use only:

- the `Rugolinifr\EnhancedFindBy\Factory\EnhancedFindByFactory` factory,
- any class from the `Rugolinifr\EnhancedFindBy\Contract` namespace.

Other classes are considered `@internal`.

Basic usage examples:

```
$finder = (new Rugolinifr\EnhancedFindBy\Factory\EnhancedFindByFactory())
    ->createEnhancedFindBy($entityManager);

// fetches every `Person` entity
$entities = $finder->findBy(Person::class);

// fetches every newborn `Person` entity using the "greater than" operator
$newborns = $finder->findBy(Person::class, ['birth >' => new \DateTimeImmutable('yesterday')]);

// fetches every newborn girl `Person`
$newborns = $finder->findBy(
    Person::class,
    [
        'birth >' => new \DateTimeImmutable('yesterday'),
        'sex =' => GenderEnum::GIRL,
    ],
);

// fetches every extra-European `Person` entity with implicit joins on two other entities
$extraEuropeans = $finder->findBy(Person::class, ['address.country.continent !=' => 'Europe']);

// fetches every `Person` entity having long black hair from a Doctrine embeddable
$blackHairedPersons = $finder->findBy(
    Person::class,
    [
        'hair->length >' => 20,
        'hair->color =' => 'black',  // Use arrow ("->") notation to filter against a Doctrine embeddable
    ],
);
```

### The `$from` parameter

[](#the-from-parameter)

The `$from` parameter takes the class name of the entity to fetch as argument. As usual, it may be retrieved from either the `get_class()` function or the `::class` keyword.

### The `$where` parameter

[](#the-where-parameter)

The `$where` parameter takes an array as argument. This array is a sequence where each key is an existing entity property, followed by a space, terminated by an *operator*.

On the other hand, the value type is mixed and depends on the operator name and the property type.

The key-value pair represents a boolean expression; an entity matching **every** expression will be returned by the `findBy()` method. The expressions are translated into DQL in the order they are declared in the array.

For example, the following PHP code:

```
$finder->findBy(
    Person::class,
    [
        'name like' => 'John%',
        'birth
