Skip to content

Commit

Permalink
DQL custom functions: document TypedExpression
Browse files Browse the repository at this point in the history
Partially related to doctrine#11537

Co-authored-by: Claudio Zizza <859964+SenseException@users.noreply.github.com>
  • Loading branch information
janedbal and SenseException committed Aug 6, 2024
1 parent 1fe1a6a commit 3f550c1
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions docs/en/cookbook/dql-user-defined-functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,33 @@ vendors SQL parser to show us further errors in the parsing
process, for example if the Unit would not be one of the supported
values by MySql.

Typed functions
---------------
By default, result of custom functions is fetched as-is from the database driver.
If you want to be sure that the type is always the same, then your custom function needs to
implement ``Doctrine\ORM\Query\AST\TypedExpression``. Then, the result is wired
through ``Doctrine\DBAL\Types\Type::convertToPhpValue()`` of the ``Type`` returned in ``getReturnType()``.

.. code-block:: php
<?php
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
use Doctrine\ORM\Query\AST\TypedExpression;
class DateDiff extends FunctionNode implements TypedExpression
{
// ...
public function getReturnType(): Type
{
return Type::getType(Types::INTEGER);
}
}
Conclusion
----------

Expand Down

0 comments on commit 3f550c1

Please sign in to comment.