# Cakephp 5 PDO instance

**URL:** <https://discourse.cakephp.org/t/cakephp-5-pdo-instance/11618>\
**Category:** Uncategorized\
**Created:** [November 3, 2023, 4:56pm UTC](https://discourse.cakephp.org/t/cakephp-5-pdo-instance/11618 "2023-11-03T16:56:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimgwhit](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/jimgwhit/32/378_2.png) [@jimgwhit](https://discourse.cakephp.org/u/jimgwhit)\
**Post date:** [November 3, 2023, 4:56pm UTC](https://discourse.cakephp.org/t/cakephp-5-pdo-instance/11618/1 "2023-11-03T16:56:24Z")

</div>

In earlier versions I was able to in a custom class:

```auto
use Cake\Datasource\ConnectionManager;

```

then

```auto
    public static function dbh()
    {

        try {
            $dbh = ConnectionManager::get('default');
            return $dbh;
        } catch (PDOException $e) {
            throw new pdoDbException($e);
        }
    }

```

And do my custom binding like:

```auto
    public static function select($sql, $array = array(), $fetchMode = PDO::FETCH_OBJ, $class = '')
    {
        $stmt = self::dbh()->prepare($sql);
        foreach ($array as $key => $value) {
            if (is_int($value)) {
                $stmt->bindValue("$key", $value, PDO::PARAM_INT);
            } else {
                $stmt->bindValue("$key", $value);
            }
        }

        $stmt->execute();

        if ($fetchMode === PDO::FETCH_CLASS) {
            return $stmt->fetchAll($fetchMode, $class);
        } else {
            return $stmt->fetchAll($fetchMode);
        }
    }

```

However now prepare doesn’t work in this line:

```auto
$stmt = self::dbh()->prepare($sql);

```

How do I go about getting the basic PDO instance?

The error is:

> Call to undefined method Cake\Database\Connection::prepare()

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [November 3, 2023, 6:09pm UTC](https://discourse.cakephp.org/t/cakephp-5-pdo-instance/11618/2 "2023-11-03T18:09:18Z")

</div>

See [the migration guide](https://book.cakephp.org/5/en/appendices/5-0-migration-guide.html#connection).

---

<div class="post-metadata">

**Author:** ![jimgwhit](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/jimgwhit/32/378_2.png) [@jimgwhit](https://discourse.cakephp.org/u/jimgwhit)\
**Post date:** [November 3, 2023, 8:22pm UTC](https://discourse.cakephp.org/t/cakephp-5-pdo-instance/11618/3 "2023-11-03T20:22:40Z")

</div>

I wrote a small custom class.

It works but the chapter [Error & Exception Handling - 5.x](https://book.cakephp.org/5/en/development/errors.html)

doesn’t explain how to throw a custom PDO exception:

```auto
    public static function dbh($group = true)
    {
        self::$gp = $group;
        if (self::$db === null) {
            try {
                if (self::$gp === true) {
                    $opt = [
                        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
                    ];
                }
                if (self::$gp === false) {
                    $opt = [
                        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
                        PDO::MYSQL_ATTR_INIT_COMMAND => 'SET sql_mode="STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION"'
                    ];
                }
                require 'DbConnection.php';
                $dsn = DBPDO_TYPE . ':host=' . DBPDO_HOST . ';dbname=' . DBPDO_NAME . ';charset=utf8';
                self::$db = new PDO($dsn, DBPDO_USER, DBPDO_PASS, $opt);
                return self::$db;
            } catch (PDOException $e) {
                WHAT would I put here?????????????
            }
        } else {
            return self::$db;
        }
    }

```

I want a friendly error, not a whole page of errors.

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [November 4, 2023, 2:49pm UTC](https://discourse.cakephp.org/t/cakephp-5-pdo-instance/11618/4 "2023-11-04T14:49:00Z")

</div>

Just let it throw that error? You won’t get a page full of them, you’ll get just one, from the first exception thrown. Unless you have some other non-standard exception handler somewhere intercepting them and turning them into errors and continuing execution, but that seems counter to the general concept of an exception…

---

<div class="post-metadata">

**Author:** ![jimgwhit](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/jimgwhit/32/378_2.png) [@jimgwhit](https://discourse.cakephp.org/u/jimgwhit)\
**Post date:** [November 6, 2023, 5:54am UTC](https://discourse.cakephp.org/t/cakephp-5-pdo-instance/11618/5 "2023-11-06T05:54:58Z")

</div>

@Zuluru thanks, I ended up putting:

```auto
$message = '<strong><bold> Bad connection </bold></strong>';
die($message);

```

That error should never be hit anyway.

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [November 6, 2023, 2:17pm UTC](https://discourse.cakephp.org/t/cakephp-5-pdo-instance/11618/6 "2023-11-06T14:17:07Z")

</div>

Still not clear why you wouldn’t just not bother catching the exception at all, and let Cake’s normal error handling mechanism deal with it.

---

<div class="post-metadata">

**Author:** ![jimgwhit](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/jimgwhit/32/378_2.png) [@jimgwhit](https://discourse.cakephp.org/u/jimgwhit)\
**Post date:** [November 6, 2023, 8:30pm UTC](https://discourse.cakephp.org/t/cakephp-5-pdo-instance/11618/7 "2023-11-06T20:30:55Z")

</div>

@Zuluru I will try that as well.

I couldn’t show solved in original post title, I guess they are locked after so many days.

But: [Solved]
