Php pdo::fetch_*

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • guillermobytes
    New Member
    • Jan 2010
    • 77

    #1

    Php pdo::fetch_*

    Hi,

    I would like to set the fetch mode for all the pdoStatements how can i do that.
    For the moment i have only found a method that will set the fetch mode for the pdoStatement instance the method is applied to.

    ex:
    Code:
    $connection = new PDO(...);
    $sql = "SELECT * FROM table";
    $statement = $connection->prepare($sql);
    $statement->setFetchMode(PDO::FETCH_ASSOC);
    $statement->execute();
    $rows = $statement->fetchAll();
    this works fine for that instance of pdoStatement. but i would like it to be permanent.

    so is there any static pdoStatement::s etFetchMode(); kind of method
    or PDO instance method : $pdo->setFetchMode() ;

    thanks
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    Originally posted by guillermobytes
    this works fine for that instance of pdoStatement. but i would like it to be permanent.

    so is there any static pdoStatement::s etFetchMode(); kind of method
    or PDO instance method : $pdo->setFetchMode() ;
    there is no static setFetchMode() method (why should there be any, every statement object could have a different fetch mode). from the OOP point-of-view, it does not make sense to have a static fetch mode.

    the PDO class does only deal with DB connection, data retrieval is done by PDOStatement, but you can write your own implementation (e.g. a wrapper or an extended class)

    Comment

    • guillermobytes
      New Member
      • Jan 2010
      • 77

      #3
      lol Dormilich, in almost every answer I get from you there is this sentence :
      from the OOP point-of-view, it does not make sense
      i may have completely missed the OOP concepts or maybe you are not enough open minded. probably it's the first option.. but consider this, and then tell me if I missed something:
      there is by default a fetch mode wich is FETCH_BOTH (I think)(it's FETCH_ASSOC and FETCH_NUM).
      the point is that the default fetch mode does not satisfy my needs, because i use 99% of the time the FETCH_ASSOC, there for i would like to change the default value to fit my needs.
      If i have to change the fetch mode to FETCH_ASSOC every time i create a pdoStatement (with a wrapper) then it makes n changes (given n is the number of pdoStatement instances created).
      However if i change it once from the bootstraper and then ocasionally when i need the other fetch mode (FETCH_NUM) i change it from the pdoStatement instance, i would only get 1n/100 which is a good improvement.

      so whenever there is a default value that gets replicated over every instance, with the right to change the instance value, i think it would make sense to make that default property static, to allow the programmer to change that property if desired like this :
      Code:
      class pdoStatement
      {
          private static $_defaultFetchMode = pdo::FETCH_BOTH;
      
          private $_fetchMode;
      
          public function __construct()
          {
               //set the instance fetchMode from default fetch mode
               $this->setFetchMode(self::$_defaultFetchMode);
          }
      
          public function setFetchMode($fetchMode)
          {
              //ensure fetch mode is supported
              self::isSupportedFetchMode($fetchMode);
              //fetch mode supported
              $this->_fetchMode = $fetchMode;  
          }
      
          static public function setDefaultFetchMode($fetchMode)
          {
              //ensure fetch mode is supported
              self::isSupportedFetchMode($fetchMode);
              //fetch mode supported
              self::$_defaultFetchMode = $fetchMode;  
          }
      
          static public function isSupportedFetchMode($fetchMode)
          {
              switch ($fetchMode) {
                  case FETCH_BOTH;
                  break;
                  case FETCH_ASSOC;
                  break;
                  case FETCH_NUM;
                  break;
                  case etc.
                  break
                  default;
                      throw new pdoStatement_Exception('Error : fetch mode not supported');
                  break;
              }
          }
      }
      it saves some resources and time to write a wrapper.


      we could even write the second option i gave, to set the fetchMode from the pdo class. That seems to make sense too because the pdo class creates pdoStatments, so it could call pdoStatement::s etFetchMode() before returning the pdoStatement. (i wont code it, you get what i mean)

      anyway I appreciate your answer and if you see any mistake in my point, i would be pleased if you made some remarks.

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        This is a feature that could obviously be useful. I'll write a patch and submit it to the PDO guys and see what they say.

        Comment

        • guillermobytes
          New Member
          • Jan 2010
          • 77

          #5
          Markus that'd be great! keep me up to date, if it is aproved/rejected.

          Comment

          Working...