Snippets

Create an account or login to be able to add, comment and rate snippets.

Navigation

Use the model in a batch or a test

To use database connections defined in the databases.yml configuration file, you can use the following snippet:

// initialize database manager
$databaseManager = new sfDatabaseManager();
$databaseManager->initialize();
by Fabien Potencier on 2006-05-21, tagged batch  configuration  propel  test 

Comments on this snippet

gravatar icon
#1 Dave Brondsema on 2007-07-03 at 11:38

There is some more initialization requirements for this to work. The simplest way I have found is to make it a "functional" test since we are relying on Propel functionality. You can still do lime unit tests. Here's one of my test files:

$app = 'admin';
include(dirname(__FILE__).'/../bootstrap/functional.php');
 
// to get configurations set up, even though we don't use the browser
$b = new sfTestBrowser();
$b->initialize(); 
 
$t = $b->test();
$t->plan = 4;
 
$databaseManager = new sfDatabaseManager();
$databaseManager->initialize();
 
try {
    $ra = RoomAssignmentPeer::getCurrentActiveFor(12345);
    $t->fail('incomplete id, should throw exception');
} catch (rciException $ex) {
    $t->pass('incomplete id');
}
// ...
// 3 more tests
// ...
gravatar icon
#2 Nathan Vonnahme on 2007-08-29 at 12:15

To get my batch file connected to the database using a raw SQL query, I had to just

generate the skeleton:

<pre> $ symfony batch default batchFileName frontend prod </pre>

Add the lines from this snippet, and get the connection in the right way: <pre class="php"> $con = $databaseManager->getDatabase('propel')->getConnection(); </pre>

You need to create an account or log in to post a comment or rate this snippet.