| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
class sfDoctrineGenerator extends sfModelGenerator |
|---|
| 20 |
{ |
|---|
| 21 |
protected |
|---|
| 22 |
$table = null; |
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
* Initializes the current sfGenerator instance. |
|---|
| 26 |
* |
|---|
| 27 |
* @param sfGeneratorManager $generatorManager A sfGeneratorManager instance |
|---|
| 28 |
*/ |
|---|
| 29 |
public function initialize(sfGeneratorManager $generatorManager) |
|---|
| 30 |
{ |
|---|
| 31 |
parent::initialize($generatorManager); |
|---|
| 32 |
|
|---|
| 33 |
$this->setGeneratorClass('sfDoctrineModule'); |
|---|
| 34 |
} |
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
* Configures this generator. |
|---|
| 38 |
*/ |
|---|
| 39 |
public function configure() |
|---|
| 40 |
{ |
|---|
| 41 |
$this->table = Doctrine_Core::getTable($this->modelClass); |
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
$this->loadPrimaryKeys(); |
|---|
| 45 |
} |
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
* Returns an array of tables that represents a many to many relationship. |
|---|
| 49 |
* |
|---|
| 50 |
* A table is considered to be a m2m table if it has 2 foreign keys that are also primary keys. |
|---|
| 51 |
* |
|---|
| 52 |
* @return array An array of tables. |
|---|
| 53 |
*/ |
|---|
| 54 |
public function getManyToManyTables() |
|---|
| 55 |
{ |
|---|
| 56 |
$relations = array(); |
|---|
| 57 |
foreach ($this->table->getRelations() as $relation) |
|---|
| 58 |
{ |
|---|
| 59 |
if ($relation->getType() === Doctrine_Relation::MANY && isset($relation['refTable'])) |
|---|
| 60 |
{ |
|---|
| 61 |
$relations[] = $relation; |
|---|
| 62 |
} |
|---|
| 63 |
} |
|---|
| 64 |
return $relations; |
|---|
| 65 |
} |
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
* Loads primary keys. |
|---|
| 69 |
* |
|---|
| 70 |
* @throws sfException |
|---|
| 71 |
*/ |
|---|
| 72 |
protected function loadPrimaryKeys() |
|---|
| 73 |
{ |
|---|
| 74 |
$this->primaryKey = array(); |
|---|
| 75 |
foreach ($this->getColumns() as $name => $column) |
|---|
| 76 |
{ |
|---|
| 77 |
if ($column->isPrimaryKey()) |
|---|
| 78 |
{ |
|---|
| 79 |
$this->primaryKey[] = $name; |
|---|
| 80 |
} |
|---|
| 81 |
} |
|---|
| 82 |
|
|---|
| 83 |
if (!count($this->primaryKey)) |
|---|
| 84 |
{ |
|---|
| 85 |
throw new sfException(sprintf('Cannot generate a module for a model without a primary key (%s)', $this->modelClass)); |
|---|
| 86 |
} |
|---|
| 87 |
} |
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
* Returns the getter either non-developped: 'getFoo' or developped: '$class->getFoo()'. |
|---|
| 91 |
* |
|---|
| 92 |
* @param string $column The column name |
|---|
| 93 |
* @param boolean $developed true if you want developped method names, false otherwise |
|---|
| 94 |
* @param string $prefix The prefix value |
|---|
| 95 |
* |
|---|
| 96 |
* @return string PHP code |
|---|
| 97 |
*/ |
|---|
| 98 |
public function getColumnGetter($column, $developed = false, $prefix = '') |
|---|
| 99 |
{ |
|---|
| 100 |
$getter = 'get'.sfInflector::camelize($column); |
|---|
| 101 |
if ($developed) |
|---|
| 102 |
{ |
|---|
| 103 |
$getter = sprintf('$%s%s->%s()', $prefix, $this->getSingularName(), $getter); |
|---|
| 104 |
} |
|---|
| 105 |
|
|---|
| 106 |
return $getter; |
|---|
| 107 |
} |
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 |
* Returns the type of a column. |
|---|
| 111 |
* |
|---|
| 112 |
* @param object $column A column object |
|---|
| 113 |
* |
|---|
| 114 |
* @return string The column type |
|---|
| 115 |
*/ |
|---|
| 116 |
public function getType($column) |
|---|
| 117 |
{ |
|---|
| 118 |
if ($column->isForeignKey()) |
|---|
| 119 |
{ |
|---|
| 120 |
return 'ForeignKey'; |
|---|
| 121 |
} |
|---|
| 122 |
|
|---|
| 123 |
switch ($column->getDoctrineType()) |
|---|
| 124 |
{ |
|---|
| 125 |
case 'enum': |
|---|
| 126 |
return 'Enum'; |
|---|
| 127 |
case 'boolean': |
|---|
| 128 |
return 'Boolean'; |
|---|
| 129 |
case 'date': |
|---|
| 130 |
case 'timestamp': |
|---|
| 131 |
return 'Date'; |
|---|
| 132 |
case 'time': |
|---|
| 133 |
return 'Time'; |
|---|
| 134 |
default: |
|---|
| 135 |
return 'Text'; |
|---|
| 136 |
} |
|---|
| 137 |
} |
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 |
* Returns the default configuration for fields. |
|---|
| 141 |
* |
|---|
| 142 |
* @return array An array of default configuration for all fields |
|---|
| 143 |
*/ |
|---|
| 144 |
public function getDefaultFieldsConfiguration() |
|---|
| 145 |
{ |
|---|
| 146 |
$fields = array(); |
|---|
| 147 |
|
|---|
| 148 |
$names = array(); |
|---|
| 149 |
foreach ($this->getColumns() as $name => $column) |
|---|
| 150 |
{ |
|---|
| 151 |
$names[] = $name; |
|---|
| 152 |
$fields[$name] = array_merge(array( |
|---|
| 153 |
'is_link' => (Boolean) $column->isPrimaryKey(), |
|---|
| 154 |
'is_real' => true, |
|---|
| 155 |
'is_partial' => false, |
|---|
| 156 |
'is_component' => false, |
|---|
| 157 |
'type' => $this->getType($column), |
|---|
| 158 |
), isset($this->config['fields'][$name]) ? $this->config['fields'][$name] : array()); |
|---|
| 159 |
} |
|---|
| 160 |
|
|---|
| 161 |
foreach ($this->getManyToManyTables() as $tables) |
|---|
| 162 |
{ |
|---|
| 163 |
$name = sfInflector::underscore($tables['alias']).'_list'; |
|---|
| 164 |
$names[] = $name; |
|---|
| 165 |
$fields[$name] = array_merge(array( |
|---|
| 166 |
'is_link' => false, |
|---|
| 167 |
'is_real' => false, |
|---|
| 168 |
'is_partial' => false, |
|---|
| 169 |
'is_component' => false, |
|---|
| 170 |
'type' => 'Text', |
|---|
| 171 |
), isset($this->config['fields'][$name]) ? $this->config['fields'][$name] : array()); |
|---|
| 172 |
} |
|---|
| 173 |
|
|---|
| 174 |
if (isset($this->config['fields'])) |
|---|
| 175 |
{ |
|---|
| 176 |
foreach ($this->config['fields'] as $name => $params) |
|---|
| 177 |
{ |
|---|
| 178 |
if (in_array($name, $names)) |
|---|
| 179 |
{ |
|---|
| 180 |
continue; |
|---|
| 181 |
} |
|---|
| 182 |
|
|---|
| 183 |
$fields[$name] = array_merge(array( |
|---|
| 184 |
'is_link' => false, |
|---|
| 185 |
'is_real' => false, |
|---|
| 186 |
'is_partial' => false, |
|---|
| 187 |
'is_component' => false, |
|---|
| 188 |
'type' => 'Text', |
|---|
| 189 |
), is_array($params) ? $params : array()); |
|---|
| 190 |
} |
|---|
| 191 |
} |
|---|
| 192 |
|
|---|
| 193 |
unset($this->config['fields']); |
|---|
| 194 |
|
|---|
| 195 |
return $fields; |
|---|
| 196 |
} |
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 |
* Returns the configuration for fields in a given context. |
|---|
| 200 |
* |
|---|
| 201 |
* @param string $context The Context |
|---|
| 202 |
* |
|---|
| 203 |
* @return array An array of configuration for all the fields in a given context |
|---|
| 204 |
*/ |
|---|
| 205 |
public function getFieldsConfiguration($context) |
|---|
| 206 |
{ |
|---|
| 207 |
$fields = array(); |
|---|
| 208 |
|
|---|
| 209 |
$names = array(); |
|---|
| 210 |
foreach ($this->getColumns() as $name => $column) |
|---|
| 211 |
{ |
|---|
| 212 |
$names[] = $name; |
|---|
| 213 |
$fields[$name] = isset($this->config[$context]['fields'][$name]) ? $this->config[$context]['fields'][$name] : array(); |
|---|
| 214 |
} |
|---|
| 215 |
|
|---|
| 216 |
foreach ($this->getManyToManyTables() as $tables) |
|---|
| 217 |
{ |
|---|
| 218 |
$name = sfInflector::underscore($tables['alias']).'_list'; |
|---|
| 219 |
$names[] = $name; |
|---|
| 220 |
$fields[$name] = isset($this->config[$context]['fields'][$name]) ? $this->config[$context]['fields'][$name] : array(); |
|---|
| 221 |
} |
|---|
| 222 |
|
|---|
| 223 |
if (isset($this->config[$context]['fields'])) |
|---|
| 224 |
{ |
|---|
| 225 |
foreach ($this->config[$context]['fields'] as $name => $params) |
|---|
| 226 |
{ |
|---|
| 227 |
if (in_array($name, $names)) |
|---|
| 228 |
{ |
|---|
| 229 |
continue; |
|---|
| 230 |
} |
|---|
| 231 |
|
|---|
| 232 |
$fields[$name] = is_array($params) ? $params : array(); |
|---|
| 233 |
} |
|---|
| 234 |
} |
|---|
| 235 |
|
|---|
| 236 |
unset($this->config[$context]['fields']); |
|---|
| 237 |
|
|---|
| 238 |
return $fields; |
|---|
| 239 |
} |
|---|
| 240 |
|
|---|
| 241 |
|
|---|
| 242 |
* Gets all the fields for the current model. |
|---|
| 243 |
* |
|---|
| 244 |
* @param Boolean $withM2M Whether to include m2m fields or not |
|---|
| 245 |
* |
|---|
| 246 |
* @return array An array of field names |
|---|
| 247 |
*/ |
|---|
| 248 |
public function getAllFieldNames($withM2M = true) |
|---|
| 249 |
{ |
|---|
| 250 |
$names = array(); |
|---|
| 251 |
foreach ($this->getColumns() as $name => $column) |
|---|
| 252 |
{ |
|---|
| 253 |
$names[] = $name; |
|---|
| 254 |
} |
|---|
| 255 |
|
|---|
| 256 |
if ($withM2M) |
|---|
| 257 |
{ |
|---|
| 258 |
foreach ($this->getManyToManyTables() as $tables) |
|---|
| 259 |
{ |
|---|
| 260 |
$names[] = sfInflector::underscore($tables['alias']).'_list'; |
|---|
| 261 |
} |
|---|
| 262 |
} |
|---|
| 263 |
|
|---|
| 264 |
return $names; |
|---|
| 265 |
} |
|---|
| 266 |
|
|---|
| 267 |
|
|---|
| 268 |
* Get array of sfDoctrineAdminColumn objects |
|---|
| 269 |
* |
|---|
| 270 |
* @return array $columns |
|---|
| 271 |
*/ |
|---|
| 272 |
public function getColumns() |
|---|
| 273 |
{ |
|---|
| 274 |
foreach (array_keys($this->table->getColumns()) as $name) |
|---|
| 275 |
{ |
|---|
| 276 |
$name = $this->table->getFieldName($name); |
|---|
| 277 |
$columns[$name] = new sfDoctrineColumn($name, $this->table); |
|---|
| 278 |
} |
|---|
| 279 |
|
|---|
| 280 |
return $columns; |
|---|
| 281 |
} |
|---|
| 282 |
} |
|---|