Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Get PDO instance or PDO DSN

Hi, as mentioned above, how can i get the PDO instance or the DSN it self? thanks.



6.3k
Accepted
answer
edited Apr '14

From inside a controller:

//resolve a singleton service named 'db' from our dependency injector
$db_service = $this->getShared('db');
//resolve the handler of the DB service (PDO in this case)
$pdo = $db->getInternalHandler();


$sql = $pdo->prepare('select ......');
//so on with regular pdo syntax
....

Any reason you need to resolve PDO in particular? Phalcon provides a pretty good abstraction on top of PDO which is a lot less verbose than regular PDO. Refer to its syntax here http://docs.phalconphp.com/en/latest/api/Phalcon%5Db%5Adapter%5Pdo.html



9.3k
edited Apr '14

Hi, Thanks for the reply.. just trying to implement Oauth2.0 server by bshaffer https://github.com/bshaffer/oauth2-server-php with phalcon and rewrite the code based on its interfaces. here's code from Pdo Storage Constructor

    public function __construct($connection, $config = array())
    {
        if (!$connection instanceof \PDO) {
            if (is_string($connection)) {
                $connection = array('dsn' => $connection);
            }
            if (!is_array($connection)) {
                throw new \InvalidArgumentException('First argument to OAuth2\Storage\Pdo must be an instance of PDO, a DSN string, or a configuration array');
            }
            if (!isset($connection['dsn'])) {
                throw new \InvalidArgumentException('configuration array must contain "dsn"');
            }
            // merge optional parameters
            $connection = array_merge(array(
                'username' => null,
                'password' => null,
            ), $connection);
            $connection = new \PDO($connection['dsn'], $connection['username'], $connection['password']);
        }
        $this->db = $connection;
        // ........
    }