Skip to content

Commit 892683e

Browse files
authored
DQL custom functions: document TypedExpression
Partially related to doctrine#11537
1 parent 1fe1a6a commit 892683e

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 the type is always the same, your custom function can
239+
implement ``Doctrine\ORM\Query\AST\TypedExpression``. Then, the result is wired
240+
through ``convertToPhpValue`` method of the ``Type`` returned from ``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)