Report abuse


			

 * @since      0.9.0
 *
 * @version    $Id$
 */
class AgaviDoctrineDatabase extends AgaviDatabase
{
  /**
   * Stores the actual AgaviDatabase implementation (AgaviCreoleDatabase or 
   * AgaviPdoDatabase).
   *
   * @var        AgaviDatabase The AgaviDatabase instance used internally.
   *
   * @since      0.11.0
   */
  protected $agaviDatabase = null;

    /**
   * Stores the actual AgaviDatabase connection
   *
   * @var        AgaviDatabase The AgaviDatabase instance used internally.
   *
   * @since      0.11.0
   */
  protected $connection = null;

  /**
   * Connect to the database.
   * 
   *
   * @throws     AgaviDatabaseException If a connection could not be 
   *                                           created.
   *
     * @author     Ross Lawley 
   * @since      0.11.0
   */
  public function connect()
  {
    try {
      // determine how to get our settings
      $method = $this->getParameter('method', 'normal');
      switch ($method) {
        case 'normal':
          $runtime = AgaviConfigHandler::replaceConstants($this->getParameter('config', null));
          break;
        case 'server':
          $runtime = $_SERVER[$this->getParameter('config')];
          break;
        case 'env':
          $runtime = $_ENV[$this->getParameter('config')];
          break;
        default:
          $error = 'Invalid DoctrineDatabase parameter retrieval method "%s"';
          $error = sprintf($error, $method);
          throw new AgaviDatabaseException($error);
      }

            $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->agaviDatabase = new Doctrine_Db($dsn);
      $this->connection = Doctrine_Manager::connection($this->agaviDatabase);
      $this->resource =& $this->manager;
    } catch( Doctrine_Db_Exception $e) {
      // the connection's foobar'd
      throw new AgaviDatabaseException($e->getMessage ());
    }
  }

  /**
   * Initialize Doctrine set the autoloading
   * 
   * @param      AgaviDatabaseManager The database manager of this instance.
   * @param      array An associative array of initialization parameters.
   *
   * @author     Ross Lawley 
   * @since      0.11.0
   */
  public function initialize(AgaviDatabaseManager $databaseManager, array $parameters = array())
  {
    parent::initialize($databaseManager, $parameters);
        // get doctrine class path
        $classPath = AgaviConfigHandler::replaceConstants($this->getParameter('classpath',null));
        // set the include path to our Propel generated classes
        if(!is_null($classPath)) {
            require($classPath);
        } else {
            require(AgaviConfig::get('core.lib_dir').'/Doctrine.php');
        }

        spl_autoload_register(array('Doctrine', 'autoload'));
        spl_autoload_register(array($this, 'autoload'));
  }


  /**
   * Autoloading function for Doctrine Models
   *
   * @param      string The name of the class to autoload.
   *
   * @author     Ross Lawley 
   * @since      0.11.0
   */
  public function autoload($className) {
        if (class_exists($className, false)) 
            return true;
    $isModel = (substr($className, -5) == 'Model') ? true : false;
        if (!$isModel)
            return false;
        $modelName = substr($className, 0, -5);
        // Check if is a module model
        $moduleParts = explode("_", $modelName);
        $moduleName = array_shift($moduleParts);
        $moduleModelName = implode("_", $moduleParts);
        $file = AgaviConfig::get('core.module_dir') . '/' . $moduleName . '/models/' . $moduleModelName . 'Model.class.php';
        // If not a module model try a global model
        if(!is_readable($file)) {
            $file = AgaviConfig::get('core.model_dir') . '/' . $modelName . 'Model.class.php';
        }
        // Require the file
        if(is_readable($file)) {
            require($file);
            return true;
        }
        return false;
  }

  /**
   * Execute the shutdown procedure.
   *
   * @throws     AgaviDatabaseException If an error occurs while shutting
   *                                           down this database.
   *
   * @author     Sean Kerr 
   * @since      0.9.0
   */
  public function shutdown()
  {
        // Do I need these??
        spl_autoload_unregister(array('Doctrine', 'autoload'));
        spl_autoload_unregister(array($this, 'autoload'));
    return;
  }
}

?>