PHP Doctrine ORM新手 - 有效的DSN用于删除数据库?

时间:2009-11-05 16:23:41

标签: php orm doctrine

到目前为止,我正在浏览教程和喜欢它的教程,但是我仍然停留在删除/重新生成数据库模式的位置。

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

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上运行。

任何建议表示赞赏。

感谢。

3 个答案:

答案 0 :(得分:1)

这个dns会起作用,更不用说它也可能更具可读性。 这是“Lazy connection”方法。

// 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();

答案 1 :(得分:0)

doctrine manual说,例如:

phptype://username:password@hostspec/database

在你的情况下:

mysql://user:pass@127.0.0.1/test

您使用的PDO变体不是不正确但不需要。

答案 2 :(得分: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
}
如果我在某个地方犯了错误,请告诉我

相关问题