Skip to content

Instantly share code, notes, and snippets.

@mwhite
Created January 6, 2011 04:16
Show Gist options
  • Save mwhite/767490 to your computer and use it in GitHub Desktop.
Save mwhite/767490 to your computer and use it in GitHub Desktop.
A Propel behavior that allows runtime specification of database name.
<?php
/**
* If you use different databases for development and production, it can be tedious to maintain
* propel configuration and database schema files in version control, because you need to change the
* database name and re-run propel-gen after pulling so the propel base objects refer to the correct
* database.
*
* This behavior allows you to specify a constant that will be defined when propel is loaded and
* which will be used as the database name in the generated Peer and Query classes instead of a
* hardcoded string. Thus, you can use the URL, file location, or something else to determine which
* database to use.
*/
class CustomDbBehavior extends Behavior {
// default parameter values
protected $parameters = array(
'original_name' => 'mydb', // the actual name of the database in schema.xml
'db_name_constant' => 'DATABASE_NAME'
);
/**
* Replaces all instances of the original database name with the db name constant.
*/
public function queryFilter(&$script) {
$find = '\'' . $this->getParameter('original_name') . '\'';
$replace = $this->getParameter('db_name_constant');
$script = str_replace($find, $replace, $script);
}
/**
* Replaces all instances of the original database name with the db name constant.
*/
public function peerFilter(&$script) {
$find = '\'' . $this->getParameter('original_name') . '\'';
$replace = $this->getParameter('db_name_constant');
$script = str_replace($find, $replace, $script);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment