I'm going through the tutorials for doctrine and liking it so far, however I'm stuck at the point of dropping/regenerating the db schema.
This is the code I am using (pretty much straight from the tutorial)
开发者_JAVA百科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'
Can anyone tell me the correct syntax to use for the DSN, the examples given are a little confusing.
I'm running on localhost through XAMPP.
Any advice appreciated.
Thanks.
This dns will work, not to mention that it might be more readable also. This is the "Lazy connection" method.
// 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');
If you want to use the PDO handler beneath, then call it as:
$pdo_handler = $conn->getPdh();
The doctrine manual says, e.g.,:
phptype://username:password@hostspec/database
In your case:
mysql://user:pass@127.0.0.1/test
You used the PDO variant, which isn't incorrect but not needed.
Doctrine 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
}
Сorrect me if I made a mistake somewhere
精彩评论