8
\$\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');
asked Jan 20, 2011 at 0:52
\$\endgroup\$
3
  • \$\begingroup\$ How different is this from the default template? Can you link to it? \$\endgroup\$ Commented Jan 20, 2011 at 1:30
  • \$\begingroup\$ @TryPyPy: doctrine-project.org/projects/orm/1.2/docs/manual/… \$\endgroup\$ Commented 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\$ Commented Nov 13, 2020 at 23:26

2 Answers 2

9
\$\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);
palacsint
30.3k9 gold badges82 silver badges157 bronze badges
answered Jan 20, 2011 at 1:48
\$\endgroup\$
7
\$\begingroup\$

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.

Laurel
7954 silver badges20 bronze badges
answered Jan 29, 2011 at 14:48
\$\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\$ Commented 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\$ Commented Jan 29, 2011 at 15:50

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.