Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Broader PDO fetch-style range #59

Closed
michalbundyra opened this issue Jan 16, 2020 · 9 comments
Closed

Broader PDO fetch-style range #59

michalbundyra opened this issue Jan 16, 2020 · 9 comments

Comments

@michalbundyra
Copy link
Member

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


Originally posted by @chris-kruining at zendframework/zend-db#296

@michalbundyra
Copy link
Member Author

@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!


Originally posted by @ezimuel at zendframework/zend-db#296 (comment)

@michalbundyra
Copy link
Member Author

@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


Originally posted by @chris-kruining at zendframework/zend-db#296 (comment)

@michalbundyra
Copy link
Member Author

@ezimuel ping


Originally posted by @chris-kruining at zendframework/zend-db#296 (comment)

@michalbundyra
Copy link
Member Author

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


Originally posted by @chris-kruining at zendframework/zend-db#296 (comment)

@michalbundyra
Copy link
Member Author

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


Originally posted by @chris-kruining at zendframework/zend-db#296 (comment)

@michalbundyra
Copy link
Member Author

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


Originally posted by @michalbundyra at zendframework/zend-db#296 (comment)

@michalbundyra
Copy link
Member Author

@webimpress fixed


Originally posted by @chris-kruining at zendframework/zend-db#296 (comment)

@michalbundyra
Copy link
Member Author

ping


Originally posted by @chris-kruining at zendframework/zend-db#296 (comment)

@michalbundyra
Copy link
Member Author

Resolved in #33 and released in 2.11.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant