You are creating a new PDO object every time you call $this->connect()
, so if you have:
$stmt1 = $this->connect()->prepare(" ... ");
$stmt2 = $this->connect()->prepare(" ... ");
$stmt1
and $stmt2
will actually be completely different PDO objects, so if you start a transaction with one object, it will NOT apply to the other one. Instead you should save a PDO object and reference that instead of creating a new one every time.
Most of the time, I find it easier just to pass this to the class's constructor, however if you want to do minimal editing, you could just do:
class YourClass {
private $dbh;
private function connect() {
if (!isset($this->dbh)) {
$this->dbh = new PDO('mysql:host=' . $this->host . ';dbname=' . $this->database, $this->username, $this->password);
}
return $this->dbh;
}
}
However you might want to change the name connect()
to something a bit more logical, like getDbh()
.
If instead you wanted to put it in the object's constructor, you could do this:
class YourClass {
private $dbh;
public function __construct(PDO $dbh) {
$this->dbh = $dbh;
}
}
$dbh = new PDO('mysql:host=' . $host . ';dbname=' . $database, $username, $password);
$yourclass = new YourClass($dbh);
Then in any of the other class methods, you would just reference $this->dbh
. Using your code as an example:
public function runExQuery($sql) {
$preparedQuery = $this->dbh->prepare($sql);
$this->dbh->beginTransaction();
$preparedQuery->execute();
$this->dbh->commit();
}
Personally, this is the way I would do it.