| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | // +---------------------------------------------------------------------------+ |
|---|
| 4 | // | This file is part of the Agavi package. | |
|---|
| 5 | // | Copyright (c) 2005-2008 the Agavi Project. | |
|---|
| 6 | // | | |
|---|
| 7 | // | For the full copyright and license information, please view the LICENSE | |
|---|
| 8 | // | file that was distributed with this source code. You can also view the | |
|---|
| 9 | // | LICENSE file online at http://www.agavi.org/LICENSE.txt | |
|---|
| 10 | // | vi: set noexpandtab: | |
|---|
| 11 | // | Local Variables: | |
|---|
| 12 | // | indent-tabs-mode: t | |
|---|
| 13 | // | End: | |
|---|
| 14 | // +---------------------------------------------------------------------------+ |
|---|
| 15 | |
|---|
| 16 | /** |
|---|
| 17 | * A database adapter for the Doctrine ORM. |
|---|
| 18 | * |
|---|
| 19 | * @package agavi |
|---|
| 20 | * @subpackage database |
|---|
| 21 | * |
|---|
| 22 | * @author Ross Lawley <ross.lawley@gmail.com> |
|---|
| 23 | * @author David Zülke <dz@bitxtender.com> |
|---|
| 24 | * @copyright Authors |
|---|
| 25 | * @copyright The Agavi Project |
|---|
| 26 | * |
|---|
| 27 | * @since 0.11.0 |
|---|
| 28 | * |
|---|
| 29 | * @version $Id$ |
|---|
| 30 | */ |
|---|
| 31 | class AgaviDoctrineDatabase extends AgaviDatabase |
|---|
| 32 | { |
|---|
| 33 | /** |
|---|
| 34 | * @var Doctrine_Manager The Doctrine Manager instance we should use. |
|---|
| 35 | */ |
|---|
| 36 | protected $doctrineManager; |
|---|
| 37 | |
|---|
| 38 | /** |
|---|
| 39 | * Connect to the database. |
|---|
| 40 | * |
|---|
| 41 | * @throws <b>AgaviDatabaseException</b> If a connection could not be |
|---|
| 42 | * created. |
|---|
| 43 | * |
|---|
| 44 | * @author David Zülke <dz@bitxtender.com> |
|---|
| 45 | * @since 0.11.0 |
|---|
| 46 | */ |
|---|
| 47 | public function connect() |
|---|
| 48 | { |
|---|
| 49 | // this doesn't do anything, Doctrine is handling the lazy connection stuff |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | /** |
|---|
| 53 | * Retrieve a raw database resource associated with this Database |
|---|
| 54 | * implementation. |
|---|
| 55 | * |
|---|
| 56 | * @return mixed A database resource. |
|---|
| 57 | * |
|---|
| 58 | * @throws <b>AgaviDatabaseException</b> If no resource could be retrieved |
|---|
| 59 | * |
|---|
| 60 | * @author David Zülke <dz@bitxtender.com> |
|---|
| 61 | * @since 0.11.0 |
|---|
| 62 | */ |
|---|
| 63 | public function getResource() |
|---|
| 64 | { |
|---|
| 65 | return $this->connection->getDbh(); |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | /** |
|---|
| 69 | * Initialize Doctrine set the autoloading |
|---|
| 70 | * |
|---|
| 71 | * @param AgaviDatabaseManager The database manager of this instance. |
|---|
| 72 | * @param array An assoc array of initialization params. |
|---|
| 73 | * |
|---|
| 74 | * @author David Zülke <dz@bitxtender.com> |
|---|
| 75 | * @author Ross Lawley <ross.lawley@gmail.com> |
|---|
| 76 | * @since 0.11.0 |
|---|
| 77 | */ |
|---|
| 78 | public function initialize(AgaviDatabaseManager $databaseManager, array $parameters = array()) |
|---|
| 79 | { |
|---|
| 80 | parent::initialize($databaseManager, $parameters); |
|---|
| 81 | |
|---|
| 82 | $name = $this->getName(); |
|---|
| 83 | |
|---|
| 84 | // try to autoload doctrine |
|---|
| 85 | if(!class_exists('Doctrine')) { |
|---|
| 86 | // okay that didn't work. last resort: include it. we assume it's on the include path by default |
|---|
| 87 | require('Doctrine.php'); |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | // in any case, it's loaded now. maybe we need to register the autoloading stuff for it! |
|---|
| 91 | if(!in_array(array('Doctrine', 'autoload'), spl_autoload_functions())) { |
|---|
| 92 | // we do |
|---|
| 93 | spl_autoload_register(array('Doctrine', 'autoload')); |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | // cool. Assign the Doctrine Manager instance |
|---|
| 97 | $this->doctrineManager = Doctrine_Manager::getInstance(); |
|---|
| 98 | |
|---|
| 99 | // now we're in business. we will set up connections right away, as Doctrine is handling the lazy-connecting stuff for us. |
|---|
| 100 | // that way, you can just start using classes in your code |
|---|
| 101 | try { |
|---|
| 102 | $dsn = $this->getParameter('dsn'); |
|---|
| 103 | |
|---|
| 104 | if($dsn === null) { |
|---|
| 105 | // missing required dsn parameter |
|---|
| 106 | $error = 'Database configuration specifies method "dsn", but is missing dsn parameter'; |
|---|
| 107 | throw new AgaviDatabaseException($error); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | $this->connection = $this->doctrineManager->openConnection($dsn, $name); |
|---|
| 111 | // do not assign the resource here. that would connect to the database |
|---|
| 112 | // $this->resource = $this->connection->getDbh(); |
|---|
| 113 | |
|---|
| 114 | foreach((array)$this->getParameter('attributes', array()) as $attributeName => $attributeValue) { |
|---|
| 115 | $this->connection->setAttribute($attributeName, $attributeValue); |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | foreach((array)$this->getParameter('bind_components', array()) as $componentName) { |
|---|
| 119 | $this->doctrineManager->bindComponent($componentName, $name); |
|---|
| 120 | } |
|---|
| 121 | } catch(Doctrine_Exception $e) { |
|---|
| 122 | // the connection's foobar'd |
|---|
| 123 | throw new AgaviDatabaseException($e->getMessage()); |
|---|
| 124 | } |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | /** |
|---|
| 128 | * Execute the shutdown procedure. |
|---|
| 129 | * |
|---|
| 130 | * @throws <b>AgaviDatabaseException</b> If an error occurs while shutting |
|---|
| 131 | * down this database. |
|---|
| 132 | * |
|---|
| 133 | * @author David Zülke <dz@bitxtender.com> |
|---|
| 134 | * @since 0.11.0 |
|---|
| 135 | */ |
|---|
| 136 | public function shutdown() |
|---|
| 137 | { |
|---|
| 138 | if($this->connection !== null) { |
|---|
| 139 | $this->doctrineManager->closeConnection($this->connection); |
|---|
| 140 | $this->connection = null; |
|---|
| 141 | $this->resource = null; |
|---|
| 142 | } |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | /** |
|---|
| 146 | * Get the Doctrine Manager instance. |
|---|
| 147 | * |
|---|
| 148 | * @return Doctrine_Manager The Doctrine Manager instance. |
|---|
| 149 | * |
|---|
| 150 | * @author David Zülke <dz@bitxtender.com> |
|---|
| 151 | * @since 0.11.0 |
|---|
| 152 | */ |
|---|
| 153 | public function getDoctrineManager() |
|---|
| 154 | { |
|---|
| 155 | return $this->doctrineManager; |
|---|
| 156 | } |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | ?> |
|---|