Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
 
3
/*
4
 *  $Id: DatabaseMap.php 1262 2009-10-26 20:54:39Z francois $
5
 *
6
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
7
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
8
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
9
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
10
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
11
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
12
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
13
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
14
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
15
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
16
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17
 *
18
 * This software consists of voluntary contributions made by many individuals
19
 * and is licensed under the LGPL. For more information please see
20
 * <http://propel.phpdb.org>.
21
 */
22
 
23
/**
24
 * DatabaseMap is used to model a database.
25
 *
26
 * GENERAL NOTE
27
 * ------------
28
 * The propel.map classes are abstract building-block classes for modeling
29
 * the database at runtime.  These classes are similar (a lite version) to the
30
 * propel.engine.database.model classes, which are build-time modeling classes.
31
 * These classes in themselves do not do any database metadata lookups.
32
 *
33
 * @author     Hans Lellelid <hans@xmpl.org> (Propel)
34
 * @author     John D. McNally <jmcnally@collab.net> (Torque)
35
 * @author     Daniel Rall <dlr@collab.net> (Torque)
36
 * @version    $Revision: 1262 $
37
 * @package    propel.map
38
 */
39
class DatabaseMap
40
{
41
 
42
  /** @var string Name of the database. */
43
  protected $name;
44
 
45
  /** @var array TableMap[] Tables in the database, using table name as key */
46
  protected $tables = array();
47
 
48
  /** @var array TableMap[] Tables in the database, using table phpName as key */
49
  protected $tablesByPhpName = array();
50
 
51
  /**
52
   * Constructor.
53
   *
54
   * @param      string $name Name of the database.
55
   */
56
  public function __construct($name)
57
  {
58
    $this->name = $name;
59
  }
60
 
61
  /**
62
   * Get the name of this database.
63
   *
64
   * @return     string The name of the database.
65
   */
66
  public function getName()
67
  {
68
    return $this->name;
69
  }
70
 
71
  /**
72
   * Add a new table to the database by name.
73
   *
74
   * @param      string $tableName The name of the table.
75
   * @return     TableMap The newly created TableMap.
76
   */
77
  public function addTable($tableName)
78
  {
79
    $this->tables[$tableName] = new TableMap($tableName, $this);
80
    return $this->tables[$tableName];
81
  }
82
 
83
  /**
84
   * Add a new table object to the database.
85
   *
86
   * @param      TableMap $table The table to add
87
   */
88
  public function addTableObject(TableMap $table)
89
  {
90
    $table->setDatabaseMap($this);
91
    $this->tables[$table->getName()] = $table;
92
    $this->tablesByPhpName[$table->getPhpName()] = $table;
93
  }
94
 
95
  /**
96
   * Add a new table to the database, using the tablemap class name.
97
   *
98
   * @param      string $tableMapClass The name of the table map to add
99
   * @return     TableMap The TableMap object
100
   */
101
  public function addTableFromMapClass($tableMapClass)
102
  {
103
    $table = new $tableMapClass();
104
    if(!$this->hasTable($table->getName())) {
105
      $this->addTableObject($table);
106
      return $table;
107
    } else {
108
      return $this->getTable($table->getName());
109
    }
110
  }
111
 
112
  /**
113
   * Does this database contain this specific table?
114
   *
115
   * @param      string $name The String representation of the table.
116
   * @return     boolean True if the database contains the table.
117
   */
118
  public function hasTable($name)
119
  {
120
    if ( strpos($name, '.') > 0) {
121
      $name = substr($name, 0, strpos($name, '.'));
122
    }
123
    return isset($this->tables[$name]);
124
  }
125
 
126
  /**
127
   * Get a TableMap for the table by name.
128
   *
129
   * @param      string $name Name of the table.
130
   * @return     TableMap A TableMap
131
   * @throws     PropelException if the table is undefined
132
   */
133
  public function getTable($name)
134
  {
135
    if (!isset($this->tables[$name])) {
136
      throw new PropelException("Cannot fetch TableMap for undefined table: " . $name );
137
    }
138
    return $this->tables[$name];
139
  }
140
 
141
  /**
142
   * Get a TableMap[] of all of the tables in the database.
143
   *
144
   * @return     array A TableMap[].
145
   */
146
  public function getTables()
147
  {
148
    return $this->tables;
149
  }
150
 
151
  /**
152
   * Get a ColumnMap for the column by name.
153
   * Name must be fully qualified, e.g. book.AUTHOR_ID
154
   *
155
   * @param      $qualifiedColumnName Name of the column.
156
   * @return     ColumnMap A TableMap
157
   * @throws     PropelException if the table is undefined, or if the table is undefined
158
   */
159
  public function getColumn($qualifiedColumnName)
160
  {
161
    list($tableName, $columnName) = explode('.', $qualifiedColumnName);
162
    return $this->getTable($tableName)->getColumn($columnName, false);
163
  }
164
 
165
  // deprecated methods
166
 
167
  /**
168
   * Does this database contain this specific table?
169
   *
170
   * @deprecated Use hasTable() instead
171
   * @param      string $name The String representation of the table.
172
   * @return     boolean True if the database contains the table.
173
   */
174
  public function containsTable($name)
175
  {
176
    return $this->hasTable($name);
177
  }
178
 
179
  public function getTableByPhpName($phpName)
180
  {
181
    if (array_key_exists($phpName, $this->tablesByPhpName)) {
182
      return $this->tablesByPhpName[$phpName];
183
    } else if (class_exists($tmClass = $phpName . 'TableMap')) {
184
      $this->addTableFromMapClass($tmClass);
185
      return $this->tablesByPhpName[$phpName];
186
    } else {
187
      throw new PropelException("Cannot fetch TableMap for undefined table phpName: " . $phpName);
188
    }
189
  }
190
 
191
  /**
192
   * Convenience method to get the DBAdapter registered with Propel for this database.
193
   * @return  DBAdapter
194
   * @see     Propel::getDB(string)
195
   */
196
  public function getDBAdapter()
197
  {
198
    return Propel::getDB($this->name);
199
  }
200
}