我的代码:
bootstrap.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()
时,我得到页面freez 3-4秒然后Error 103 (net::ERR_CONNECTION_ABORTED): Unknown error
我在这里做了一些调试。我设置了mysql日志记录,这是我在执行上面的代码后在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 shell中执行此查询 - 它们工作正常。
我发现问题出在\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 $