Development

/branches/1.2/lib/plugins/sfDoctrinePlugin/lib/generator/sfDoctrineColumn.class.php

You must first sign up to be able to contribute.

root/branches/1.2/lib/plugins/sfDoctrinePlugin/lib/generator/sfDoctrineColumn.class.php

Revision 20681, 6.1 kB (checked in by Kris.Wallsmith, 5 years ago)

[1.2, 1.3] fixed widget class selection to work with latest doctrine development which added the possibility of a string column with a length of null (closes #6873, thanks co3k)

Line 
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: sfDoctrineAdminColumn.class.php 12356 2008-10-23 21:54:50Z 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 && !is_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 true of the column is not null and false if it is null
181    *
182    * @return boolean
183    */
184   public function isNotNull()
185   {
186     if (isset($this->definition['notnull']))
187     {
188       return $this->definition['notnull'];
189     }
190     if (isset($this->definition['notblank']))
191     {
192       return $this->definition['notblank'];
193     }
194     return false;
195   }
196
197   /**
198    * Returns true if the column is a primary key and false if it is not
199    *
200    * @return void
201    */
202   public function isPrimaryKey()
203   {
204     if (isset($this->definition['primary']))
205     {
206       return $this->definition['primary'];
207     }
208     return false;
209   }
210
211   /**
212    * Returns true if this column is a foreign key and false if it is not
213    *
214    * @return boolean $isForeignKey
215    */
216   public function isForeignKey()
217   {
218     if (isset($this->foreignClassName))
219     {
220       return true;
221     }
222
223     if ($this->isPrimaryKey())
224     {
225       return false;
226     }
227
228     foreach ($this->table->getRelations() as $relation)
229     {
230       if (strtolower($relation['local']) == strtolower($this->name))
231       {
232         $this->foreignClassName = $relation['class'];
233         return true;
234       }
235     }
236     return false;
237   }
238
239   /**
240    * Get the name of the related class for this column foreign key.
241    *
242    * @return string $foreignClassName
243    */
244   public function getForeignClassName()
245   {
246     if ($this->isForeignKey())
247     {
248       return $this->foreignClassName;
249     } else {
250       return false;
251     }
252   }
253
254   /**
255    * If foreign key get the related Doctrine_Table object
256    *
257    * @return Doctrine_Table $table
258    */
259   public function getForeignTable()
260   {
261     if ($this->isForeignKey())
262     {
263       return Doctrine::getTable($this->foreignClassName);
264     } else {
265       return false;
266     }
267   }
268
269   /**
270    * Set the Doctrine_Table object this column belongs to
271    *
272    * @param Doctrine_Table $table
273    * @return void
274    */
275   public function setTable(Doctrine_Table $table)
276   {
277     $this->table = $table;
278   }
279
280   /**
281    * Get the Doctrine_Table object this column belongs to
282    *
283    * @return Doctrine_Table $table
284    */
285   public function getTable()
286   {
287     return $this->table;
288   }
289
290   public function offsetExists($offset)
291   {
292     return isset($this->definition[$offset]);
293   }
294
295   public function offsetSet($offset, $value)
296   {
297     $this->definition[$offset] = $value;
298   }
299
300   public function offsetGet($offset)
301   {
302     return $this->definition[$offset];
303   }
304
305   public function offsetUnset($offset)
306   {
307     unset($this->definition[$offset]);
308   }
309 }
Note: See TracBrowser for help on using the browser.