私のコード:
ブートストラップ.php
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
$paths = array(dirname(__FILE__)."/entities");
require_once __DIR__ . '/entities/Users.php';
$isDevMode = false;
// the connection configuration
$dbParams = array(
'driver' => 'pdo_mysql',
'user' => 'root',
'password' => 'pass',
'dbname' => 'p1db',
);
$config = Setup::createConfiguration($isDevMode);
$driver = new AnnotationDriver(new AnnotationReader(), $paths);
// registering noop annotation autoloader - allow all annotations by default
AnnotationRegistry::registerLoader('class_exists');
$config->setMetadataDriverImpl($driver);
$config->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());
$em = EntityManager::create($dbParams, $config);
test.php
<?php
include 'bootstrap.php';
....
try {
$user = new Users();
$user->setName($_POST['name']);
$user->setEmail($_POST['email']);
$user->setPassword(sha1($_POST['password'] . $_SALT_));
$user->setPhone($_POST['phone']);
$em->persist($user);
$em->flush();
} catch (Exception $e) {
$em->getConnection()->rollback();
$em->close();
throw $e;
}
なぜ機能しないのか理解できません。実行すると$em->flush()
、3〜4秒間ページがフリーズした後、Error 103 (net::ERR_CONNECTION_ABORTED): Unknown error
ここでデバッグを行いました。上記のコードを実行した後、MySQL ログに記録されているものを以下に示します。
13 Query START TRANSACTION
13 Query INSERT INTO users (email, password, name, added_date, phone) VALUES ('foo@example.com', 'ec4cea0c00610c486f52e62e2850fa7e8b95fb04', 'test name', NULL, '5555555')
このクエリを MySQL シェルで実行すると、問題なく動作します。私は、問題が\Doctrine\ORM\UnitOfWork->commit()
、ライン上にあることを発見しました$conn->beginTransaction();
。beginTransaction()
はここで定義されています\Doctrine\DBAL\Driver\Connection->beginTransaction()
が、これは単なるインターフェースです...ここで何が起こっているのかわかりません。
MySQL v 5.1.48、pdo_mysql
PDO Driver for MySQL enabled
Client API version mysqlnd 5.0.8-dev - 20102224 - $Revision: 308673 $