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\$
3
  • \$\begingroup\$ How different is this from the default template? Can you link to it? \$\endgroup\$
    – TryPyPy
    Jan 20, 2011 at 1:30
  • \$\begingroup\$ @TryPyPy: doctrine-project.org/projects/orm/1.2/docs/manual/… \$\endgroup\$
    – BenV
    Jan 20, 2011 at 3:30
  • \$\begingroup\$ @ZoranJankov I have rejected your suggested edit because it does not improve the quality of the title. A title must uniquely describe what the script does -- not what it is & what it is comprised of. \$\endgroup\$ Nov 13, 2020 at 23:26

2 Answers 2

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\$
2
  • \$\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, 2011 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, 2011 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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