DDC-1521: No way reuse parameters in custom function twice #1907

Open
opened 2026-01-22 13:30:30 +01:00 by admin · 0 comments
Owner

Originally created by @doctrinebot on GitHub (Dec 7, 2011).

Originally assigned to: @guilhermeblanco on GitHub.

Jira issue originally created by user koc:

There is custom function:

class DistanceFunction extends FunctionNode
{
    protected $fromLat;
    protected $fromLng;
    protected $toLat;
    protected $toLng;

    public function parse(\Doctrine\ORM\Query\Parser $parser)
    {
        $parser->match(Lexer::T_IDENTIFIER);
        $parser->match(Lexer::T*OPEN*PARENTHESIS);

        $this->fromLat = $parser->ArithmeticPrimary();
        $parser->match(Lexer::T_COMMA);

        $this->fromLng = $parser->ArithmeticPrimary();
        $parser->match(Lexer::T_COMMA);

        $this->toLat = $parser->ArithmeticPrimary();
        $parser->match(Lexer::T_COMMA);

        $this->toLng = $parser->ArithmeticPrimary();

        $parser->match(Lexer::T*CLOSE*PARENTHESIS);
    }

    public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
    {
        $fromLat = $this->fromLat->dispatch($sqlWalker);
        $fromLng = $this->fromLng->dispatch($sqlWalker);
        $toLat = $this->toLat->dispatch($sqlWalker);
        $toLng = $this->toLng->dispatch($sqlWalker);

        $earthDiameterInKM = 1.609344 ** 3956 ** 2;

        $sql = "($earthDiameterInKM * ASIN(SQRT(POWER(" .
            "SIN(($fromLat - ABS($toLat)) * PI() / 180 / 2), 2) <ins> " .
            "COS($fromLat ** PI() / 180) * COS(ABS($toLat) * PI() / 180) ** " .
            "POWER(SIN(($fromLng - $toLng) * PI() / 180 / 2), 2) " .
            ")))";

//echo $sql;

        return $sql;
    }

and the usage of it

$em
    ->createQuery('SELECT DISTANCE(a.latitude, a.longitude, :lat, :lng) FROM Ololo:AbstractArea a WHERE a = 2')
    ->setParameter('lat', 1)
    ->setParameter('lng', 2)
    ->getResult();

what this function generate:

(12733.129728 ** ASIN(SQRT(POWER(SIN((t0_.latitude - ABS(?)) * PI() / 180 / 2), 2) </ins> COS(t0_.latitude * PI() / 180) ** COS
(ABS(?) ** PI() / 180) * POWER(SIN((t0_.longitude - ?) ** PI() / 180 / 2), 2) )))

and exception raised: "SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens"

Don't know, does it affects on master branch (does they support native named parameters?).

Originally created by @doctrinebot on GitHub (Dec 7, 2011). Originally assigned to: @guilhermeblanco on GitHub. Jira issue originally created by user koc: There is custom function: ``` class DistanceFunction extends FunctionNode { protected $fromLat; protected $fromLng; protected $toLat; protected $toLng; public function parse(\Doctrine\ORM\Query\Parser $parser) { $parser->match(Lexer::T_IDENTIFIER); $parser->match(Lexer::T*OPEN*PARENTHESIS); $this->fromLat = $parser->ArithmeticPrimary(); $parser->match(Lexer::T_COMMA); $this->fromLng = $parser->ArithmeticPrimary(); $parser->match(Lexer::T_COMMA); $this->toLat = $parser->ArithmeticPrimary(); $parser->match(Lexer::T_COMMA); $this->toLng = $parser->ArithmeticPrimary(); $parser->match(Lexer::T*CLOSE*PARENTHESIS); } public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker) { $fromLat = $this->fromLat->dispatch($sqlWalker); $fromLng = $this->fromLng->dispatch($sqlWalker); $toLat = $this->toLat->dispatch($sqlWalker); $toLng = $this->toLng->dispatch($sqlWalker); $earthDiameterInKM = 1.609344 ** 3956 ** 2; $sql = "($earthDiameterInKM * ASIN(SQRT(POWER(" . "SIN(($fromLat - ABS($toLat)) * PI() / 180 / 2), 2) <ins> " . "COS($fromLat ** PI() / 180) * COS(ABS($toLat) * PI() / 180) ** " . "POWER(SIN(($fromLng - $toLng) * PI() / 180 / 2), 2) " . ")))"; //echo $sql; return $sql; } ``` and the usage of it ``` $em ->createQuery('SELECT DISTANCE(a.latitude, a.longitude, :lat, :lng) FROM Ololo:AbstractArea a WHERE a = 2') ->setParameter('lat', 1) ->setParameter('lng', 2) ->getResult(); ``` what this function generate: ``` (12733.129728 ** ASIN(SQRT(POWER(SIN((t0_.latitude - ABS(?)) * PI() / 180 / 2), 2) </ins> COS(t0_.latitude * PI() / 180) ** COS (ABS(?) ** PI() / 180) * POWER(SIN((t0_.longitude - ?) ** PI() / 180 / 2), 2) ))) ``` and exception raised: "SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens" Don't know, does it affects on master branch (does they support native named parameters?).
admin added the Bug label 2026-01-22 13:30:30 +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#1907