[ Index ] |
|
Code source de Symfony 1.0.0 |
1 <?php 2 /* 3 * $Id: TableMap.php 273 2005-11-08 15:25:43Z hans $ 4 * 5 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 6 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 7 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 8 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 9 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 10 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 11 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 12 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 13 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 14 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 15 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 16 * 17 * This software consists of voluntary contributions made by many individuals 18 * and is licensed under the LGPL. For more information please see 19 * <http://propel.phpdb.org>. 20 */ 21 22 include_once 'propel/map/ColumnMap.php'; 23 include_once 'propel/map/ValidatorMap.php'; 24 25 /** 26 * TableMap is used to model a table in a database. 27 * 28 * GENERAL NOTE 29 * ------------ 30 * The propel.map classes are abstract building-block classes for modeling 31 * the database at runtime. These classes are similar (a lite version) to the 32 * propel.engine.database.model classes, which are build-time modeling classes. 33 * These classes in themselves do not do any database metadata lookups, but instead 34 * are used by the MapBuilder classes that were generated for your datamodel. The 35 * MapBuilder that was created for your datamodel build a representation of your 36 * database by creating instances of the DatabaseMap, TableMap, ColumnMap, etc. 37 * classes. See propel/templates/om/php5/MapBuilder.tpl and the classes generated 38 * by that template for your datamodel to further understand how these are put 39 * together. 40 * 41 * @author Hans Lellelid <hans@xmpl.org> (Propel) 42 * @author John D. McNally <jmcnally@collab.net> (Torque) 43 * @author Daniel Rall <dlr@finemaltcoding.com> (Torque) 44 * @version $Revision: 273 $ 45 * @package propel.map 46 */ 47 class TableMap { 48 49 /** The columns in the table. */ 50 private $columns; 51 52 /** The database this table belongs to. */ 53 private $dbMap; 54 55 /** The name of the table. */ 56 private $tableName; 57 58 /** The PHP name of the table. */ 59 private $phpName; 60 61 /** The prefix on the table name. */ 62 private $prefix; 63 64 /** Whether to use an id generator for pkey. */ 65 private $useIdGenerator; 66 67 /** 68 * Object to store information that is needed if the 69 * for generating primary keys. 70 */ 71 private $pkInfo; 72 73 /** 74 * Construct a new TableMap. 75 * 76 * @param string $tableName The name of the table. 77 * @param DatabaseMap $containingDB A DatabaseMap that this table belongs to. 78 */ 79 public function __construct($tableName, DatabaseMap $containingDB) 80 { 81 $this->tableName = $tableName; 82 $this->dbMap = $containingDB; 83 $this->columns = array(); 84 } 85 86 /** 87 * Normalizes the column name, removing table prefix and uppercasing. 88 * @param string $name 89 * @return string Normalized column name. 90 */ 91 private function normalizeColName($name) { 92 if (false !== ($pos = strpos($name, '.'))) { 93 $name = substr($name, $pos + 1); 94 } 95 $name = strtoupper($name); 96 return $name; 97 } 98 99 /** 100 * Does this table contain the specified column? 101 * 102 * @param string $name name of the column 103 * @return boolean True if the table contains the column. 104 */ 105 public function containsColumn($name) 106 { 107 if (!is_string($name)) { 108 $name = $name->getColumnName(); 109 } 110 return isset($this->columns[$this->normalizeColName($name)]); 111 } 112 113 /** 114 * Get the DatabaseMap containing this TableMap. 115 * 116 * @return DatabaseMap A DatabaseMap. 117 */ 118 public function getDatabaseMap() 119 { 120 return $this->dbMap; 121 } 122 123 /** 124 * Get the name of the Table. 125 * 126 * @return string A String with the name of the table. 127 */ 128 public function getName() 129 { 130 return $this->tableName; 131 } 132 133 /** 134 * Get the PHP name of the Table. 135 * 136 * @return string A String with the name of the table. 137 */ 138 public function getPhpName() 139 { 140 return $this->phpName; 141 } 142 143 /** 144 * Set the PHP name of the Table. 145 * 146 * @param string $phpName The PHP Name for this table 147 */ 148 public function setPhpName($phpName) 149 { 150 $this->phpName = $phpName; 151 } 152 153 /** 154 * Get table prefix name. 155 * 156 * @return string A String with the prefix. 157 */ 158 public function getPrefix() 159 { 160 return $this->prefix; 161 } 162 163 /** 164 * Set table prefix name. 165 * 166 * @param string $prefix The prefix for the table name (ie: SCARAB for 167 * SCARAB_PROJECT). 168 * @return void 169 */ 170 public function setPrefix($prefix) 171 { 172 $this->prefix = $prefix; 173 } 174 175 /** 176 * Whether to use Id generator for primary key. 177 * @return boolean 178 */ 179 public function isUseIdGenerator() { 180 return $this->useIdGenerator; 181 } 182 183 /** 184 * Get the information used to generate a primary key 185 * 186 * @return An Object. 187 */ 188 public function getPrimaryKeyMethodInfo() 189 { 190 return $this->pkInfo; 191 } 192 193 /** 194 * Get a ColumnMap[] of the columns in this table. 195 * 196 * @return array A ColumnMap[]. 197 */ 198 public function getColumns() 199 { 200 return $this->columns; 201 } 202 203 /** 204 * Get a ColumnMap for the named table. 205 * 206 * @param string $name A String with the name of the table. 207 * @return ColumnMap A ColumnMap. 208 * @throws PropelException if the column is undefined 209 */ 210 public function getColumn($name) 211 { 212 $name = $this->normalizeColName($name); 213 if (!isset($this->columns[$name])) { 214 throw new PropelException("Cannot fetch ColumnMap for undefined column: " . $name); 215 } 216 return $this->columns[$name]; 217 } 218 219 /** 220 * Add a primary key column to this Table. 221 * 222 * @param string $columnName A String with the column name. 223 * @param string $type A string specifying the PHP native type. 224 * @param int $creoleType The integer representing the Creole type. 225 * @param boolean $isNotNull Whether column does not allow NULL values. 226 * @param $size An int specifying the size. 227 * @return ColumnMap Newly added PrimaryKey column. 228 */ 229 public function addPrimaryKey($columnName, $phpName, $type, $creoleType, $isNotNull = false, $size = null) 230 { 231 return $this->addColumn($columnName, $phpName, $type, $creoleType, $isNotNull, $size, true, null, null); 232 } 233 234 /** 235 * Add a foreign key column to the table. 236 * 237 * @param string $columnName A String with the column name. 238 * @param string $type A string specifying the PHP native type. 239 * @param int $creoleType The integer representing the Creole type. 240 * @param string $fkTable A String with the foreign key table name. 241 * @param string $fkColumn A String with the foreign key column name. 242 * @param boolean $isNotNull Whether column does not allow NULL values. 243 * @param int $size An int specifying the size. 244 * @param string $defaultValue The default value for this column. 245 * @return ColumnMap Newly added ForeignKey column. 246 */ 247 public function addForeignKey($columnName, $phpName, $type, $creoleType, $fkTable, $fkColumn, $isNotNull = false, $size = 0) 248 { 249 return $this->addColumn($columnName, $phpName, $type, $creoleType, $isNotNull, $size, false, $fkTable, $fkColumn); 250 } 251 252 /** 253 * Add a foreign primary key column to the table. 254 * 255 * @param string $columnName A String with the column name. 256 * @param string $type A string specifying the PHP native type. 257 * @param int $creoleType The integer representing the Creole type. 258 * @param string $fkTable A String with the foreign key table name. 259 * @param string $fkColumn A String with the foreign key column name. 260 * @param boolean $isNotNull Whether column does not allow NULL values. 261 * @param int $size An int specifying the size. 262 * @param string $defaultValue The default value for this column. 263 * @return ColumnMap Newly created foreign pkey column. 264 */ 265 public function addForeignPrimaryKey($columnName, $phpName, $type, $creoleType, $fkTable, $fkColumn, $isNotNull = false, $size = 0) 266 { 267 return $this->addColumn($columnName, $phpName, $type, $creoleType, $isNotNull, $size, true, $fkTable, $fkColumn); 268 } 269 270 /** 271 * Add a pre-created column to this table. It will replace any 272 * existing column. 273 * 274 * @param ColumnMap $cmap A ColumnMap. 275 * @return ColumnMap The added column map. 276 */ 277 public function addConfiguredColumn($cmap) 278 { 279 $this->columns[ $cmap->getColumnName() ] = $cmap; 280 return $cmap; 281 } 282 283 /** 284 * Add a column to the table. 285 * 286 * @param string name A String with the column name. 287 * @param string $type A string specifying the PHP native type. 288 * @param int $creoleType The integer representing the Creole type. 289 * @param boolean $isNotNull Whether column does not allow NULL values. 290 * @param int $size An int specifying the size. 291 * @param boolean $pk True if column is a primary key. 292 * @param string $fkTable A String with the foreign key table name. 293 * @param $fkColumn A String with the foreign key column name. 294 * @param string $defaultValue The default value for this column. 295 * @return ColumnMap The newly created column. 296 */ 297 public function addColumn($name, $phpName, $type, $creoleType, $isNotNull = false, $size = null, $pk = null, $fkTable = null, $fkColumn = null) 298 { 299 300 $col = new ColumnMap($name, $this); 301 302 if ($fkTable && $fkColumn) { 303 if (substr($fkColumn, '.') > 0 && substr($fkColumn, $fkTable) !== false) { 304 $fkColumn = substr($fkColumn, strlen($fkTable) + 1); 305 } 306 $col->setForeignKey($fkTable, $fkColumn); 307 } 308 309 $col->setType($type); 310 $col->setCreoleType($creoleType); 311 $col->setPrimaryKey($pk); 312 $col->setSize($size); 313 $col->setPhpName($phpName); 314 $col->setNotNull($isNotNull); 315 316 $this->columns[$name] = $col; 317 318 return $this->columns[$name]; 319 } 320 321 /** 322 * Add a validator to a table's column 323 * 324 * @param string $columnName The name of the validator's column 325 * @param string $name The rule name of this validator 326 * @param string $classname The dot-path name of class to use (e.g. myapp.propel.MyValidator) 327 * @param string $value 328 * @param string $message The error message which is returned on invalid values 329 * @return void 330 */ 331 public function addValidator($columnName, $name, $classname, $value, $message) 332 { 333 if (false !== ($pos = strpos($columnName, '.'))) { 334 $columnName = substr($columnName, $pos + 1); 335 } 336 337 $col = $this->getColumn($columnName); 338 if ($col !== null) { 339 $validator = new ValidatorMap($col); 340 $validator->setName($name); 341 $validator->setClass($classname); 342 $validator->setValue($value); 343 $validator->setMessage($message); 344 $col->addValidator($validator); 345 } 346 } 347 348 /** 349 * Set whether or not to use Id generator for primary key. 350 * @param boolean $bit 351 */ 352 public function setUseIdGenerator($bit) { 353 $this->useIdGenerator = $bit; 354 } 355 356 /** 357 * Sets the pk information needed to generate a key 358 * 359 * @param $pkInfo information needed to generate a key 360 */ 361 public function setPrimaryKeyMethodInfo($pkInfo) 362 { 363 $this->pkInfo = $pkInfo; 364 } 365 366 //---Utility methods for doing intelligent lookup of table names 367 368 /** 369 * Tell me if i have PREFIX in my string. 370 * 371 * @param data A String. 372 * @return boolean True if prefix is contained in data. 373 */ 374 private function hasPrefix($data) 375 { 376 return (substr($data, $this->getPrefix()) !== false); 377 } 378 379 /** 380 * Removes the PREFIX. 381 * 382 * @param string $data A String. 383 * @return string A String with data, but with prefix removed. 384 */ 385 private function removePrefix($data) 386 { 387 return substr($data, strlen($this->getPrefix())); 388 } 389 390 391 392 /** 393 * Removes the PREFIX, removes the underscores and makes 394 * first letter caps. 395 * 396 * SCARAB_FOO_BAR becomes FooBar. 397 * 398 * @param data A String. 399 * @return string A String with data processed. 400 */ 401 public final function removeUnderScores($data) 402 { 403 $tmp = null; 404 $out = ""; 405 if ($this->hasPrefix($data)) { 406 $tmp = $this->removePrefix($data); 407 } else { 408 $tmp = $data; 409 } 410 411 $tok = strtok($tmp, "_"); 412 while ($tok) { 413 $out .= ucfirst($tok); 414 $tok = strtok("_"); 415 } 416 return $out; 417 } 418 419 /** 420 * Makes the first letter caps and the rest lowercase. 421 * 422 * @param string $data A String. 423 * @return string A String with data processed. 424 */ 425 private function firstLetterCaps($data) 426 { 427 return(ucfirst(strtolower($data))); 428 } 429 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Mar 16 22:42:14 2007 | par Balluche grâce à PHPXref 0.7 |