Ticket #381: BaseDoctrineDatabase.class.php

File BaseDoctrineDatabase.class.php, 3.2 KB (added by ross, 16 months ago)
Line 
1<?php
2
3/**
4 * An Agavi Database Wrapper ORM for Doctrine, derived from the native PDO driver.
5 *
6 *
7 * @package    BusinessEdge
8 * @subpackage database
9 *
10 * @author     Ross Lawley <ross.lawley@libraryhouse.net>
11 * @since      0.1
12 *
13 * @version    $Id: BaseDoctrineDatabase.class.php 585 2007-06-25 13:58:12Z  $
14 */
15class BaseDoctrineDatabase extends AgaviDatabase
16{
17
18  /**
19   * Stores the actual AgaviDatabase connection
20   *
21   * @var        AgaviDatabase The AgaviDatabase instance used internally.
22   *
23   * @since      0.1
24   */
25  protected $connection = null;
26
27  /**
28   * Connect to the database.
29   *
30   *
31   * @throws     <b>AgaviDatabaseException</b> If a connection could not be
32   *                                           created.
33   *
34   * @author     Ross Lawley <ross.lawley@libraryhouse.net>
35   * @since      0.1
36   */
37  public function connect()
38  {
39    try {
40      // determine how to get our settings
41      $method = $this->getParameter('method', 'normal');
42      switch ($method) {
43        case 'normal':
44          $runtime = AgaviToolkit::expandDirectives($this->getParameter('config', null));
45          break;
46        case 'server':
47          $runtime = $_SERVER[$this->getParameter('config')];
48          break;
49        case 'env':
50          $runtime = $_ENV[$this->getParameter('config')];
51          break;
52        default:
53          $error = 'Invalid DoctrineDatabase parameter retrieval method "%s"';
54          $error = sprintf($error, $method);
55          throw new AgaviDatabaseException($error);
56      }
57
58      $dsn = $this->getParameter('dsn');
59      if($dsn == null) {
60        // missing required dsn parameter
61        $error = 'Database configuration specifies method "dsn", but is missing dsn parameter';
62        throw new AgaviDatabaseException($error);
63      }
64
65      //$this->agaviDatabase = new Doctrine_Connection($dsn);
66      $this->connection = Doctrine_Manager::connection($dsn);
67      $this->resource =& $this->manager;
68    } catch( Doctrine_Db_Exception $e) {
69      // the connection's foobar'd
70      throw new AgaviDatabaseException($e->getMessage ());
71    }
72  }
73
74  /**
75   * Initialize Doctrine set the autoloading
76   *
77   * @param      AgaviDatabaseManager The database manager of this instance.
78   * @param      array An associative array of initialization parameters.
79   *
80   * @author     Ross Lawley <ross.lawley@libraryhouse.net>
81   * @since      0.11.?
82   */
83  public function initialize(AgaviDatabaseManager $databaseManager, array $parameters = array())
84  {
85    parent::initialize($databaseManager, $parameters);
86    // Ensure that Doctrine hasn't already been included
87    if (!class_exists('Doctrine')) {
88      // get doctrine class path
89      $classPath = AgaviToolkit::expandDirectives($this->getParameter('classpath',null));
90      if(!is_null($classPath)) {
91        require($classPath);
92      } else {
93        require(AgaviConfig::get('core.libs_dir').'/doctrine/Doctrine.php');
94      }
95      spl_autoload_register(array('Doctrine', 'autoload'));
96    }
97  }
98
99  /**
100   * Execute the shutdown procedure.
101   *
102   * @throws     <b>AgaviDatabaseException</b> If an error occurs while shutting
103   *                                           down this database.
104   *
105   * @author     Ross Lawley <ross.lawley@libraryhouse.net>
106   * @since      0.1
107   */
108  public function shutdown()
109  {
110    return;
111  }
112}
113
114?>