[PR #991] [MERGED] Ability to define custom functions with callback instead of class name #9005

Closed
opened 2026-01-22 16:02:44 +01:00 by admin · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/doctrine/orm/pull/991
Author: @mnapoli
Created: 3/27/2014
Status: Merged
Merged: 5/16/2014
Merged by: @guilhermeblanco

Base: masterHead: custom-functions-callback


📝 Commits (1)

  • c7eb42b Ability to define custom functions with callback + tests

📊 Changes

2 files changed (+90 additions, -9 deletions)

View changed files

📝 lib/Doctrine/ORM/Query/Parser.php (+18 -9)
tests/Doctrine/Tests/ORM/Functional/CustomFunctionsTest.php (+72 -0)

📄 Description

Right now the only way to define custom DQL functions is by giving the class name, and Doctrine will create the class:

$config->addCustomNumericFunction('FOO', 'My\Custom\DqlFunction');

This is very limiting when the custom functions has dependencies, for example it can't be created by a DI container.

The approach I have taken here is very simple: it allows to define a callback instead of the class name: the callback will be called and it should return the instance:

$config->addCustomNumericFunction('FOO', function($funcName) {
    return new My\Custom\DqlFunction($funcName);
});

On a side note, I think it would be great to generalize that approach because currently there are a lot of places where the same constraints apply.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/doctrine/orm/pull/991 **Author:** [@mnapoli](https://github.com/mnapoli) **Created:** 3/27/2014 **Status:** ✅ Merged **Merged:** 5/16/2014 **Merged by:** [@guilhermeblanco](https://github.com/guilhermeblanco) **Base:** `master` ← **Head:** `custom-functions-callback` --- ### 📝 Commits (1) - [`c7eb42b`](https://github.com/doctrine/orm/commit/c7eb42b04f1a1c2f363acdcd69556f1929cb168f) Ability to define custom functions with callback + tests ### 📊 Changes **2 files changed** (+90 additions, -9 deletions) <details> <summary>View changed files</summary> 📝 `lib/Doctrine/ORM/Query/Parser.php` (+18 -9) ➕ `tests/Doctrine/Tests/ORM/Functional/CustomFunctionsTest.php` (+72 -0) </details> ### 📄 Description Right now the only way to define custom DQL functions is by giving the class name, and Doctrine will create the class: ``` php $config->addCustomNumericFunction('FOO', 'My\Custom\DqlFunction'); ``` This is very limiting when the custom functions has dependencies, for example it can't be created by a DI container. The approach I have taken here is very simple: it allows to define a callback instead of the class name: the callback will be called and it should return the instance: ``` php $config->addCustomNumericFunction('FOO', function($funcName) { return new My\Custom\DqlFunction($funcName); }); ``` On a side note, I think it would be great to generalize that approach because currently there are a lot of places where the same constraints apply. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
admin added the pull-request label 2026-01-22 16:02:44 +01:00
admin closed this issue 2026-01-22 16:02:44 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#9005