Doctrine DBAL
  1. Doctrine DBAL
  2. DBAL-196

The function fetchAll(PDO::FETCH_ALL, 'MyClass') doesn't create object of MyClass

    Details

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

      Description

      The code below doesn't create objects of MyClass when it has to.
      Instead it create StdClass objects

      $stmt = $db->prepare($sql);
      $stmt->execute();
      
      $results = $stmt->fetchAll(
          \PDO::FETCH_CLASS,
          'MyClass'
      );
      

      The problem comes from the file Doctrine/DBAL/Statement.php

          /**
           * Returns an array containing all of the result set rows.
           * 
           * @param integer $fetchStyle
           * @param integer $columnIndex
           * @return array An array containing all of the remaining rows in the result set.
           */
          public function fetchAll($fetchStyle = PDO::FETCH_BOTH, $columnIndex = 0)
          {
      	if ($columnIndex != 0) {
                  return $this->_stmt->fetchAll($fetchStyle, $columnIndex);
              }
              return $this->_stmt->fetchAll($fetchStyle);
          }
      

      The line

      if ($columnIndex != 0) {
      

      must be replace by

      if ($columnIndex !== 0) {
      

      because the parameter $columnIndex is not always an integer and could be a name of a class : see the example n°4 in php doc http://www.php.net/manual/en/pdostatement.fetchall.php

        Activity

        Antoine Froger created issue -
        Antoine Froger made changes -
        Field Original Value New Value
        Description The code below doesn't create objects when it has to.

        {noformat}
        $stmt = $db->prepare($sql);
        $stmt->execute();

        $results = $stmt->fetchAll(
            \PDO::FETCH_CLASS,
            'MyClass'
        );
        {noformat}

        The problem comes from the file Doctrine/DBAL/Statement.php

        {noformat}
            /**
             * Returns an array containing all of the result set rows.
             *
             * @param integer $fetchStyle
             * @param integer $columnIndex
             * @return array An array containing all of the remaining rows in the result set.
             */
            public function fetchAll($fetchStyle = PDO::FETCH_BOTH, $columnIndex = 0)
            {
        if ($columnIndex != 0) {
                    return $this->_stmt->fetchAll($fetchStyle, $columnIndex);
                }
                return $this->_stmt->fetchAll($fetchStyle);
            }
        {noformat}


        The line
        {noformat}
        if ($columnIndex != 0) {
        {noformat}
        must be replace by
        {noformat}
        if ($columnIndex !== 0) {
        {noformat}
        because the parameter $columnIndex is not always an integer and could be a name of a class : see the example n°4 in php doc http://www.php.net/manual/en/pdostatement.fetchall.php
        The code below doesn't create objects of MyClass when it has to.
        Instead it create StdClass objects

        {noformat}
        $stmt = $db->prepare($sql);
        $stmt->execute();

        $results = $stmt->fetchAll(
            \PDO::FETCH_CLASS,
            'MyClass'
        );
        {noformat}

        The problem comes from the file Doctrine/DBAL/Statement.php

        {noformat}
            /**
             * Returns an array containing all of the result set rows.
             *
             * @param integer $fetchStyle
             * @param integer $columnIndex
             * @return array An array containing all of the remaining rows in the result set.
             */
            public function fetchAll($fetchStyle = PDO::FETCH_BOTH, $columnIndex = 0)
            {
        if ($columnIndex != 0) {
                    return $this->_stmt->fetchAll($fetchStyle, $columnIndex);
                }
                return $this->_stmt->fetchAll($fetchStyle);
            }
        {noformat}


        The line
        {noformat}
        if ($columnIndex != 0) {
        {noformat}
        must be replace by
        {noformat}
        if ($columnIndex !== 0) {
        {noformat}
        because the parameter $columnIndex is not always an integer and could be a name of a class : see the example n°4 in php doc http://www.php.net/manual/en/pdostatement.fetchall.php
        Fabio B. Silva made changes -
        Assignee Benjamin Eberlei [ beberlei ] Fabio B. Silva [ fabio.bat.silva ]
        Fabio B. Silva made changes -
        Status Open [ 1 ] In Progress [ 3 ]
        Hide
        Benjamin Eberlei added a comment -

        Fixed and merged into 2.2

        Show
        Benjamin Eberlei added a comment - Fixed and merged into 2.2
        Benjamin Eberlei made changes -
        Status In Progress [ 3 ] Resolved [ 5 ]
        Fix Version/s 2.2 [ 10142 ]
        Fix Version/s 2.1 [ 10068 ]
        Resolution Fixed [ 1 ]
        Benjamin Eberlei made changes -
        Workflow jira [ 13338 ] jira-feedback2 [ 17776 ]
        Benjamin Eberlei made changes -
        Workflow jira-feedback2 [ 17776 ] jira-feedback3 [ 20131 ]

        This list may be incomplete, as errors occurred whilst retrieving source from linked applications:

        • Request to http://www.doctrine-project.org/fisheye/ failed: Error in remote call to 'FishEye 0 (http://www.doctrine-project.org/fisheye/)' (http://www.doctrine-project.org/fisheye) [AbstractRestCommand{path='/rest-service-fe/search-v1/crossRepositoryQuery', params={query=DBAL-196, expand=changesets[0:20].revisions[0:29],reviews}, methodType=GET}] : Received status code 503 (Service Temporarily Unavailable)

          People

          • Assignee:
            Fabio B. Silva
            Reporter:
            Antoine Froger
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: