zend-db icon indicating copy to clipboard operation
zend-db copied to clipboard

Broader PDO fetch-style range

Open chris-kruining opened this issue 7 years ago • 10 comments

I encountered a situation Where I want to fetch multiple columns with the same name where aliases are not an option, so I set the fetchmode to \PDO::FETCH_NAMED which resulted in the Exception\InvalidArgumentException "The fetch mode must be one of the PDO::FETCH_* constants.", which \PDO::FETCH_NAMED is.

I've attempted to fix this by editing the condition so that all the fetch styles listed in http://php.net/manual/en/pdostatement.fetch.php

I hope that this PR is to the required standards :D

chris-kruining avatar Dec 22 '17 14:12 chris-kruining

@chris-kruining thanks for this PR, I'm sorry to review it only now. The goal of your PR is fine (@Ocramius as library, we need to support all the PDO fetch modes), but I think it's better to check for valid PDO::FETCH_* constants using an array of allowed modes. Something like this:

use PDO;

class Result implements Iterator, ResultInterface
{
  // ...
  private $allowedFetchModes = [
    PDO::FETCH_ASSOC,
    PDO::FETCH_BOTH,
    PDO::FETCH_BOUND,
    PDO::FETCH_CLASS,
    PDO::FETCH_CLASSTYPE,
    PDO::FETCH_PROPS_LATE,
    PDO::FETCH_INTO,
    PDO::FETCH_LAZY,
    PDO::FETCH_NAMED,
    PDO::FETCH_NUM,
    PDO::FETCH_OBJ
  ];

  /**
   * @param int $fetchMode
   * @throws Exception\InvalidArgumentException on invalid fetch mode
   */
  public function setFetchMode($fetchMode)
  {
      if (! in_array($fetchMode, $this->allowedFetchModes)) {
          throw new Exception\InvalidArgumentException(
              'The fetch mode must be one of the PDO::FETCH_* constants.'
           );
      }
      $this->fetchMode = (int) $fetchMode;
  }
}

This change is required also for fix an issue on the previous implementation. In fact, checking for $fetchMode < 1 || $fetchMode > 10 is not enough. For instance 7 is not a valid PDO::FETCH_* mode.

@chris-kruining can you change this PR according to my suggestion? Thanks a ton!

ezimuel avatar Aug 07 '18 14:08 ezimuel

@ezimuel I've made the changed like you proposed, however I do have some concerns:

In my opinion is not a completely valid set as \PDO::FETCH_PROPS_LATE and \PDO::FETCH_CLASSTYPE should only be used in combination with \PDO::FETCH_CLASS

chris-kruining avatar Aug 09 '18 07:08 chris-kruining

@ezimuel ping

chris-kruining avatar Feb 28 '19 10:02 chris-kruining

Would be great if this could be resolved guys, I think 1.5 years is a tad bit long :P

chris-kruining avatar May 27 '19 13:05 chris-kruining

@webimpress the issues you have mentioned should have been resolves afaik

chris-kruining avatar Jul 03 '19 10:07 chris-kruining

@chris-kruining Thanks, but now somehow I see conflicts on this PR. Can you resolve them, please?

michalbundyra avatar Jul 04 '19 07:07 michalbundyra

@webimpress fixed

chris-kruining avatar Jul 04 '19 16:07 chris-kruining

ping

chris-kruining avatar Sep 09 '19 08:09 chris-kruining

This repository has been moved to laminas/laminas-db. If you feel that this patch is still relevant, please re-open against that repository, and reference this issue. To re-open, we suggest the following workflow:

  • Squash all commits in your branch (git rebase -i origin/{branch})
  • Make a note of all changed files (`git diff --name-only origin/{branch}...HEAD
  • Run the laminas/laminas-migration tool on the code.
  • Clone laminas/laminas-db to another directory.
  • Copy the files from the second bullet point to the clone of laminas/laminas-db.
  • In your clone of laminas/laminas-db, commit the files, push to your fork, and open the new PR. We will be providing tooling via laminas/laminas-migration soon to help automate the process.

weierophinney avatar Dec 31 '19 22:12 weierophinney

This repository has been closed and moved to laminas/laminas-db; a new issue has been opened at https://github.com/laminas/laminas-db/issues/59.

michalbundyra avatar Jan 16 '20 19:01 michalbundyra