Created
January 6, 2011 04:16
-
-
Save mwhite/767490 to your computer and use it in GitHub Desktop.
A Propel behavior that allows runtime specification of database name.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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