public function _initDoctrine() { $this->getApplication()->getAutoloader()->pushAutoloader(array('Doctrine', 'autoload')); spl_autoload_register(array('Doctrine', 'modelsAutoload')); $manager = Doctrine_Manager::getInstance(); $config = $this->getOption('doctrine'); Doctrine_Core::setModelsDirectory($config['models_path']); $connection = Doctrine_Manager::connection($config['dsn'], 'doctrine'); $profiler = new Doctrine_Connection_Profiler(); $connection->setListener($profiler); }
public function testClassExistsAfterImport() { Doctrine_Core::setModelsDirectory(dirname(__FILE__) . '/DC95/models'); $import = new Doctrine_Import_Schema(); $import->setOptions(array('pearStyle' => true, 'baseClassesDirectory' => null, 'baseClassPrefix' => 'Base_', 'classPrefix' => 'DC95_', 'classPrefixFiles' => true)); $modelsPath = dirname(__FILE__) . '/DC95/models'; $import->importSchema(dirname(__FILE__) . '/DC95/schema.yml', 'yml', $modelsPath); /* $this->assertTrue(file_exists($modelsPath . '/DC95/Base/Article.php')); $this->assertTrue(file_exists($modelsPath . '/DC95/Base/Article/Category.php')); $this->assertTrue(file_exists($modelsPath . '/DC95/Article.php')); $this->assertTrue(file_exists($modelsPath . '/DC95/Article/Category.php')); */ Doctrine_Core::setModelsDirectory(null); Doctrine_Lib::removeDirectories(dirname(__FILE__) . '/DC95/models'); }
public static function registerDoctrine() { if (self::$doctrineLoaded) { return; } $settings = sgConfiguration::get('settings.DoctrinePlugin'); Doctrine_Core::setExtensionsPath(self::getPath('extensions')); Doctrine_Core::setModelsDirectory(self::getPath('models')); $manager = Doctrine_Manager::getInstance(); $manager->openConnection($settings['dsn'], 'doctrine'); if (isset($settings['attributes'])) { foreach ($settings['attributes'] as $attribute => $value) { $manager->setAttribute($attribute, $value); } } self::$doctrineLoaded = true; }
function __construct() { $manager = Doctrine_Manager::getInstance(); $manager->setAttribute(Doctrine::ATTR_QUOTE_IDENTIFIER, true); Doctrine_Core::setModelsDirectory(MODELS_PATH); if (!self::$_dbConnection) { try { self::$_dbConnection = Doctrine_Manager::connection(new PDO($this->_dbtype . ':host=' . $this->_host . ';port=' . $this->_port . ';dbname=' . $this->_dbName, $this->_dbUser, $this->_dbPwd, $this->_db_params)); } catch (PDOException $e) { self::$_dbConnection = null; /* error message output */ if ($this->_production_mode === true) { file_put_contents('dortine_errors.log', $e->getMessage(), FILE_APPEND); // log the errors die($e->getMessage()); } else { die($e->getMessage()); } } // end try } // end if }
define('SANDBOX_PATH', dirname(__FILE__)); define('BASEPATH', dirname(__FILE__)); define('DOCTRINE_PATH', APP_PATH . DS . 'plugins' . DS . 'doctrine' . DS . 'lib'); define('DATA_FIXTURES_PATH', dirname(SANDBOX_PATH) . DS . 'orm' . DS . 'fixtures'); define('MODELS_PATH', APP_PATH . DS . 'models'); define('MIGRATIONS_PATH', dirname(SANDBOX_PATH) . DS . 'orm' . DS . 'migrations'); define('SQL_PATH', dirname(SANDBOX_PATH) . DS . 'orm' . DS . 'sql'); define('YAML_SCHEMA_PATH', dirname(SANDBOX_PATH) . DS . 'orm' . DS . 'schema/schema.yml'); //define('DB_PATH', SANDBOX_PATH . DS . 'sandbox.db'); define('DSN', 'mysql://*****:*****@localhost/db_lola'); require_once(DOCTRINE_PATH . DS . 'Doctrine.php'); require_once APP_PATH . DS . 'config' . DS . 'database.php'; Doctrine_Core::setModelsDirectory(MODELS_PATH); //Doctrine_Core::setExtensionsPath(dirname(__FILE__).'/extensions'); spl_autoload_register(array('Doctrine', 'autoload')); spl_autoload_register(array('Doctrine', 'modelsAutoload')); //spl_autoload_register(array('Doctrine', 'extensionsAutoload')); $manager = Doctrine_Manager::getInstance(); //$manager->setAttribute(Doctrine_Core::ATTR_MODEL_LOADING, Doctrine_Core::MODEL_LOADING_PEAR); $manager->setAttribute(Doctrine::ATTR_AUTO_ACCESSOR_OVERRIDE, true); $manager->setAttribute( Doctrine::ATTR_MODEL_LOADING, DOctrine::MODEL_LOADING_CONSERVATIVE );
* * This is a sample implementation of Doctrine * * @package Doctrine * @subpackage Config * @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @link www.phpdoctrine.org * @since 1.0 * @version $Revision: 2753 $ * @author Konsta Vesterinen <*****@*****.**> * @author Jonathan H. Wage <*****@*****.**> */ define('SANDBOX_PATH', dirname(__FILE__)); define('DOCTRINE_PATH', dirname(dirname(SANDBOX_PATH)) . DIRECTORY_SEPARATOR . 'lib'); define('DATA_FIXTURES_PATH', SANDBOX_PATH . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'fixtures'); define('MODELS_PATH', SANDBOX_PATH . DIRECTORY_SEPARATOR . 'models'); define('MIGRATIONS_PATH', SANDBOX_PATH . DIRECTORY_SEPARATOR . 'migrations'); define('SQL_PATH', SANDBOX_PATH . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'sql'); define('YAML_SCHEMA_PATH', SANDBOX_PATH . DIRECTORY_SEPARATOR . 'schema'); define('DB_PATH', SANDBOX_PATH . DIRECTORY_SEPARATOR . 'sandbox.db'); define('DSN', 'sqlite:///' . DB_PATH); require_once DOCTRINE_PATH . DIRECTORY_SEPARATOR . 'Doctrine.php'; Doctrine_Core::setExtensionsPath(dirname(__FILE__) . '/extensions'); spl_autoload_register(array('Doctrine', 'autoload')); spl_autoload_register(array('Doctrine', 'modelsAutoload')); spl_autoload_register(array('Doctrine', 'extensionsAutoload')); $manager = Doctrine_Manager::getInstance(); $manager->openConnection(DSN, 'doctrine'); $manager->setAttribute(Doctrine_Core::ATTR_MODEL_LOADING, Doctrine_Core::MODEL_LOADING_PEAR); Doctrine_Core::setModelsDirectory('models');
/** * Initialize some Doctrine models at a given path. * * @param string $path * @return array $models */ protected function _initializeModels($path) { $manager = Doctrine_Manager::getInstance(); $modelLoading = $manager->getAttribute(Doctrine_Core::ATTR_MODEL_LOADING); if ($modelLoading === Doctrine_Core::MODEL_LOADING_PEAR) { $orig = Doctrine_Core::getModelsDirectory(); Doctrine_Core::setModelsDirectory($path); $models = Doctrine_Core::initializeModels(Doctrine_Core::loadModels($path)); Doctrine_Core::setModelsDirectory($orig); } else { $models = Doctrine_Core::initializeModels(Doctrine_Core::loadModels($path)); } return $models; }
/** * Initialize Doctrine set the autoloading * * @param AgaviDatabaseManager The database manager of this instance. * @param array An assoc array of initialization params. * * @author David Zülke <*****@*****.**> * @author Ross Lawley <*****@*****.**> * @author TANAKA Koichi <*****@*****.**> * @since 0.11.0 */ public function initialize(AgaviDatabaseManager $databaseManager, array $parameters = array()) { parent::initialize($databaseManager, $parameters); $name = $this->getName(); // try to autoload doctrine if (!class_exists('Doctrine')) { // okay that didn't work. last resort: include it. we assume it's on the include path by default require 'Doctrine.php'; } $is12 = version_compare(Doctrine::VERSION, '1.2', '>='); // in any case, it's loaded now. maybe we need to register the autoloading stuff for it! // we need this list further down $splAutoloadFunctions = spl_autoload_functions(); if (!in_array(array('Doctrine', 'autoload'), $splAutoloadFunctions) && !in_array(array('Doctrine_Core', 'autoload'), $splAutoloadFunctions)) { // we do spl_autoload_register(array('Doctrine', 'autoload')); } // cool. Assign the Doctrine Manager instance $this->doctrineManager = Doctrine_Manager::getInstance(); // now we're in business. we will set up connections right away, as Doctrine is handling the lazy-connecting stuff for us. // that way, you can just start using classes in your code try { $dsn = $this->getParameter('dsn'); if ($dsn === null) { // missing required dsn parameter $error = 'Database configuration specifies method "dsn", but is missing dsn parameter'; throw new AgaviDatabaseException($error); } $this->connection = $this->doctrineManager->openConnection($dsn, $name); // do not assign the resource here. that would connect to the database // $this->resource = $this->connection->getDbh(); // set our event listener that, on connect, sets the configured charset and runs init queries $this->connection->setListener(new AgaviDoctrineDatabaseEventListener($this)); $this->connection->setPrefix($this->getParameter('prefix', "")); // set the context instance as a connection parameter $this->connection->setParam('context', $databaseManager->getContext(), 'org.agavi'); // date format if ($this->hasParameter('date_format')) { $this->connection->setDateFormat($this->getParameter('date_format')); } // options foreach ((array) $this->getParameter('options') as $optionName => $optionValue) { $this->connection->setOption($optionName, $optionValue); } foreach (array('manager_attributes' => $this->doctrineManager, 'attributes' => $this->connection) as $attributesKey => $attributesDestination) { foreach ((array) $this->getParameter($attributesKey, array()) as $attributeName => $attributeValue) { if ($is12) { if (!strpos($attributeName, '::')) { throw new AgaviDatabaseException(sprintf('For Doctrine 1.2 and newer, attribute names (and, if desired to be resolved against a constant, values) must be fully qualified, e.g. "Doctrine_Core::ATTR_VALIDATE" and "Doctrine_Core::VALIDATE_NONE". Given attribute with name "%s" in collection "%s" does not match this condition.', $attributeName, $attributesKey)); } if (!defined($attributeName)) { throw new AgaviDatabaseException(sprintf('Unknown Attribute "%s"', $attributeName)); } } // resolve from constant if possible if (strpos($attributeName, '::') && defined($attributeName)) { $attributeName = constant($attributeName); } // resolve from constant if possible if (strpos($attributeValue, '::') && defined($attributeValue)) { $attributeValue = constant($attributeValue); } elseif (ctype_digit($attributeValue)) { $attributeValue = (int) $attributeValue; } $attributesDestination->setAttribute($attributeName, $attributeValue); } } foreach ((array) $this->getParameter('impls', array()) as $templateName => $className) { $this->connection->setImpl($templateName, $className); } foreach ((array) $this->getParameter('manager_impls', array()) as $templateName => $className) { $this->doctrineManager->setImpl($templateName, $className); } // load models (that'll just work with empty values too) Doctrine::loadModels($this->getParameter('load_models')); // for 1.2, handle model autoloading and base paths if ($is12 && ($this->hasParameter('load_models') || $this->hasParameter('models_directory'))) { if (!in_array(array('Doctrine', 'modelsAutoload'), $splAutoloadFunctions) && !in_array(array('Doctrine_Core', 'modelsAutoload'), $splAutoloadFunctions)) { spl_autoload_register(array('Doctrine_Core', 'modelsAutoload')); } if ($this->hasParameter('models_directory')) { Doctrine_Core::setModelsDirectory($this->getParameter('models_directory')); } } // for 1.2, handle extension autoloading, base paths and registration if ($is12 && ($this->hasParameter('extensions_path') || $this->hasParameter('register_extensions'))) { if (!in_array(array('Doctrine', 'extensionsAutoload'), $splAutoloadFunctions) && !in_array(array('Doctrine_Core', 'extensionsAutoload'), $splAutoloadFunctions)) { spl_autoload_register(array('Doctrine_Core', 'extensionsAutoload')); } if ($this->hasParameter('extensions_path')) { Doctrine_Core::setExtensionsPath($this->getParameter('extensions_path')); } foreach ((array) $this->getParameter('register_extensions', array()) as $extensionName) { if (is_array($extensionName)) { call_user_func_array(array($this->doctrineManager, 'registerExtension'), $extensionName); } else { $this->doctrineManager->registerExtension($extensionName); } } } foreach ((array) $this->getParameter('bind_components', array()) as $componentName) { $this->doctrineManager->bindComponent($componentName, $name); } } catch (Doctrine_Exception $e) { // the connection's foobar'd throw new AgaviDatabaseException($e->getMessage()); } }
<?php // system/application/plugins/doctrine_pi.php // load Doctrine library require_once APPPATH . '/plugins/doctrine/lib/Doctrine.php'; // load database configuration from CodeIgniter require_once APPPATH . '/config/database.php'; Doctrine_Core::setModelsDirectory(APPPATH . '/models'); // this will allow Doctrine to load Model classes automatically spl_autoload_register(array('Doctrine', 'autoload')); spl_autoload_register(array('Doctrine', 'modelsAutoload')); // we load our database connections into Doctrine_Manager // this loop allows us to use multiple connections later on foreach ($db as $connection_name => $db_values) { // first we must convert to dsn format $dsn = $db[$connection_name]['dbdriver'] . '://' . $db[$connection_name]['username'] . ':' . $db[$connection_name]['password'] . '@' . $db[$connection_name]['hostname'] . '/' . $db[$connection_name]['database']; Doctrine_Manager::connection($dsn, $connection_name); } // CodeIgniter's Model class needs to be loaded require_once BASEPATH . '/libraries/Model.php'; // (OPTIONAL) CONFIGURATION BELOW
/** * Initialize some Doctrine models at a given path. * * @param string $path * @return array $models */ protected function _initializeModels($path) { $orig = Doctrine_Core::getModelsDirectory(); Doctrine_Core::setModelsDirectory($path); $models = Doctrine_Core::initializeModels(Doctrine_Core::loadModels($path)); Doctrine_Core::setModelsDirectory($orig); return $models; }