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
 * This file is part of the symfony package.
5
 * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
6
 * (c) Jonathan H. Wage <jonwage@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
/**
13
 * Represents a Doctrine column
14
 *
15
 * @package    symfony
16
 * @subpackage doctrine
17
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
18
 * @author     Jonathan H. Wage <jonwage@gmail.com>
19
 * @version    SVN: $Id: sfDoctrineColumn.class.php 24604 2009-11-30 21:00:46Z Jonathan.Wage $
20
 */
21
class sfDoctrineColumn implements ArrayAccess
22
{
23
  /**
24
   * Array mapping Doctrine column types to the native symfony type
25
   */
26
  static $doctrineToSymfony = array(
27
    'boolean'   => 'BOOLEAN',
28
    'string'    => 'LONGVARCHAR',
29
    'integer'   => 'INTEGER',
30
    'date'      => 'DATE',
31
    'timestamp' => 'TIMESTAMP',
32
    'time'      => 'TIME',
33
    'enum'      => 'LONGVARCHAR',
34
    'float'     => 'FLOAT',
35
    'double'    => 'DOUBLE',
36
    'clob'      => 'CLOB',
37
    'blob'      => 'BLOB',
38
    'object'    => 'LONGVARCHAR',
39
    'array'     => 'LONGVARCHAR',
40
    'decimal'   => 'DECIMAL',
41
  );
42
 
43
  /**
44
   * Store the name of the related class for this column if it is
45
   * a foreign key
46
   *
47
   * @var string
48
   */
49
  protected $foreignClassName = null;
50
 
51
  /**
52
   * Doctrine_Table instance this column belongs to
53
   *
54
   * @var Doctrine_Table $table
55
   */
56
  protected $table = null;
57
 
58
  /**
59
   * Field name of the column
60
   *
61
   * @var string
62
   */
63
  protected $name = null;
64
 
65
  /**
66
   * Definition of the column
67
   *
68
   * @var array $definition
69
   */
70
  protected $definition = array();
71
 
72
  public function __construct($name, Doctrine_Table $table)
73
  {
74
    $this->name = $name;
75
    $this->table = $table;
76
    $this->definition = $table->getDefinitionOf($name);
77
  }
78
 
79
  /**
80
   * Get the name of the column
81
   *
82
   * @return string $name
83
   */
84
  public function getName()
85
  {
86
    return $this->table->getColumnName($this->name);
87
  }
88
 
89
  /**
90
   * Get the alias/field name
91
   *
92
   * @return string $fieldName
93
   */
94
  public function getFieldName()
95
  {
96
    return $this->table->getFieldName($this->getName());
97
  }
98
 
99
  /**
100
   * Get php name. Exists for backwards compatibility with propel orm
101
   *
102
   * @return string $fieldName
103
   */
104
  public function getPhpName()
105
  {
106
    return $this->getFieldName();
107
  }
108
 
109
  /**
110
   * Get the Doctrine type of the column
111
   *
112
   * @return void
113
   */
114
  public function getDoctrineType()
115
  {
116
    return isset($this->definition['type']) ? $this->definition['type']:null;
117
  }
118
 
119
  /**
120
   * Get symfony type of the column
121
   *
122
   * @return void
123
   */
124
  public function getType()
125
  {
126
    $doctrineType = $this->getDoctrineType();
127
 
128
    // we simulate the CHAR/VARCHAR types to generate input_tags
129
    if ('string' == $doctrineType && null !== $this->getSize() && $this->getSize() <= 255)
130
    {
131
      return 'VARCHAR';
132
    }
133
 
134
    return $doctrineType ? self::$doctrineToSymfony[$doctrineType] : 'VARCHAR';
135
  }
136
 
137
  /**
138
   * Get size/length of the column
139
   *
140
   * @return void
141
   */
142
  public function getSize()
143
  {
144
    return $this->definition['length'];
145
  }
146
 
147
  public function getLength()
148
  {
149
    return $this->getSize();
150
  }
151
 
152
  /**
153
   * Check if the column definition has a certain key
154
   *
155
   * @param string $key
156
   * @return bool
157
   */
158
  public function hasDefinitionKey($key)
159
  {
160
    return isset($this->definition[$key]) ? true:false;
161
  }
162
 
163
  /**
164
   * Get the value of a column definition key
165
   *
166
   * @param string $key
167
   * @return array $definition
168
   */
169
  public function getDefinitionKey($key)
170
  {
171
    if ($this->hasDefinitionKey($key))
172
    {
173
      return $this->definition[$key];
174
    } else {
175
      return false;
176
    }
177
  }
178
 
179
  /**
180
   * Returns a value from the current column's relation.
181
   *
182
   * @param string $key
183
   *
184
   * @return mixed|null
185
   */
186
  public function getRelationKey($key)
187
  {
188
    foreach ($this->table->getRelations() as $relation)
189
    {
190
      $local = (array) $relation['local'];
191
      $local = array_map('strtolower', $local);
192
      if (in_array(strtolower($this->name), $local))
193
      {
194
        return $relation[$key];
195
      }
196
    }
197
  }
198
 
199
  /**
200
   * Returns true of the column is not null and false if it is null
201
   *
202
   * @return boolean
203
   */
204
  public function isNotNull()
205
  {
206
    if (isset($this->definition['notnull']))
207
    {
208
      return $this->definition['notnull'];
209
    }
210
    if (isset($this->definition['notblank']))
211
    {
212
      return $this->definition['notblank'];
213
    }
214
    return false;
215
  }
216
 
217
  /**
218
   * Returns true if the column is a primary key and false if it is not
219
   *
220
   * @return void
221
   */
222
  public function isPrimaryKey()
223
  {
224
    if (isset($this->definition['primary']))
225
    {
226
      return $this->definition['primary'];
227
    }
228
    return false;
229
  }
230
 
231
  /**
232
   * Returns true if this column is a foreign key and false if it is not
233
   *
234
   * @return boolean $isForeignKey
235
   */
236
  public function isForeignKey()
237
  {
238
    if (isset($this->foreignClassName))
239
    {
240
      return true;
241
    }
242
 
243
    if ($this->isPrimaryKey())
244
    {
245
      return false;
246
    }
247
 
248
    foreach ($this->table->getRelations() as $relation)
249
    {
250
      $local = (array) $relation['local'];
251
      $local = array_map('strtolower', $local);
252
      if (in_array(strtolower($this->name), $local))
253
      {
254
        $this->foreignClassName = $relation['class'];
255
        return true;
256
      }
257
    }
258
    return false;
259
  }
260
 
261
  /**
262
   * Get the name of the related class for this column foreign key.
263
   *
264
   * @return string $foreignClassName
265
   */
266
  public function getForeignClassName()
267
  {
268
    if ($this->isForeignKey())
269
    {
270
      return $this->foreignClassName;
271
    } else {
272
      return false;
273
    }
274
  }
275
 
276
  /**
277
   * If foreign key get the related Doctrine_Table object
278
   *
279
   * @return Doctrine_Table $table
280
   */
281
  public function getForeignTable()
282
  {
283
    if ($this->isForeignKey())
284
    {
285
      return Doctrine_Core::getTable($this->foreignClassName);
286
    } else {
287
      return false;
288
    }
289
  }
290
 
291
  /**
292
   * Set the Doctrine_Table object this column belongs to
293
   *
294
   * @param Doctrine_Table $table
295
   * @return void
296
   */
297
  public function setTable(Doctrine_Table $table)
298
  {
299
    $this->table = $table;
300
  }
301
 
302
  /**
303
   * Get the Doctrine_Table object this column belongs to
304
   *
305
   * @return Doctrine_Table $table
306
   */
307
  public function getTable()
308
  {
309
    return $this->table;
310
  }
311
 
312
  public function offsetExists($offset)
313
  {
314
    return isset($this->definition[$offset]);
315
  }
316
 
317
  public function offsetSet($offset, $value)
318
  {
319
    $this->definition[$offset] = $value;
320
  }
321
 
322
  public function offsetGet($offset)
323
  {
324
    return $this->definition[$offset];
325
  }
326
 
327
  public function offsetUnset($offset)
328
  {
329
    unset($this->definition[$offset]);
330
  }
331
}