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');
-
\$\begingroup\$ How different is this from the default template? Can you link to it? \$\endgroup\$TryPyPy– TryPyPy2011年01月20日 01:30:57 +00:00Commented Jan 20, 2011 at 1:30
-
\$\begingroup\$ @TryPyPy: doctrine-project.org/projects/orm/1.2/docs/manual/… \$\endgroup\$BenV– BenV2011年01月20日 03:30:06 +00:00Commented 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\$mickmackusa– mickmackusa2020年11月13日 23:26:23 +00:00Commented Nov 13, 2020 at 23:26
2 Answers 2
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);
Where you state Define global constants you're not defining your directories with the correct slashes for the operating system.
You can use /
slashes in your application as both Unix and Windows support them but if you really want to be on the safe side then you should use DIRECTORY_SEPARATOR
.
Since Doctrine 2.0 only supports PHP 5.3.0 or higher, 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.
-
\$\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– BenV2011年01月29日 15:10:13 +00:00Commented Jan 29, 2011 at 15:10 -
1\$\begingroup\$ sorry, the
DS
Constants is a shortor version forDIRECTORY_SEPARATOR
and you would then build your paths like so:LIB_ROOT . DS . "openid" . DS . "openid.class.php"
to save line space. \$\endgroup\$RobertPitt– RobertPitt2011年01月29日 15:50:28 +00:00Commented Jan 29, 2011 at 15:50