Skip to content

Commit 3f550c1

Browse files
DQL custom functions: document TypedExpression
Partially related to doctrine#11537 Co-authored-by: Claudio Zizza <859964+SenseException@users.noreply.github.com>
1 parent 1fe1a6a commit 3f550c1

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

docs/en/cookbook/dql-user-defined-functions.rst

+27
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,33 @@ vendors SQL parser to show us further errors in the parsing
232232
process, for example if the Unit would not be one of the supported
233233
values by MySql.
234234

235+
Typed functions
236+
---------------
237+
By default, result of custom functions is fetched as-is from the database driver.
238+
If you want to be sure that the type is always the same, then your custom function needs to
239+
implement ``Doctrine\ORM\Query\AST\TypedExpression``. Then, the result is wired
240+
through ``Doctrine\DBAL\Types\Type::convertToPhpValue()`` of the ``Type`` returned in ``getReturnType()``.
241+
242+
.. code-block:: php
243+
244+
<?php
245+
246+
use Doctrine\DBAL\Types\Type;
247+
use Doctrine\DBAL\Types\Types;
248+
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
249+
use Doctrine\ORM\Query\AST\TypedExpression;
250+
251+
class DateDiff extends FunctionNode implements TypedExpression
252+
{
253+
// ...
254+
255+
public function getReturnType(): Type
256+
{
257+
return Type::getType(Types::INTEGER);
258+
}
259+
}
260+
261+
235262
Conclusion
236263
----------
237264

0 commit comments

Comments
 (0)