お世話になります。
下記の要領でトランザクション処理を行いたいのですが、
PDO::beginTransactionが効かずにどちらかが書き込みに失敗していてもどちらかの書き込みが実行されてしまいます。
(getAttributeで確認。※正直この方法できちんと確かめられているのかもわかりません。)
データベースエンジンはInnoDBになっていました。
ソースは下記のサイトを参考に作りました。
http://www.objective-php.net/mvc/model_ex
http://www.objective-php.net/mvc/data
理由のわかるかた、ご教示ください。
PHP 7.3.1
Mysql 8.0.15
php
1// ctrl.php 2 require("../〇〇/ModelBase.php"); 3 require("../●●/ChildrenDao.php"); 4 require("../〇〇/ChildrenDto.php"); 5 6 class ChildrenModel extends ModelBase 7 { 8 public function addChildren($brother_data, $sister_data) 9 { 10 $nowattr = $this->db->getAttribute(PDO::ATTR_AUTOCOMMIT); 11 echo $nowattr."<br>"; // 1 12 13 $this->db->beginTransaction(); 14 15 $nowattr = $this->db->getAttribute(PDO::ATTR_AUTOCOMMIT); 16 echo $nowattr."<br>"; // 1 17 18 $tbl_brother = new TableBrotherDto(); 19 $tbl_sister = new TableSisterDto(); 20 21 $tbl_brother->SetData($brother_data); 22 $tbl_sister->SetData($sister_data); 23 24 try 25 { 26 $brother = new TableBrotherDao(); 27 $sister = new TableSisterDao(); 28 29 $users->table_insert($tbl_brother); 30 $login->table_insert($tbl_sister); 31 32 $this->db->commit(); 33 return true; 34 35 } catch (Exception $e) { 36 $this->db->rollBack(); 37 throw $e; 38 } 39 } 40 } 41
php
1// TableChildrenDao.php 2 class TableBrotherDao extends ModelBase 3 { 4 protected $name = 'table_brother'; 5 6 ~略~ 7 public function table_insert($data){ 8 $res = $this->insert($data); 9 return $res; 10 } 11 }
php
1//ModelBase.php 2class ModelBase 3{ 4 private static $connInfo; 5 protected $db; 6 protected $name; 7 8 ~略~ 9 10 public function initDb() 11 { 12 $dsn = sprintf( 13 'mysql:host=%s;dbname=%s;port=3306;', 14 self::$connInfo['host'], 15 self::$connInfo['dbname'] 16 ); 17 $this->db = new PDO($dsn, self::$connInfo['dbuser'], self::$connInfo['password']); 18 $this->db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); 19 $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 20 } 21 22 ~関数略~ 23 24 // INSERTを実行 25 public function insert($data) 26 { 27 ~略~ 28 29 $res = $stmt->execute(); 30 $return $res; 31 } 32 33}
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/02/19 03:11
2019/02/19 03:14
2019/02/19 03:18
2019/02/19 03:20
2019/02/19 03:38
2019/02/19 03:41
2019/02/19 03:56