After reading the previous sections of this chapter, you should now know how to create a connection. So, lets modify our bootstrap file to include the initialization of a connection. For this example we will just be using a sqlite memory database but you can use whatever type of database connection you prefer.
Add your database connection to bootstrap.php and it should look something like the following:
/**
* Bootstrap Doctrine.php, register autoloader and specify
* configuration attributes
*/
require_once('../doctrine/branches/1.2/lib/Doctrine.php');
spl_autoload_register(array('Doctrine', 'autoload'));
$manager = Doctrine_Manager::getInstance();
$conn = Doctrine_Manager::connection('sqlite::memory:', 'doctrine');
To test the connection lets modify our test.php script and perform a small test. Since we create a variable name $conn, that variable is available to the test script so lets setup a small test to make sure our connection is working:
First lets create a test table and insert a record:
// test.php
// ...
$conn->export->createTable('test', array('name' => array('type' => 'string')));
$conn->execute('INSERT INTO test (name) VALUES (?)', array('jwage'));
Now lets execute a simple SELECT query from the test table we just created to make sure the data was inserted and that we can retrieve it:
// test.php
// ...
$stmt = $conn->prepare('SELECT * FROM test');
$stmt->execute();
$results = $stmt->fetchAll();
print_r($results);
Execute test.php from your terminal and you should see:
$ php test.php
Array
(
[0] => Array
(
[name] => jwage
[0] => jwage
)
)