tourze / doctrine-function-collection
Doctrine Function Collection
0.0.1
2025-04-12 10:19 UTC
Requires
- php: ^8.1
- beberlei/doctrineextensions: ^1.5.0
- doctrine/orm: ^2.17 || ^3.0
- scienta/doctrine-json-functions: ^6.3.0
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^10.0
This package is auto-updated.
Last update: 2025-04-17 13:32:33 UTC
README
A collection of Doctrine ORM SQL functions that can be used in DQL queries.
Features
- Provides a unified interface for various SQL functions
- Supports date/time functions
- Supports string functions
- Supports JSON functions
- Easy to use and register with Doctrine
Installation
composer require tourze/doctrine-function-collection
Quick Start
Register the functions with Doctrine Configuration:
<?php use Doctrine\ORM\Configuration; use Tourze\DoctrineFunctionCollection\DatetimeFunction\Day; use Tourze\DoctrineFunctionCollection\StringFunction\IfElse; use Tourze\DoctrineFunctionCollection\JsonFunction\JsonContains; /** @var Configuration $config */ $config->addCustomDatetimeFunction('day', Day::class); $config->addCustomStringFunction('if', IfElse::class); $config->addCustomStringFunction('json_contains', JsonContains::class);
Use the functions in your DQL:
$query = $entityManager->createQuery(' SELECT e FROM App\Entity\Event e WHERE DAY(e.createdAt) = :day AND JSON_CONTAINS(e.properties, :value) = 1 '); $query->setParameters([ 'day' => 15, 'value' => '"example"', ]);
Available Functions
Date/Time Functions
Day
- Returns the day of the month (0-31)Hour
- Returns the hour (0-23)Minute
- Returns the minute (0-59)Month
- Returns the month (1-12)Week
- Returns the week numberWeekDay
- Returns the weekday indexYear
- Returns the year
String Functions
AnyValue
- Returns any value from a setDateDiff
- Returns the difference between two datesField
- Returns the index positionFindInSet
- Returns the position in a setIfElse
- If-else condition in SQLRand
- Random number generator
JSON Functions
JsonArray
- Creates a JSON arrayJsonContains
- Checks if JSON document contains specific valueJsonExtract
- Extracts value from JSON documentJsonLength
- Returns the length of JSON documentJsonSearch
- Searches JSON document
Running Tests
To run the unit tests for this package:
# From the monorepo root directory
phpunit packages/doctrine-function-collection/tests
Contributing
Please see CONTRIBUTING.md for details.
License
The MIT License (MIT). Please see License File for more information.