| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
class sfPropelCrudGenerator extends sfAdminGenerator |
|---|
| 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('sfPropelCrud'); |
|---|
| 34 |
} |
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
* Loads primary keys. |
|---|
| 38 |
* |
|---|
| 39 |
* This method is ORM dependant. |
|---|
| 40 |
* |
|---|
| 41 |
* @throws sfException |
|---|
| 42 |
*/ |
|---|
| 43 |
protected function loadPrimaryKeys() |
|---|
| 44 |
{ |
|---|
| 45 |
foreach ($this->tableMap->getColumns() as $column) |
|---|
| 46 |
{ |
|---|
| 47 |
if ($column->isPrimaryKey()) |
|---|
| 48 |
{ |
|---|
| 49 |
$this->primaryKey[] = $column; |
|---|
| 50 |
} |
|---|
| 51 |
} |
|---|
| 52 |
|
|---|
| 53 |
if (!count($this->primaryKey)) |
|---|
| 54 |
{ |
|---|
| 55 |
throw new sfException(sprintf('Cannot generate a module for a model without a primary key (%s)', $this->className)); |
|---|
| 56 |
} |
|---|
| 57 |
} |
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
* Loads map builder classes. |
|---|
| 61 |
* |
|---|
| 62 |
* This method is ORM dependant. |
|---|
| 63 |
* |
|---|
| 64 |
* @throws sfException |
|---|
| 65 |
*/ |
|---|
| 66 |
protected function loadMapBuilderClasses() |
|---|
| 67 |
{ |
|---|
| 68 |
|
|---|
| 69 |
$classes = sfFinder::type('file')->name('*MapBuilder.php')->in($this->generatorManager->getConfiguration()->getModelDirs()); |
|---|
| 70 |
foreach ($classes as $class) |
|---|
| 71 |
{ |
|---|
| 72 |
$class_map_builder = basename($class, '.php'); |
|---|
| 73 |
$maps[$class_map_builder] = new $class_map_builder(); |
|---|
| 74 |
if (!$maps[$class_map_builder]->isBuilt()) |
|---|
| 75 |
{ |
|---|
| 76 |
$maps[$class_map_builder]->doBuild(); |
|---|
| 77 |
} |
|---|
| 78 |
|
|---|
| 79 |
if ($this->className == str_replace('MapBuilder', '', $class_map_builder)) |
|---|
| 80 |
{ |
|---|
| 81 |
$this->map = $maps[$class_map_builder]; |
|---|
| 82 |
} |
|---|
| 83 |
} |
|---|
| 84 |
if (!$this->map) |
|---|
| 85 |
{ |
|---|
| 86 |
throw new sfException(sprintf('The model class "%s" does not exist.', $this->className)); |
|---|
| 87 |
} |
|---|
| 88 |
|
|---|
| 89 |
$this->tableMap = $this->map->getDatabaseMap()->getTable(constant($this->className.'Peer::TABLE_NAME')); |
|---|
| 90 |
} |
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
* Generates a PHP call to an object helper. |
|---|
| 94 |
* |
|---|
| 95 |
* @param string $helperName The helper name |
|---|
| 96 |
* @param string $column The column name |
|---|
| 97 |
* @param array $params An array of parameters |
|---|
| 98 |
* @param array $localParams An array of local parameters |
|---|
| 99 |
* |
|---|
| 100 |
* @return string PHP code |
|---|
| 101 |
*/ |
|---|
| 102 |
function getPHPObjectHelper($helperName, $column, $params, $localParams = array()) |
|---|
| 103 |
{ |
|---|
| 104 |
$params = $this->getObjectTagParams($params, $localParams); |
|---|
| 105 |
|
|---|
| 106 |
return sprintf('object_%s($%s, \'%s\', %s)', $helperName, $this->getSingularName(), $this->getColumnGetter($column, false), $params); |
|---|
| 107 |
} |
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 |
* Returns the getter either non-developped: 'getFoo' or developped: '$class->getFoo()'. |
|---|
| 111 |
* |
|---|
| 112 |
* @param string $column The column name |
|---|
| 113 |
* @param boolean $developed true if you want developped method names, false otherwise |
|---|
| 114 |
* @param string $prefix The prefix value |
|---|
| 115 |
* |
|---|
| 116 |
* @return string PHP code |
|---|
| 117 |
*/ |
|---|
| 118 |
function getColumnGetter($column, $developed = false, $prefix = '') |
|---|
| 119 |
{ |
|---|
| 120 |
$getter = 'get'.$column->getPhpName(); |
|---|
| 121 |
if ($developed) |
|---|
| 122 |
{ |
|---|
| 123 |
$getter = sprintf('$%s%s->%s()', $prefix, $this->getSingularName(), $getter); |
|---|
| 124 |
} |
|---|
| 125 |
|
|---|
| 126 |
return $getter; |
|---|
| 127 |
} |
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
* Gets the PHP name of the related class name. |
|---|
| 131 |
* |
|---|
| 132 |
* Used for foreign keys only; this method should be removed when we use sfAdminColumn instead. |
|---|
| 133 |
* |
|---|
| 134 |
* @param string $column The column name |
|---|
| 135 |
* |
|---|
| 136 |
* @return string The PHP name of the related class name |
|---|
| 137 |
*/ |
|---|
| 138 |
function getRelatedClassName($column) |
|---|
| 139 |
{ |
|---|
| 140 |
$relatedTable = $this->getMap()->getDatabaseMap()->getTable($column->getRelatedTableName()); |
|---|
| 141 |
|
|---|
| 142 |
return $relatedTable->getPhpName(); |
|---|
| 143 |
} |
|---|
| 144 |
} |
|---|
| 145 |
|
|---|