findMany

Documentation for the findMany method, which retrieves multiple User records meeting specified filter criteria.

Purpose

The findMany method retrieves multiple User records that match specified filter criteria. It supports filtering, sorting, pagination, selective field returns, cursor-based pagination, and related model inclusion. If no records match, an empty array is returned.

Parameters

  • 'where' – Filter criteria for the query.
  • 'orderBy' – Sorting rules for result ordering.
  • 'take' – Number of records to retrieve.
  • 'skip' – Number of records to skip before retrieving results.
  • 'cursor' – Cursor-based pagination starting point.
  • 'select' – Fields to return in the result set.
  • 'include' – Related models to include in the result.
  • 'distinct' – Ensures that records returned are unique.

Return Value

Returns an array of objects matching the specified criteria. If requesting User data, an array of User objects will be returned. If no matching records exist, an empty array is returned.

Exception Handling

  • Throws an exception if include and select are both used.
  • Throws an exception if invalid query criteria are provided.

These safeguards ensure reliable and secure data retrieval.

Features

  • Supports complex queries with filtering, sorting, and pagination.
  • Allows selective field retrieval and related model inclusion.
  • Enables cursor-based pagination for advanced dataset navigation.
  • Validates inputs to ensure safe and accurate query execution.
  • Supports distinct results to prevent duplicates.

Example Usage

Example Usage: Default Call

use Lib\Prisma\Classes\Prisma;

$prisma = Prisma::getInstance();
$users = $prisma->user->findMany();

echo "<pre>";
print_r($users);
echo "</pre>";

    

Example Usage: Filtering with Conditions

use Lib\Prisma\Classes\Prisma;

$prisma = Prisma::getInstance();
$users = $prisma->user->findMany([
    'where' => [
        'name' => [
            'contains' => 'John Doe'
        ]
    ],
    'distinct' => true,
    'take' => 10,
    'skip' => 0,
    'orderBy' => [
        'name' => 'asc'
    ],
]);

echo "<pre>";
print_r($users);
echo "</pre>";

    

Example Usage: Applying Filters and Pagination

use Lib\Prisma\Classes\Prisma;

$prisma = Prisma::getInstance();
$users = $prisma->user->findMany([
    'take' => 10,
    'skip' => 0,
    'orderBy' => [
        'name' => 'asc'
    ],
]);

echo "<pre>";
print_r($users);
echo "</pre>";

    

Example Usage: Using include

use Lib\Prisma\Classes\Prisma;

$prisma = Prisma::getInstance();
$users = $prisma->user->findMany([
    'where' => [
        'email' => [
            'contains' => 'example@email.com'
        ]
    ],
    'take' => 5,
    'include' => [
        'profile' => true
    ]
]);

echo "<pre>";
print_r($users);
echo "</pre>";

    

Example Usage: Using select

use Lib\Prisma\Classes\Prisma;

$prisma = Prisma::getInstance();
$users = $prisma->user->findMany([
    'where' => [
        'name' => [
            'contains' => 'Doe'
        ]
    ],
    'orderBy' => [
        'name' => 'asc'
    ],
    'take' => 5,
    'select' => [
        'id' => true,
        'name' => true,
        'email' => true,
        'profile' => [
            'select' => [
                'bio' => true
            ]
        ]
    ]
]);

echo "<pre>";
print_r($users);
echo "</pre>";

    

Example Usage: Advanced Filtering Options

Advanced filtering includes:

  • contains
  • startsWith
  • endsWith
  • in
  • notIn
  • lt
  • lte
  • gt
  • gte
  • equals
  • not
use Lib\Prisma\Classes\Prisma;

$prisma = Prisma::getInstance();
$users = $prisma->user->findMany([
    'where' => [
        'name' => [
            'contains' => 'Doe',
            'startsWith' => 'J',
            'endsWith' => 'e'
        ],
        'age' => [
            'gt' => 25,
            'lt' => 35
        ],
        'email' => [
            'in' => [
                'example1@email.com',
                'example2@email.com'
            ],
            'notIn' => [
                'admin@email.com'
            ]
        ],
        'status' => [
            'equals' => 'active',
            'not' => 'suspended'
        ]
    ],
    'orderBy' => [
        'name' => 'asc',
        'age' => 'desc'
    ],
    'take' => 10,
]);

echo "<pre>";
print_r($users);
echo "</pre>";

    

Understanding Logical Operators: AND, OR, NOT

Logical operators allow combining multiple conditions inside the where clause.

AND

All conditions must be true for the record to match.

OR

Matches records that satisfy at least one condition.

NOT

Excludes records that match the specified criteria.

Example Usage

use Lib\Prisma\Classes\Prisma;

$prisma = Prisma::getInstance();
$users = $prisma->user->findMany([
    'where' => [
        'OR' => [
            [
                'email' => [
                    'contains' => 'gmail.com'
                ]
            ],
            [
                'AND' => [
                    [
                        'name' => [
                            'contains' => 'Reina'
                        ]
                    ],
                    [
                        'isActive' => true
                    ]
                ]
            ]
        ],
        'NOT' => [
            'status' => [
                'equals' => 'suspended'
            ]
        ]
    ]
]);

echo "<pre>";
print_r($users);
echo "</pre>";