Cakephp 5 PDO instance

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

use Cake\Datasource\ConnectionManager;

then

    public static function dbh()
    {

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

And do my custom binding like:

    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:

$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()

See the migration guide.

I wrote a small custom class.

It works but the chapter Error & Exception Handling - 5.x

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

    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.

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…

@Zuluru thanks, I ended up putting:

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

That error should never be hit anyway.

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.

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