Ticket #1597: BaseDAO.php
| File BaseDAO.php, 4.5 kB (added by malber, 5 years ago) |
|---|
| Line | |
|---|---|
| 1 | <?php |
| 2 | /* Reminder: always indent with 4 spaces (no tabs). */ |
| 3 | // +---------------------------------------------------------------------------+ |
| 4 | // | Copyright (c) 2008, Demian Turner | |
| 5 | // | All rights reserved. | |
| 6 | // | | |
| 7 | // | Redistribution and use in source and binary forms, with or without | |
| 8 | // | modification, are permitted provided that the following conditions | |
| 9 | // | are met: | |
| 10 | // | | |
| 11 | // | o Redistributions of source code must retain the above copyright | |
| 12 | // | notice, this list of conditions and the following disclaimer. | |
| 13 | // | o Redistributions in binary form must reproduce the above copyright | |
| 14 | // | notice, this list of conditions and the following disclaimer in the | |
| 15 | // | documentation and/or other materials provided with the distribution. | |
| 16 | // | o The names of the authors may not be used to endorse or promote | |
| 17 | // | products derived from this software without specific prior written | |
| 18 | // | permission. | |
| 19 | // | | |
| 20 | // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 21 | // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 22 | // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 23 | // | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 24 | // | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 25 | // | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 26 | // | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 27 | // | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 28 | // | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 29 | // | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 30 | // | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 31 | // | | |
| 32 | // +---------------------------------------------------------------------------+ |
| 33 | // | Seagull 0.6 | |
| 34 | // +---------------------------------------------------------------------------+ |
| 35 | // | BaseDAO.php | |
| 36 | // +---------------------------------------------------------------------------+ |
| 37 | // | Author: Michael K. Alber <malber@interactdigitalmedia.com> | |
| 38 | // +---------------------------------------------------------------------------+ |
| 39 | // $Id: $ |
| 40 | |
| 41 | /** |
| 42 | * based on http://article.gmane.org/gmane.comp.php.seagull.general/8035/match=base+dao |
| 43 | * by Dmitri Lakachauskis <lakiboy83@gmail.com> |
| 44 | * @category Library |
| 45 | * @package SGL |
| 46 | * @author Michael K. Alber <malber@interactdigitalmedia.com> |
| 47 | * @copyright Copyright (c) 2008, Demian Turner |
| 48 | * @version SVN: $Id: $ |
| 49 | * @since Wed Mar 19 22:16:18 PDT 2008 22:16:18 |
| 50 | */ |
| 51 | /* Reminder: always indent with 4 spaces (no tabs). */ |
| 52 | |
| 53 | class SGL_BaseDAO |
| 54 | { |
| 55 | public $conf = array(); |
| 56 | public $dbh = null; |
| 57 | |
| 58 | /** |
| 59 | * Constructor. |
| 60 | * |
| 61 | * @access public |
| 62 | * @return void |
| 63 | */ |
| 64 | |
| 65 | function SGL_BaseDAO() |
| 66 | { |
| 67 | $moduleName = str_replace('dao', '', strtolower(get_class($this))); |
| 68 | $c = &SGL_Config::singleton(); |
| 69 | $this->conf = $c->ensureModuleConfigLoaded($moduleName); |
| 70 | $this->dbh = &$this->_getDb(); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * @access public |
| 75 | * @static |
| 76 | * @return DAO class reference to DAO object |
| 77 | */ |
| 78 | function &singleton() |
| 79 | { |
| 80 | static $instance; |
| 81 | |
| 82 | // If the instance is not there, create one |
| 83 | if (!isset($instance)) { |
| 84 | $class = __CLASS__; |
| 85 | $instance = new $class(); |
| 86 | } |
| 87 | return $instance; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Enter description here... |
| 92 | * |
| 93 | * @return unknown |
| 94 | */ |
| 95 | function &_getDb() |
| 96 | { |
| 97 | $locator = &SGL_ServiceLocator::singleton(); |
| 98 | $dbh = $locator->get('DB'); |
| 99 | if (!$dbh) { |
| 100 | $dbh = &SGL_DB::singleton(); |
| 101 | $locator->register('DB', $dbh); |
| 102 | } |
| 103 | return $dbh; |
| 104 | } |
| 105 | |
| 106 | function getConfig() |
| 107 | { |
| 108 | $c = &SGL_Config::singleton(); |
| 109 | return $c; |
| 110 | } |
| 111 | } |
| 112 | ?> |
