Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-1040

Using the same named parameter at various places within a DQL query will result in a PHP fatal error

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 2.0.4
    • Component/s: None
    • Security Level: All
    • Labels:
      None

      Description

      There are perfectly legitimate situations where the same value needs to be used at various places within a single query (e. g. to compare various versioned artifacts against a common revision number, while joining them). However, specifying the same parameter name twice within a DQL query currently results in a fatal error:

      Warning: array_combine(): Both parameters should have an equal number of elements in /var/www/invoiceCreator/src/thirdPartyComponents/doctrine/Doctrine/ORM/Query.php on line 252 Fatal error: Unsupported operand types in /var/www/invoiceCreator/src/thirdPartyComponents/doctrine/Doctrine/ORM/Query.php on line 252

      It is possible to work around this by just giving each parameter a unique name. Unfortunately, it becomes an impossibility when using generated DQL statements, e.g. when a correlated subquery is replicated multiple times within the same DQL query.

      I can not think of a convincing reason to allow each parameter to appear only once in a query, as the number of parameters could be determined as well by counting the number of unique names rather than simply all names.

      Would it be possible to relax the restriction? It would remove a massive burden on dynamically building queries, as currently all parameter names have to be adapted to every single situation, and that would be no longer an issue, if multiple parameters with the same name would be allowed and correctly handled by Doctrine 2.

      Apart from the functional requirement, I don't like the crashing effect. If such use is forbidden, it should be officially so (with the documentation at least mentioning the restriction and Doctrine 2 giving an understandable error message when used wrongly, instead of crashing).

      1. ddc1040.diff
        1 kB
        Benjamin Eberlei

        Activity

        Hide
        Daniel Alvarez Arribas added a comment -

        Currently, I automatically post-process all queries so that all parameters are assigned new (unique) names. This way, even the multiple replicated subqueries within the same DQL query will end up having unique parameter names. While this works, it is clumsy as a general approach.

        Show
        Daniel Alvarez Arribas added a comment - Currently, I automatically post-process all queries so that all parameters are assigned new (unique) names. This way, even the multiple replicated subqueries within the same DQL query will end up having unique parameter names. While this works, it is clumsy as a general approach.
        Hide
        Benjamin Eberlei added a comment -

        Works for me, see patch attached.

        The error occuring to you is not related to named parameters but to objects as parameters. Can you give a reproducable test-case to fix this?

        Show
        Benjamin Eberlei added a comment - Works for me, see patch attached. The error occuring to you is not related to named parameters but to objects as parameters. Can you give a reproducable test-case to fix this?
        Hide
        Daniel Alvarez Arribas added a comment -

        OK, I will see what I can do. I will prepare an example of a working scenario and a non-working scenario, and let you know.

        Show
        Daniel Alvarez Arribas added a comment - OK, I will see what I can do. I will prepare an example of a working scenario and a non-working scenario, and let you know.
        Hide
        Daniel Alvarez Arribas added a comment -

        I tried to reduce the non-working query as much as possible to get a schematic idea of what provokes the error.

        It seems that queries like this one will break:

        {{$doctrineQuery = $doctrineEntityManager->createQuery('SELECT a
        FROM \persistentData\model\import\A a,
        WHERE a.a = :anEntityInstance
        AND a.b = :anEntityInstance
        AND a.c = :aString');

        $doctrineQuery->setParameter('anEntityInstance', $anEntityInstance); // Whatever entity instance
        $doctrineQuery->setParameter('aString', $aString); // Some string-valued variable

        $doctrineQuery->getResult();}}

        Note that the first argument is an entity instance, and the second one a string. Both are non-null.

        This gives:

        Warning: array_combine(): Both parameters should have an equal number of elements in /var/www/invoiceCreator/src/thirdPartyComponents/doctrine/Doctrine/ORM/Query.php on line 252 Fatal error: Unsupported operand types in /var/www/invoiceCreator/src/thirdPartyComponents/doctrine/Doctrine/ORM/Query.php on line 252

        This is a basic form of a working scenario (all parameters have unique names):

        {{$doctrineQuery = $doctrineEntityManager->createQuery('SELECT a
        FROM \persistentData\model\import\A a,
        WHERE a.a = :anEntityInstance1
        AND a.b = :anEntityInstance2
        AND a.c = :aString');

        $doctrineQuery->setParameter('anEntityInstance1', $anEntityInstance); // Whatever entity instance
        $doctrineQuery->setParameter('anEntityInstance2', $anEntityInstance); // Whatever entity instance
        $doctrineQuery->setParameter('aString', $aString); // Some string-valued variable

        $doctrineQuery->getResult();}}

        Show
        Daniel Alvarez Arribas added a comment - I tried to reduce the non-working query as much as possible to get a schematic idea of what provokes the error. It seems that queries like this one will break: {{$doctrineQuery = $doctrineEntityManager->createQuery('SELECT a FROM \persistentData\model\import\A a, WHERE a.a = :anEntityInstance AND a.b = :anEntityInstance AND a.c = :aString'); $doctrineQuery->setParameter('anEntityInstance', $anEntityInstance); // Whatever entity instance $doctrineQuery->setParameter('aString', $aString); // Some string-valued variable $doctrineQuery->getResult();}} Note that the first argument is an entity instance, and the second one a string. Both are non-null. This gives: Warning: array_combine(): Both parameters should have an equal number of elements in /var/www/invoiceCreator/src/thirdPartyComponents/doctrine/Doctrine/ORM/Query.php on line 252 Fatal error: Unsupported operand types in /var/www/invoiceCreator/src/thirdPartyComponents/doctrine/Doctrine/ORM/Query.php on line 252 This is a basic form of a working scenario (all parameters have unique names): {{$doctrineQuery = $doctrineEntityManager->createQuery('SELECT a FROM \persistentData\model\import\A a, WHERE a.a = :anEntityInstance1 AND a.b = :anEntityInstance2 AND a.c = :aString'); $doctrineQuery->setParameter('anEntityInstance1', $anEntityInstance); // Whatever entity instance $doctrineQuery->setParameter('anEntityInstance2', $anEntityInstance); // Whatever entity instance $doctrineQuery->setParameter('aString', $aString); // Some string-valued variable $doctrineQuery->getResult();}}
        Hide
        Benjamin Eberlei added a comment -

        Fixed.

        Show
        Benjamin Eberlei added a comment - Fixed.
        Hide
        Daniel Alvarez Arribas added a comment -

        Great, thanks.

        Show
        Daniel Alvarez Arribas added a comment - Great, thanks.

          People

          • Assignee:
            Benjamin Eberlei
            Reporter:
            Daniel Alvarez Arribas
          • Votes:
            1 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: