ProgZi
ProgZi

Reputation: 1267

How to set Attribute to PDO connection in Codeigniter

How to set attributes (PDO::ATTR_ERRMODE) on the PDO database handle in Codeigniter?

Upvotes: 3

Views: 1912

Answers (2)

Antony
Antony

Reputation: 4364

I think a better option is to use a MY_Model (which you then extend and this is available then across the application) and define something like this in the construct:

$this->db->conn_id->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);

Note conn_id allows you to access the main PDO object.

Upvotes: 5

ProgZi
ProgZi

Reputation: 1267

There are two ways:
1. The lazy (hacky) way
Add to the following code into system/core/database/drivers/pdo/pdo_driver.php (in CI 3):

public function db_connect($persistent = FALSE)
{

    $this->options[PDO::ATTR_PERSISTENT] = $persistent;

    // Added code start
    $this->options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
    // Added code end
    try
    {
        return new PDO($this->dsn, $this->username, $this->password, $this->options);
    ...
}

2. The right way
Extend Database Driver and add the same line

Note: If you will set PDO::ERRMODE_EXCEPTION in Codeigniter it will show exception errors even in Production environment.

Upvotes: 0

Related Questions