public function prepareTables()
 {
     Doctrine_Core::setExtensionsPath(dirname(__FILE__) . '/Extension');
     spl_autoload_register(array('Doctrine', 'extensionsAutoload'));
     Doctrine_Manager::getInstance()->registerExtension('TestExtension');
     $this->tables[] = 'ExtensionBehaviorTest';
     parent::prepareTables();
 }
 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;
 }
Exemple #3
0
 public function getDoctrine()
 {
     if (null === $this->_doctrine) {
         try {
             // Get Doctrine configuration options from the application.ini file
             $doctrineConfig = $this->getOptions();
             require_once 'Doctrine.php';
             $loader = Zend_Loader_Autoloader::getInstance();
             $loader->pushAutoloader(array('Doctrine', 'autoload'));
             $loader->pushAutoloader(array('Doctrine', 'modelsAutoload'));
             $manager = Doctrine_Manager::getInstance();
             if (isset($doctrineConfig['extensions_path']) && isset($doctrineConfig['extensions']) && is_array($doctrineConfig['extensions'])) {
                 Doctrine_Core::setExtensionsPath($doctrineConfig['extensions_path']);
                 $loader->pushAutoloader(array('Doctrine', 'extensionsAutoload'));
                 foreach ($doctrineConfig['extensions'] as $e) {
                     $manager->registerExtension($e);
                 }
             }
             $manager->setAttribute(Doctrine::ATTR_MODEL_LOADING, Doctrine::MODEL_LOADING_CONSERVATIVE);
             $manager->setAttribute(Doctrine::ATTR_AUTOLOAD_TABLE_CLASSES, true);
             $manager->setAttribute(Doctrine::ATTR_USE_DQL_CALLBACKS, true);
             $manager->setCollate('utf8_unicode_ci');
             $manager->setCharset('utf8');
             Doctrine::loadModels($doctrineConfig['models_path']);
             $manager->openConnection($doctrineConfig['connection_string']);
             $manager->connection()->setCollate('utf8_unicode_ci');
             $manager->connection()->setCharset('utf8');
             $this->_doctrine = $manager;
         } catch (Exception $e) {
             echo "<html><body><pre>\nERROR: Your Doctrine set-up is not working.\n\n";
             echo $e->getMessage() . "\n\n";
             echo $e->getTraceAsString();
             echo "</pre></body></html>";
             die;
         }
     }
     return $this->_doctrine;
 }
 /**
  * Get doctrine
  * 
  * @return Doctrine_Manager
  */
 public function getDoctrine()
 {
     if (null === $this->_doctrine) {
         // Get Doctrine configuration options from the application.ini file
         $doctrineConfig = $this->getOptions();
         /**
          * @see Doctrine
          */
         require_once 'Doctrine.php';
         $loader = Zend_Loader_Autoloader::getInstance();
         $loader->pushAutoloader(array('Doctrine', 'autoload'));
         $loader->pushAutoloader(array('Doctrine', 'modelsAutoload'));
         $manager = Doctrine_Manager::getInstance();
         if (isset($doctrineConfig['extensions_path']) && is_array($doctrineConfig['extensions'])) {
             Doctrine_Core::setExtensionsPath($doctrineConfig['extensions_path']);
             $loader->pushAutoloader(array('Doctrine', 'extensionsAutoload'));
             foreach ($doctrineConfig['extensions'] as $e) {
                 $manager->registerExtension($e);
             }
         }
         $manager->setAttribute(Doctrine::ATTR_MODEL_LOADING, Doctrine::MODEL_LOADING_CONSERVATIVE);
         $manager->setAttribute(Doctrine::ATTR_AUTOLOAD_TABLE_CLASSES, true);
         $manager->setAttribute(Doctrine::ATTR_USE_DQL_CALLBACKS, true);
         $manager->setCollate('utf8_unicode_ci');
         $manager->setCharset('utf8');
         Doctrine::loadModels($doctrineConfig['models_path']);
         $db_profiler = new Doctrine_Connection_Profiler();
         $manager->openConnection($doctrineConfig['connection_string']);
         $manager->connection()->setListener($db_profiler);
         $manager->connection()->setCollate('utf8_unicode_ci');
         $manager->connection()->setCharset('utf8');
         Zend_Registry::set('db_profiler', $db_profiler);
         $this->_doctrine = $manager;
     }
     return $this->_doctrine;
 }
 * Doctrine Configuration File
 *
 * 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 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());
     }
 }