delight-im/PHP-DB

Feature request, fetching as a class

Stonebubble opened this issue · 2 comments

It would be pretty helpful if the PDO Object is accessible or protected (instead of private) and/or your Class wouldn't overwrite PDO_DEFAULT_FETCH_MODE or expose the $stmt from the select functions, because I want the data to be fetched in specific classes as a basic MVC Model.
Thanks,
Jakob

ocram commented

Thank you!

If we added variations of the four existing Database#select... methods that do not return associative arrays but instead objects or classes, would you consider that sufficient?

To be precise, what about the following modes as separate variants? Which one would you find most useful?

  • \PDO::FETCH_OBJ (for stdClass instances)
  • \PDO::FETCH_CLASS (supplying your own class name)
  • \PDO::FETCH_CLASS | \PDO::FETCH_CLASSTYPE (having the class names as the first column in your database results)
  • \PDO::FETCH_INTO (overwriting a single existing object)

While that is not exactly one of the ways you suggested, it does seem to solve the issue, doesn’t it?

Hey Orcram,

thank you for the fast response.
Yes, this would solve the problem in my case, but in my opinion selectClass(..), selectInto(...), selectObj(..)..., is a bit cluttered. Currently, I only use \PDO::Fetch_CLASS I would rather suggest a method to set the fetch mode as you do it in PDO, give access to the PDO like others requested or more ideal setting selectInternal() to protected, which would make it possible for others to extend your class.

Yes, I also learned in software engineering to set everything to private which isn't used by other classes but right now, sadly no one can add their own functionality without modifying your code locally (which would be much worse)