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: ColumnMap.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
 * ColumnMap is used to model a column of a table in 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
 * @version    $Revision: 1262 $
36
 * @package    propel.map
37
 */
38
class ColumnMap {
39
 
40
  // Propel type of the column
41
  protected $type;
42
 
43
  // Size of the column
44
  protected $size = 0;
45
 
46
  // Is it a primary key?
47
  protected $pk = false;
48
 
49
  // Is null value allowed?
50
  protected $notNull = false;
51
 
52
  // The default value for this column
53
  protected $defaultValue;
54
 
55
  // Name of the table that this column is related to
56
  protected $relatedTableName = "";
57
 
58
  // Name of the column that this column is related to
59
  protected $relatedColumnName = "";
60
 
61
  // The TableMap for this column
62
  protected $table;
63
 
64
  // The name of the column
65
  protected $columnName;
66
 
67
  // The php name of the column
68
  protected $phpName;
69
 
70
  // The validators for this column
71
  protected $validators = array();
72
 
73
  /**
74
   * Constructor.
75
   *
76
   * @param      string $name The name of the column.
77
   * @param      TableMap containingTable TableMap of the table this column is in.
78
   */
79
  public function __construct($name, TableMap $containingTable)
80
  {
81
    $this->columnName = $name;
82
    $this->table = $containingTable;
83
  }
84
 
85
  /**
86
   * Get the name of a column.
87
   *
88
   * @return     string A String with the column name.
89
   */
90
  public function getName()
91
  {
92
    return $this->columnName;
93
  }
94
 
95
  /**
96
   * Get the table map this column belongs to.
97
   * @return     TableMap
98
   */
99
  public function getTable()
100
  {
101
    return $this->table;
102
  }
103
 
104
  /**
105
   * Get the name of the table this column is in.
106
   *
107
   * @return     string A String with the table name.
108
   */
109
  public function getTableName()
110
  {
111
    return $this->table->getName();
112
  }
113
 
114
  /**
115
   * Get the table name + column name.
116
   *
117
   * @return     string A String with the full column name.
118
   */
119
  public function getFullyQualifiedName()
120
  {
121
    return $this->getTableName() . "." . $this->columnName;
122
  }
123
 
124
  /**
125
   * Set the php anme of this column.
126
   *
127
   * @param      string $phpName A string representing the PHP name.
128
   * @return     void
129
   */
130
  public function setPhpName($phpName)
131
  {
132
    $this->phpName = $phpName;
133
  }
134
 
135
  /**
136
   * Get the name of a column.
137
   *
138
   * @return     string A String with the column name.
139
   */
140
  public function getPhpName()
141
  {
142
    return $this->phpName;
143
  }
144
 
145
  /**
146
   * Set the Propel type of this column.
147
   *
148
   * @param      string $type A string representing the Propel type (e.g. PropelColumnTypes::DATE).
149
   * @return     void
150
   */
151
  public function setType($type)
152
  {
153
    $this->type = $type;
154
  }
155
 
156
  /**
157
   * Get the Propel type of this column.
158
   *
159
   * @return     string A string representing the Propel type (e.g. PropelColumnTypes::DATE).
160
   */
161
  public function getType()
162
  {
163
    return $this->type;
164
  }
165
 
166
  /**
167
   * Get the PDO type of this column.
168
   *
169
   * @return     int The PDO::PARMA_* value
170
   */
171
  public function getPdoType()
172
  {
173
    return PropelColumnTypes::getPdoType($this->type);
174
  }
175
 
176
  /**
177
   * Whether this is a BLOB, LONGVARBINARY, or VARBINARY.
178
   * @return     boolean
179
   */
180
  public function isLob()
181
  {
182
    return ($this->type == PropelColumnTypes::BLOB || $this->type == PropelColumnTypes::VARBINARY || $this->type == PropelColumnTypes::LONGVARBINARY);
183
  }
184
 
185
  /**
186
   * Whether this is a DATE/TIME/TIMESTAMP column.
187
   *
188
   * @return     boolean
189
   * @since      1.3
190
   */
191
  public function isTemporal()
192
  {
193
    return ($this->type == PropelColumnTypes::TIMESTAMP || $this->type == PropelColumnTypes::DATE || $this->type == PropelColumnTypes::TIME || $this->type == PropelColumnTypes::BU_DATE  || $this->type == PropelColumnTypes::BU_TIMESTAMP);
194
  }
195
 
196
  /**
197
   * Whether this is a DATE/TIME/TIMESTAMP column that is post-epoch (1970).
198
   *
199
   * PHP cannot handle pre-epoch timestamps well -- hence the need to differentiate
200
   * between epoch and pre-epoch timestamps.
201
   *
202
   * @return     boolean
203
   * @deprecated Propel supports non-epoch dates
204
   */
205
  public function isEpochTemporal()
206
  {
207
    return ($this->type == PropelColumnTypes::TIMESTAMP || $this->type == PropelColumnTypes::DATE || $this->type == PropelColumnTypes::TIME);
208
  }
209
 
210
  /**
211
   * Whether this column is numeric (int, decimal, bigint etc).
212
   * @return     boolean
213
   */
214
  public function isNumeric()
215
  {
216
    return ($this->type == PropelColumnTypes::NUMERIC || $this->type == PropelColumnTypes::DECIMAL || $this->type == PropelColumnTypes::TINYINT || $this->type == PropelColumnTypes::SMALLINT || $this->type == PropelColumnTypes::INTEGER || $this->type == PropelColumnTypes::BIGINT || $this->type == PropelColumnTypes::REAL || $this->type == PropelColumnTypes::FLOAT || $this->type == PropelColumnTypes::DOUBLE);
217
  }
218
 
219
  /**
220
   * Whether this column is a text column (varchar, char, longvarchar).
221
   * @return     boolean
222
   */
223
  public function isText()
224
  {
225
    return ($this->type == PropelColumnTypes::VARCHAR || $this->type == PropelColumnTypes::LONGVARCHAR || $this->type == PropelColumnTypes::CHAR);
226
  }
227
 
228
  /**
229
   * Set the size of this column.
230
   *
231
   * @param      int $size An int specifying the size.
232
   * @return     void
233
   */
234
  public function setSize($size)
235
  {
236
    $this->size = $size;
237
  }
238
 
239
  /**
240
   * Get the size of this column.
241
   *
242
   * @return     int An int specifying the size.
243
   */
244
  public function getSize()
245
  {
246
    return $this->size;
247
  }
248
 
249
  /**
250
   * Set if this column is a primary key or not.
251
   *
252
   * @param      boolean $pk True if column is a primary key.
253
   * @return     void
254
   */
255
  public function setPrimaryKey($pk)
256
  {
257
    $this->pk = $pk;
258
  }
259
 
260
  /**
261
   * Is this column a primary key?
262
   *
263
   * @return     boolean True if column is a primary key.
264
   */
265
  public function isPrimaryKey()
266
  {
267
    return $this->pk;
268
  }
269
 
270
  /**
271
   * Set if this column may be null.
272
   *
273
   * @param      boolean nn True if column may be null.
274
   * @return     void
275
   */
276
  public function setNotNull($nn)
277
  {
278
    $this->notNull = $nn;
279
  }
280
 
281
  /**
282
   * Is null value allowed ?
283
   *
284
   * @return     boolean True if column may not be null.
285
   */
286
  public function isNotNull()
287
  {
288
    return ($this->notNull || $this->isPrimaryKey());
289
  }
290
 
291
  /**
292
   * Sets the default value for this column.
293
   * @param      mixed $defaultValue the default value for the column
294
   * @return     void
295
   */
296
  public function setDefaultValue($defaultValue)
297
  {
298
    $this->defaultValue = $defaultValue;
299
  }
300
 
301
  /**
302
   * Gets the default value for this column.
303
   * @return     mixed String or NULL
304
   */
305
  public function getDefaultValue()
306
  {
307
    return $this->defaultValue;
308
  }
309
 
310
  /**
311
   * Set the foreign key for this column.
312
   *
313
   * @param      string tableName The name of the table that is foreign.
314
   * @param      string columnName The name of the column that is foreign.
315
   * @return     void
316
   */
317
  public function setForeignKey($tableName, $columnName)
318
  {
319
    if ($tableName && $columnName) {
320
      $this->relatedTableName = $tableName;
321
      $this->relatedColumnName = $columnName;
322
    } else {
323
      $this->relatedTableName = "";
324
      $this->relatedColumnName = "";
325
    }
326
  }
327
 
328
  /**
329
   * Is this column a foreign key?
330
   *
331
   * @return     boolean True if column is a foreign key.
332
   */
333
  public function isForeignKey()
334
  {
335
    if ($this->relatedTableName) {
336
      return true;
337
    } else {
338
      return false;
339
    }
340
  }
341
 
342
  /**
343
   * Get the RelationMap object for this foreign key
344
   */
345
  public function getRelation()
346
  {
347
    if(!$this->relatedTableName) return null;
348
    foreach ($this->getTable()->getRelations() as $name => $relation)
349
    {
350
      if($relation->getType() == RelationMap::MANY_TO_ONE)
351
      {
352
        if ($relation->getForeignTable()->getName() == $this->getRelatedTableName()
353
         && array_key_exists($this->getFullyQualifiedName(), $relation->getColumnMappings()))
354
        {
355
          return $relation;
356
        }
357
      }
358
    }
359
  }
360
 
361
  /**
362
   * Get the table.column that this column is related to.
363
   *
364
   * @return     string A String with the full name for the related column.
365
   */
366
  public function getRelatedName()
367
  {
368
    return $this->relatedTableName . "." . $this->relatedColumnName;
369
  }
370
 
371
  /**
372
   * Get the table name that this column is related to.
373
   *
374
   * @return     string A String with the name for the related table.
375
   */
376
  public function getRelatedTableName()
377
  {
378
    return $this->relatedTableName;
379
  }
380
 
381
  /**
382
   * Get the column name that this column is related to.
383
   *
384
   * @return     string A String with the name for the related column.
385
   */
386
  public function getRelatedColumnName()
387
  {
388
    return $this->relatedColumnName;
389
  }
390
 
391
  /**
392
   * Get the TableMap object that this column is related to.
393
   *
394
   * @return     TableMap The related TableMap object
395
   * @throws     PropelException when called on a column with no foreign key
396
   */
397
  public function getRelatedTable()
398
  {
399
    if ($this->relatedTableName) {
400
      return $this->table->getDatabaseMap()->getTable($this->relatedTableName);
401
    } else {
402
      throw new PropelException("Cannot fetch RelatedTable for column with no foreign key: " . $this->columnName);
403
    }
404
  }
405
 
406
  /**
407
   * Get the TableMap object that this column is related to.
408
   *
409
   * @return     ColumnMap The related ColumnMap object
410
   * @throws     PropelException when called on a column with no foreign key
411
   */
412
  public function getRelatedColumn()
413
  {
414
    return $this->getRelatedTable()->getColumn($this->relatedColumnName);
415
  }
416
 
417
  public function addValidator($validator)
418
  {
419
    $this->validators[] = $validator;
420
  }
421
 
422
  public function hasValidators()
423
  {
424
    return count($this->validators) > 0;
425
  }
426
 
427
  public function getValidators()
428
  {
429
    return $this->validators;
430
  }
431
 
432
  /**
433
   * Performs DB-specific ignore case, but only if the column type necessitates it.
434
   * @param      string $str The expression we want to apply the ignore case formatting to (e.g. the column name).
435
   * @param      DBAdapter $db
436
   */
437
  public function ignoreCase($str, DBAdapter $db)
438
  {
439
    if ($this->isText()) {
440
      return $db->ignoreCase($str);
441
    } else {
442
      return $str;
443
    }
444
  }
445
 
446
  /**
447
   * Normalizes the column name, removing table prefix and uppercasing.
448
   *
449
   * article.first_name becomes FIRST_NAME
450
   *
451
   * @param      string $name
452
   * @return     string Normalized column name.
453
   */
454
  public static function normalizeName($name)
455
  {
456
    if (false !== ($pos = strpos($name, '.'))) {
457
      $name = substr($name, $pos + 1);
458
    }
459
    $name = strtoupper($name);
460
    return $name;
461
  }
462
 
463
  // deprecated methods
464
 
465
  /**
466
   * Gets column name
467
   * @deprecated Use getName() instead
468
   * @return     string
469
   * @deprecated Use getName() instead.
470
   */
471
  public function getColumnName()
472
  {
473
    return $this->getName();
474
  }
475
}