Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/*
3
 *  $Id: TableMap.php 1262 2009-10-26 20:54:39Z francois $
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
/**
23
 * TableMap is used to model a table in a database.
24
 *
25
 * GENERAL NOTE
26
 * ------------
27
 * The propel.map classes are abstract building-block classes for modeling
28
 * the database at runtime.  These classes are similar (a lite version) to the
29
 * propel.engine.database.model classes, which are build-time modeling classes.
30
 * These classes in themselves do not do any database metadata lookups.
31
 *
32
 * @author     Hans Lellelid <hans@xmpl.org> (Propel)
33
 * @author     John D. McNally <jmcnally@collab.net> (Torque)
34
 * @author     Daniel Rall <dlr@finemaltcoding.com> (Torque)
35
 * @version    $Revision: 1262 $
36
 * @package    propel.map
37
 */
38
class TableMap {
39
 
40
  // The columns in the table
41
  protected $columns = array();
42
 
43
  // The database this table belongs to
44
  protected $dbMap;
45
 
46
  // The name of the table
47
  protected $tableName;
48
 
49
  // The PHP name of the table
50
  protected $phpName;
51
 
52
  // The Classname for this table
53
  protected $classname;
54
 
55
  // The Package for this table
56
  protected $package;
57
 
58
  // Whether to use an id generator for pkey
59
  protected $useIdGenerator;
60
 
61
  // The primary key columns in the table
62
  protected $primaryKeys = array();
63
 
64
  // The foreign key columns in the table
65
  protected $foreignKeys = array();
66
 
67
  // The relationships in the table
68
  protected $relations = array();
69
 
70
  // Relations are lazy loaded. This property tells if the relations are loaded or not
71
  protected $relationsBuilt = false;
72
 
73
  // Object to store information that is needed if the for generating primary keys
74
  protected $pkInfo;
75
 
76
  /**
77
   * Construct a new TableMap.
78
   *
79
   */
80
  public function __construct($name = null, $dbMap = null)
81
  {
82
    if(!is_null($name)) $this->setName($name);
83
    if(!is_null($dbMap)) $this->setDatabaseMap($dbMap);
84
    $this->initialize();
85
  }
86
 
87
  /**
88
   * Initialize the TableMap to build columns, relations, etc
89
   * This method should be overridden by descendents
90
   */
91
  public function initialize()
92
  {
93
  }
94
 
95
  /**
96
   * Set the DatabaseMap containing this TableMap.
97
   *
98
   * @param     DatabaseMap $dbMap A DatabaseMap.
99
   */
100
  public function setDatabaseMap(DatabaseMap $dbMap)
101
  {
102
    $this->dbMap = $dbMap;
103
  }
104
 
105
  /**
106
   * Get the DatabaseMap containing this TableMap.
107
   *
108
   * @return     DatabaseMap A DatabaseMap.
109
   */
110
  public function getDatabaseMap()
111
  {
112
    return $this->dbMap;
113
  }
114
 
115
  /**
116
   * Set the name of the Table.
117
   *
118
   * @param      string $name The name of the table.
119
   */
120
  public function setName($name)
121
  {
122
    $this->tableName = $name;
123
  }
124
 
125
  /**
126
   * Get the name of the Table.
127
   *
128
   * @return     string A String with the name of the table.
129
   */
130
  public function getName()
131
  {
132
    return $this->tableName;
133
  }
134
 
135
  /**
136
   * Set the PHP name of the Table.
137
   *
138
   * @param      string $phpName The PHP Name for this table
139
   */
140
  public function setPhpName($phpName)
141
  {
142
    $this->phpName = $phpName;
143
  }
144
 
145
  /**
146
   * Get the PHP name of the Table.
147
   *
148
   * @return     string A String with the name of the table.
149
   */
150
  public function getPhpName()
151
  {
152
    return $this->phpName;
153
  }
154
 
155
  /**
156
   * Set the Classname of the Table. Could be useful for calling
157
   * Peer and Object methods dynamically.
158
   * @param      string $classname The Classname
159
   */
160
  public function setClassname($classname)
161
  {
162
    $this->classname = $classname;
163
  }
164
 
165
  /**
166
   * Get the Classname of the Propel-Classes belonging to this table.
167
   * @return     string
168
   */
169
  public function getClassname()
170
  {
171
    return $this->classname;
172
  }
173
 
174
  /**
175
   * Set the Package of the Table
176
   *
177
   * @param      string $package The Package
178
   */
179
  public function setPackage($package)
180
  {
181
    $this->package = $package;
182
  }
183
 
184
  /**
185
   * Get the Package of the table.
186
   * @return     string
187
   */
188
  public function getPackage()
189
  {
190
    return $this->package;
191
  }
192
 
193
  /**
194
   * Set whether or not to use Id generator for primary key.
195
   * @param      boolean $bit
196
   */
197
  public function setUseIdGenerator($bit)
198
  {
199
    $this->useIdGenerator = $bit;
200
  }
201
 
202
  /**
203
   * Whether to use Id generator for primary key.
204
   * @return     boolean
205
   */
206
  public function isUseIdGenerator()
207
  {
208
    return $this->useIdGenerator;
209
  }
210
 
211
  /**
212
   * Sets the pk information needed to generate a key
213
   *
214
   * @param      $pkInfo information needed to generate a key
215
   */
216
  public function setPrimaryKeyMethodInfo($pkInfo)
217
  {
218
    $this->pkInfo = $pkInfo;
219
  }
220
 
221
  /**
222
   * Get the information used to generate a primary key
223
   *
224
   * @return     An Object.
225
   */
226
  public function getPrimaryKeyMethodInfo()
227
  {
228
    return $this->pkInfo;
229
  }
230
 
231
  /**
232
   * Add a column to the table.
233
   *
234
   * @param      string name A String with the column name.
235
   * @param      string $type A string specifying the Propel type.
236
   * @param      boolean $isNotNull Whether column does not allow NULL values.
237
   * @param      int $size An int specifying the size.
238
   * @param      boolean $pk True if column is a primary key.
239
   * @param      string $fkTable A String with the foreign key table name.
240
   * @param      $fkColumn A String with the foreign key column name.
241
   * @param      string $defaultValue The default value for this column.
242
   * @return     ColumnMap The newly created column.
243
   */
244
  public function addColumn($name, $phpName, $type, $isNotNull = false, $size = null, $defaultValue = null, $pk = false, $fkTable = null, $fkColumn = null)
245
  {
246
 
247
    $col = new ColumnMap($name, $this);
248
 
249
    if ($fkTable && $fkColumn) {
250
      if (strpos($fkColumn, '.') > 0 && strpos($fkColumn, $fkTable) !== false) {
251
        $fkColumn = substr($fkColumn, strlen($fkTable) + 1);
252
      }
253
      $col->setForeignKey($fkTable, $fkColumn);
254
      $this->foreignKeys[$name] = $col;
255
    }
256
 
257
    $col->setType($type);
258
    $col->setSize($size);
259
    $col->setPhpName($phpName);
260
    $col->setNotNull($isNotNull);
261
    $col->setDefaultValue($defaultValue);
262
    if ($pk) {
263
      $col->setPrimaryKey(true);
264
      $this->primaryKeys[$name] = $col;
265
    }
266
    $this->columns[$name] = $col;
267
 
268
    return $this->columns[$name];
269
  }
270
 
271
  /**
272
   * Add a pre-created column to this table. It will replace any
273
   * existing column.
274
   *
275
   * @param      ColumnMap $cmap A ColumnMap.
276
   * @return     ColumnMap The added column map.
277
   */
278
  public function addConfiguredColumn($cmap)
279
  {
280
    $this->columns[ $cmap->getColumnName() ] = $cmap;
281
    return $cmap;
282
  }
283
 
284
  /**
285
   * Does this table contain the specified column?
286
   *
287
   * @param      mixed   $name name of the column or ColumnMap instance
288
   * @param      boolean $normalize Normalize the column name (if column name not like FIRST_NAME)
289
   * @return     boolean True if the table contains the column.
290
   */
291
  public function hasColumn($name, $normalize = true)
292
  {
293
    if ($name instanceof ColumnMap) {
294
      $name = $name->getColumnName();
295
    } else if($normalize) {
296
      $name = ColumnMap::normalizeName($name);
297
    }
298
    return isset($this->columns[$name]);
299
  }
300
 
301
  /**
302
   * Get a ColumnMap for the named table.
303
   *
304
   * @param      string    $name A String with the name of the table.
305
   * @param      boolean   $normalize Normalize the column name (if column name not like FIRST_NAME)
306
   * @return     ColumnMap A ColumnMap.
307
   * @throws     PropelException if the column is undefined
308
   */
309
  public function getColumn($name, $normalize = true)
310
  {
311
    if ($normalize) {
312
      $name = ColumnMap::normalizeName($name);
313
    }
314
    if (!$this->containsColumn($name, false)) {
315
      throw new PropelException("Cannot fetch ColumnMap for undefined column: " . $name);
316
    }
317
    return $this->columns[$name];
318
  }
319
 
320
  /**
321
   * Get a ColumnMap[] of the columns in this table.
322
   *
323
   * @return     array A ColumnMap[].
324
   */
325
  public function getColumns()
326
  {
327
    return $this->columns;
328
  }
329
 
330
  /**
331
   * Add a primary key column to this Table.
332
   *
333
   * @param      string $columnName A String with the column name.
334
   * @param      string $type A string specifying the Propel type.
335
   * @param      boolean $isNotNull Whether column does not allow NULL values.
336
   * @param      $size An int specifying the size.
337
   * @return     ColumnMap Newly added PrimaryKey column.
338
   */
339
  public function addPrimaryKey($columnName, $phpName, $type, $isNotNull = false, $size = null, $defaultValue = null)
340
  {
341
    return $this->addColumn($columnName, $phpName, $type, $isNotNull, $size, $defaultValue, true, null, null);
342
  }
343
 
344
  /**
345
   * Add a foreign key column to the table.
346
   *
347
   * @param      string $columnName A String with the column name.
348
   * @param      string $type A string specifying the Propel type.
349
   * @param      string $fkTable A String with the foreign key table name.
350
   * @param      string $fkColumn A String with the foreign key column name.
351
   * @param      boolean $isNotNull Whether column does not allow NULL values.
352
   * @param      int $size An int specifying the size.
353
   * @param      string $defaultValue The default value for this column.
354
   * @return     ColumnMap Newly added ForeignKey column.
355
   */
356
  public function addForeignKey($columnName, $phpName, $type, $fkTable, $fkColumn, $isNotNull = false, $size = 0, $defaultValue = null)
357
  {
358
    return $this->addColumn($columnName, $phpName, $type, $isNotNull, $size, $defaultValue, false, $fkTable, $fkColumn);
359
  }
360
 
361
  /**
362
   * Add a foreign primary key column to the table.
363
   *
364
   * @param      string $columnName A String with the column name.
365
   * @param      string $type A string specifying the Propel type.
366
   * @param      string $fkTable A String with the foreign key table name.
367
   * @param      string $fkColumn A String with the foreign key column name.
368
   * @param      boolean $isNotNull Whether column does not allow NULL values.
369
   * @param      int $size An int specifying the size.
370
   * @param      string $defaultValue The default value for this column.
371
   * @return     ColumnMap Newly created foreign pkey column.
372
   */
373
  public function addForeignPrimaryKey($columnName, $phpName, $type, $fkTable, $fkColumn, $isNotNull = false, $size = 0, $defaultValue = null)
374
  {
375
    return $this->addColumn($columnName, $phpName, $type, $isNotNull, $size, $defaultValue, true, $fkTable, $fkColumn);
376
  }
377
 
378
  /**
379
   * Returns array of ColumnMap objects that make up the primary key for this table
380
   *
381
   * @return     array ColumnMap[]
382
   */
383
  public function getPrimaryKeys()
384
  {
385
    return $this->primaryKeys;
386
  }
387
 
388
  /**
389
   * Returns array of ColumnMap objects that are foreign keys for this table
390
   *
391
   * @return     array ColumnMap[]
392
   */
393
  public function getForeignKeys()
394
  {
395
    return $this->foreignKeys;
396
  }
397
 
398
  /**
399
  * Add a validator to a table's column
400
  *
401
  * @param      string $columnName The name of the validator's column
402
  * @param      string $name The rule name of this validator
403
  * @param      string $classname The dot-path name of class to use (e.g. myapp.propel.MyValidator)
404
  * @param      string $value
405
  * @param      string $message The error message which is returned on invalid values
406
  * @return     void
407
  */
408
  public function addValidator($columnName, $name, $classname, $value, $message)
409
  {
410
    if (false !== ($pos = strpos($columnName, '.'))) {
411
      $columnName = substr($columnName, $pos + 1);
412
    }
413
 
414
    $col = $this->getColumn($columnName);
415
    if ($col !== null) {
416
      $validator = new ValidatorMap($col);
417
      $validator->setName($name);
418
      $validator->setClass($classname);
419
      $validator->setValue($value);
420
      $validator->setMessage($message);
421
      $col->addValidator($validator);
422
    }
423
  }
424
 
425
  /**
426
   * Build relations
427
   * Relations are lazy loaded for performance reasons
428
   * This method should be overridden by descendents
429
   */
430
  public function buildRelations()
431
  {
432
  }
433
 
434
  /**
435
   * Adds a RelationMap to the table
436
   *
437
   * @param      string $name The relation name
438
   * @param      string $tablePhpName The related table name
439
   * @param      integer $type The relation type (either RelationMap::MANY_TO_ONE, RelationMap::ONE_TO_MANY, or RelationMAp::ONE_TO_ONE)
440
   * @param      array $columnMapping An associative array mapping column names (local => foreign)
441
   * @return     RelationMap the built RelationMap object
442
   */
443
  public function addRelation($name, $tablePhpName, $type, $columnMapping = array(), $onDelete = null, $onUpdate = null)
444
  {
445
    // note: using phpName for the second table allows the use of DatabaseMap::getTableByPhpName()
446
    // and this method autoloads the TableMap if the table isn't loaded yet
447
    $relation = new RelationMap($name);
448
    $relation->setType($type);
449
    $relation->setOnUpdate($onUpdate);
450
    $relation->setOnDelete($onDelete);
451
    // set tables
452
    if ($type == RelationMap::MANY_TO_ONE) {
453
      $relation->setLocalTable($this);
454
      $relation->setForeignTable($this->dbMap->getTableByPhpName($tablePhpName));
455
    } else {
456
      $relation->setLocalTable($this->dbMap->getTableByPhpName($tablePhpName));
457
      $relation->setForeignTable($this);
458
      $columnMapping  = array_flip($columnMapping);
459
    }
460
    // set columns
461
    foreach ($columnMapping as $key => $value)
462
    {
463
      $relation->addColumnMapping(
464
        $relation->getLocalTable()->getColumn($key),
465
        $relation->getForeignTable()->getColumn($value)
466
      );
467
    }
468
    $this->relations[$name] = $relation;
469
    return $relation;
470
  }
471
 
472
  /**
473
   * Gets a RelationMap of the table by relation name
474
   *
475
   * @param       String $name The relation name
476
   * @return      boolean true if the relation exists
477
   */
478
  public function hasRelation($name)
479
  {
480
    return array_key_exists($name, $this->getRelations());
481
  }
482
 
483
  /**
484
   * Gets a RelationMap of the table by relation name
485
   *
486
   * @param       String $name The relation name
487
   * @return      RelationMap The relation object
488
   * @throws      PropelException When called on an inexistent relation
489
   */
490
  public function getRelation($name)
491
  {
492
    if (!array_key_exists($name, $this->getRelations()))
493
    {
494
      throw new PropelException('Calling getRelation() on an unknown relation, ' . $name);
495
    }
496
    return $this->relations[$name];
497
  }
498
 
499
  /**
500
   * Gets the RelationMap objects of the table
501
   * This method will build the relations if they are not built yet
502
   *
503
   * @return      Array list of RelationMap objects
504
   */
505
  public function getRelations()
506
  {
507
    if(!$this->relationsBuilt)
508
    {
509
      $this->buildRelations();
510
      $this->relationsBuilt = true;
511
    }
512
    return $this->relations;
513
  }
514
 
515
  /**
516
   *
517
   * Gets the list of behaviors registered for this table
518
   *
519
   * @return array
520
   */
521
  public function getBehaviors()
522
  {
523
    return array();
524
  }
525
 
526
  // Deprecated methods and attributres, to be removed
527
 
528
  /**
529
   * Does this table contain the specified column?
530
   *
531
   * @deprecated Use hasColumn instead
532
   * @param      mixed   $name name of the column or ColumnMap instance
533
   * @param      boolean $normalize Normalize the column name (if column name not like FIRST_NAME)
534
   * @return     boolean True if the table contains the column.
535
   */
536
  public function containsColumn($name, $normalize = true)
537
  {
538
    return $this->hasColumn($name, $normalize);
539
  }
540
 
541
  /**
542
   * Normalizes the column name, removing table prefix and uppercasing.
543
   * article.first_name becomes FIRST_NAME
544
   *
545
   * @deprecated Use ColumnMap::normalizeColumName() instead
546
   * @param      string $name
547
   * @return     string Normalized column name.
548
   */
549
  protected function normalizeColName($name)
550
  {
551
    return ColumnMap::normalizeName($name);
552
  }
553
 
554
  /**
555
   * Returns array of ColumnMap objects that make up the primary key for this table.
556
   *
557
   * @deprecated Use getPrimaryKeys instead
558
   * @return     array ColumnMap[]
559
   */
560
  public function getPrimaryKeyColumns()
561
  {
562
    return array_values($this->primaryKeys);
563
  }
564
 
565
  //---Utility methods for doing intelligent lookup of table names
566
 
567
  /**
568
   * The prefix on the table name.
569
   * @deprecated Not used anywhere in Propel
570
   */
571
  private $prefix;
572
 
573
  /**
574
   * Get table prefix name.
575
   *
576
   * @deprecated Not used anywhere in Propel
577
   * @return     string A String with the prefix.
578
   */
579
  public function getPrefix()
580
  {
581
    return $this->prefix;
582
  }
583
 
584
  /**
585
   * Set table prefix name.
586
   *
587
   * @deprecated Not used anywhere in Propel
588
   * @param      string $prefix The prefix for the table name (ie: SCARAB for
589
   * SCARAB_PROJECT).
590
   * @return     void
591
   */
592
  public function setPrefix($prefix)
593
  {
594
    $this->prefix = $prefix;
595
  }
596
 
597
  /**
598
   * Tell me if i have PREFIX in my string.
599
   *
600
   * @deprecated Not used anywhere in Propel
601
   * @param      data A String.
602
   * @return     boolean True if prefix is contained in data.
603
   */
604
  protected function hasPrefix($data)
605
  {
606
    return (strpos($data, $this->prefix) === 0);
607
  }
608
 
609
  /**
610
   * Removes the PREFIX if found
611
   *
612
   * @deprecated Not used anywhere in Propel
613
   * @param      string $data A String.
614
   * @return     string A String with data, but with prefix removed.
615
   */
616
  protected function removePrefix($data)
617
  {
618
    return $this->hasPrefix($data) ? substr($data, strlen($this->prefix)) : $data;
619
  }
620
 
621
  /**
622
   * Removes the PREFIX, removes the underscores and makes
623
   * first letter caps.
624
   *
625
   * SCARAB_FOO_BAR becomes FooBar.
626
   *
627
   * @deprecated Not used anywhere in Propel. At buildtime, use Column::generatePhpName() for that purpose
628
   * @param      data A String.
629
   * @return     string A String with data processed.
630
   */
631
  public final function removeUnderScores($data)
632
  {
633
    $out = '';
634
    $tmp = $this->removePrefix($data);
635
    $tok = strtok($tmp, '_');
636
    while ($tok) {
637
      $out .= ucfirst($tok);
638
      $tok = strtok('_');
639
    }
640
    return $out;
641
  }
642
 
643
  /**
644
   * Makes the first letter caps and the rest lowercase.
645
   *
646
   * @deprecated Not used anywhere in Propel.
647
   * @param      string $data A String.
648
   * @return     string A String with data processed.
649
   */
650
  private function firstLetterCaps($data)
651
  {
652
    return(ucfirst(strtolower($data)));
653
  }
654
}