6
\$\begingroup\$

Here's my bootstrap.php for my PHP/MySQL/Doctrine app. It's my first PHP app so I'm interested in learning from the experience of others how this could be improved - security-wise, performance-wise, or otherwise.

//-------------------------------------------------------------------------
// Define global constants
define('ROOT_PATH', dirname(__FILE__) . '/');
define('URL_BASE', 'http://localhost/myapp/public/');
define('LIB_PATH', 'C:\\wamp\\www\\lib\\');
define('OPENID_PATH', 'C:\\wamp\www\\lib\\lightopenid.git\\openid.php');

//-------------------------------------------------------------------------
// Bootstrap Doctrine.php, register autoloader, specify
// configuration attributes and load models.
require_once(LIB_PATH . 'doctrine12/lib/Doctrine.php');
spl_autoload_register(array('Doctrine', 'autoload'));
$manager = Doctrine_Manager::getInstance();

//-------------------------------------------------------------------------
// Define database connection
$dsn = 'mysql:dbname=mydb;host=127.0.0.1';
$user = 'xxx';
$password = 'yyy';
$dbh = new PDO($dsn, $user, $password);
$conn = Doctrine_Manager::connection($dbh);

//-------------------------------------------------------------------------
// Set defaults
$manager->setAttribute(Doctrine::ATTR_DEFAULT_COLUMN_OPTIONS,
  array('type' => 'string', 'length' => 255, 'notnull' => true));
$conn->setAttribute(Doctrine_Core::ATTR_QUOTE_IDENTIFIER, true);
$manager->setAttribute(Doctrine_Core::ATTR_AUTO_ACCESSOR_OVERRIDE, true);
// Don't load a model until it's needed (causes problems when this is on)
//$manager->setAttribute(Doctrine_Core::ATTR_MODEL_LOADING, Doctrine_Core::MODEL_LOADING_CONSERVATIVE);

//-------------------------------------------------------------------------
// Import model objects
Doctrine_Core::loadModels(ROOT_PATH . 'app/models/generated'); // have to load base classes first
Doctrine_Core::loadModels(ROOT_PATH . 'app/models');
\$\endgroup\$
7
\$\begingroup\$

Here are a few lines that might be useful to add if you would like to use these options:

/**
 * Needed for SoftDelete to work
 */
$manager->setAttribute(Doctrine::ATTR_USE_DQL_CALLBACKS, true);

/**
 * Tell doctrine to look for custom ___Table classes in the models folder
 */
$manager->setAttribute(Doctrine::ATTR_AUTOLOAD_TABLE_CLASSES, true);
\$\endgroup\$
5
\$\begingroup\$

where you state Define global constants your not defining your directories with the correct slashes for the Operating system

You can use / slashes fluently on your application as both unix and windows supports them but if you really want to be on the safe side then you should use DIRECTORY_SEPARATOR

As Doctrine 2.0 is only supporting PHP 5.3.0 you can replace dirname(__FILE__) with __DIR__

I would also create a constant called DS which would normally be created in a constants file, and should be one of the first included.

I would convert the above to:

 define('ROOT_PATH'  ,  __DIR__);
 define('URL_BASE'   ,  'http://localhost/myapp/public/');
 define('LIB_PATH'   ,  realpath("../../path/to/libs")); //Unix / Windows
 define('OPENID_PATH',  realpath("../../path/to/openid"));//Unix / Windows

And then change the loadModels accordingly?

\$\endgroup\$
  • \$\begingroup\$ Excellent information, thanks! Just one point of clarification - you never stated what the DS constant is or where you would use it. \$\endgroup\$ – BenV Jan 29 '11 at 15:10
  • 1
    \$\begingroup\$ sorry, the DS Constants is a shortor version for DIRECTORY_SEPARATOR and you would then build your paths like so: LIB_ROOT . DS . "openid" . DS . "openid.class.php" to save line space. \$\endgroup\$ – RobertPitt Jan 29 '11 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.