Skip to content

Commit 0587323

Browse files
janedbalondrejmirtes
authored andcommitted
Readme: how to type custom DQL functions
1 parent 6339dff commit 0587323

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,3 +242,47 @@ abstract class Uuid7Entity
242242
private Uuid7 $hsCode;
243243

244244
```
245+
246+
## Custom DQL functions
247+
248+
Any custom DQL function that implements Doctrine's `TypedExpression` is understood by this extension and is inferred with the type used in its `getReturnType()` method.
249+
All other custom DQL functions are inferred as `mixed`.
250+
Please note that you cannot use native `StringType` to cast (and infer) string results (see [ORM issue](https://github.com/doctrine/orm/issues/11537)).
251+
252+
```php
253+
254+
use Doctrine\DBAL\Types\Type;
255+
use Doctrine\DBAL\Types\Types;
256+
use Doctrine\ORM\Query\AST\TypedExpression;
257+
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
258+
use Doctrine\ORM\Query\Parser;
259+
use Doctrine\ORM\Query\SqlWalker;
260+
use Doctrine\ORM\Query\TokenType;
261+
262+
class Floor extends FunctionNode implements TypedExpression
263+
{
264+
private AST\Node|string $arithmeticExpression;
265+
266+
public function getSql(SqlWalker $sqlWalker): string
267+
{
268+
return 'FLOOR(' . $sqlWalker->walkSimpleArithmeticExpression($this->arithmeticExpression) . ')';
269+
}
270+
271+
public function parse(Parser $parser): void
272+
{
273+
$parser->match(TokenType::T_IDENTIFIER);
274+
$parser->match(TokenType::T_OPEN_PARENTHESIS);
275+
276+
$this->arithmeticExpression = $parser->SimpleArithmeticExpression();
277+
278+
$parser->match(TokenType::T_CLOSE_PARENTHESIS);
279+
}
280+
281+
282+
public function getReturnType(): Type
283+
{
284+
return Type::getType(Types::INTEGER);
285+
}
286+
}
287+
288+
```

0 commit comments

Comments
 (0)