1

到目前为止,我正在阅读学说教程并喜欢它,但是我被困在删除/重新生成数据库模式的点上。

这是我正在使用的代码(几乎直接来自教程)

require_once(dirname(__FILE__) . '/lib/vendor/doctrine/Doctrine.php');
spl_autoload_register(array('Doctrine', 'autoload'));
$manager = Doctrine_Manager::getInstance();


$manager->setAttribute(Doctrine::ATTR_AUTO_ACCESSOR_OVERRIDE, true);
$manager->setAttribute(Doctrine::ATTR_AUTOLOAD_TABLE_CLASSES, true);   
$dsn = 'mysql:dbname=test;host=127.0.0.1';
$user = 'root';
$password = 'test';

$dbh = new PDO($dsn, $user, $password);
$conn = Doctrine_Manager::connection($dbh);

$conn->setOption('username', $user);
$conn->setOption('password', $password);

Doctrine::loadModels('models');



Fatal error: Uncaught exception 'Doctrine_Connection_Exception' with message
'You must create your Doctrine_Connection by using a valid Doctrine style dsn in order 
to use the create/drop database functionality'

谁能告诉我用于 DSN 的正确语法,给出的示例有点令人困惑。

我通过 XAMPP 在 localhost 上运行。

任何建议表示赞赏。

谢谢。

4

3 回答 3

1

这个 dns 可以工作,更不用说它也可能更具可读性。这就是“惰性连接”方法。

// At this point no actual connection to the database is created
$conn = Doctrine_Manager::connection('mysql://username:password@localhost/test');

// The first time the connection is needed, it is instantiated
// This query triggers the connection to be created
$conn->execute('SHOW TABLES');

如果要使用下面的 PDO 处理程序,则将其称为:

$pdo_handler = $conn->getPdh();
于 2009-11-05T21:28:59.393 回答
0

教义手册说,例如:

phptype://username:password@hostspec/database

在你的情况下:

mysql://user:pass@127.0.0.1/test

您使用了 PDO 变体,它不正确但不需要。

于 2009-11-05T21:30:07.847 回答
0

教义2:

$params = array(
    'driver' => 'pdo_mysql',
    'host' => '127.0.0.1',
    'port' => null,
    'dbname' => 'test',
    'user' => 'root',
    'password' => 'test',
);

$conn = DriverManager::getConnection($params);

// Drop
try {
    $conn->getSchemaManager()->dropDatabase($name);
} catch (\Exception $e) {
    // Could not drop database
}

如果我在某处犯了错误,请纠正我

于 2014-02-04T09:27:36.700 回答