PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/propel/propel1/generator/lib/builder/om/PHP5TableMapBuilder.php

https://gitlab.com/Isaki/le331.fr
PHP | 376 lines | 263 code | 22 blank | 91 comment | 29 complexity | 5ef05cf22a22064b11df77bb54220779 MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of the Propel package.
  4. * For the full copyright and license information, please view the LICENSE
  5. * file that was distributed with this source code.
  6. *
  7. * @license MIT License
  8. */
  9. require_once dirname(__FILE__) . '/OMBuilder.php';
  10. /**
  11. * Generates the PHP5 table map class for user object model (OM).
  12. *
  13. * @author Hans Lellelid <hans@xmpl.org>
  14. * @package propel.generator.builder.om
  15. */
  16. class PHP5TableMapBuilder extends OMBuilder
  17. {
  18. /**
  19. * Gets the package for the map builder classes.
  20. *
  21. * @return string
  22. */
  23. public function getPackage()
  24. {
  25. return parent::getPackage() . '.map';
  26. }
  27. public function getNamespace()
  28. {
  29. if ($namespace = parent::getNamespace()) {
  30. if ($this->getGeneratorConfig() && $omns = $this->getGeneratorConfig()->getBuildProperty('namespaceMap')) {
  31. return $namespace . '\\' . $omns;
  32. } else {
  33. return $namespace;
  34. }
  35. }
  36. }
  37. /**
  38. * Returns the name of the current class being built.
  39. *
  40. * @return string
  41. */
  42. public function getUnprefixedClassname()
  43. {
  44. return $this->getTable()->getPhpName() . 'TableMap';
  45. }
  46. /**
  47. * Adds the include() statements for files that this class depends on or utilizes.
  48. *
  49. * @param string &$script The script will be modified in this method.
  50. */
  51. protected function addIncludes(&$script)
  52. {
  53. } // addIncludes()
  54. /**
  55. * Adds class phpdoc comment and opening of class.
  56. *
  57. * @param string &$script The script will be modified in this method.
  58. */
  59. protected function addClassOpen(&$script)
  60. {
  61. $table = $this->getTable();
  62. $script .= "
  63. /**
  64. * This class defines the structure of the '" . $table->getName() . "' table.
  65. *
  66. *";
  67. if ($this->getBuildProperty('addTimeStamp')) {
  68. $now = strftime('%c');
  69. $script .= "
  70. * This class was autogenerated by Propel " . $this->getBuildProperty('version') . " on:
  71. *
  72. * $now
  73. *";
  74. }
  75. $script .= "
  76. *
  77. * This map class is used by Propel to do runtime db structure discovery.
  78. * For example, the createSelectSql() method checks the type of a given column used in an
  79. * ORDER BY clause to know whether it needs to apply SQL to make the ORDER BY case-insensitive
  80. * (i.e. if it's a text column type).
  81. *
  82. * @package propel.generator." . $this->getPackage() . "
  83. */
  84. class " . $this->getClassname() . " extends TableMap
  85. {
  86. ";
  87. }
  88. /**
  89. * Specifies the methods that are added as part of the map builder class.
  90. * This can be overridden by subclasses that wish to add more methods.
  91. *
  92. * @see ObjectBuilder::addClassBody()
  93. */
  94. protected function addClassBody(&$script)
  95. {
  96. $this->declareClasses('TableMap', 'RelationMap');
  97. $this->addConstants($script);
  98. $this->addAttributes($script);
  99. $this->addInitialize($script);
  100. $this->addBuildRelations($script);
  101. $this->addGetBehaviors($script);
  102. }
  103. /**
  104. * Adds any constants needed for this TableMap class.
  105. *
  106. * @param string &$script The script will be modified in this method.
  107. */
  108. protected function addConstants(&$script)
  109. {
  110. $script .= "
  111. /**
  112. * The (dot-path) name of this class
  113. */
  114. const CLASS_NAME = '" . $this->getClasspath() . "';
  115. ";
  116. }
  117. /**
  118. * Adds any attributes needed for this TableMap class.
  119. *
  120. * @param string &$script The script will be modified in this method.
  121. */
  122. protected function addAttributes(&$script)
  123. {
  124. }
  125. /**
  126. * Closes class.
  127. *
  128. * @param string &$script The script will be modified in this method.
  129. */
  130. protected function addClassClose(&$script)
  131. {
  132. $script .= "
  133. } // " . $this->getClassname() . "
  134. ";
  135. $this->applyBehaviorModifier('tableMapFilter', $script, "");
  136. }
  137. /**
  138. * Adds the addInitialize() method to the table map class.
  139. *
  140. * @param string &$script The script will be modified in this method.
  141. */
  142. protected function addInitialize(&$script)
  143. {
  144. $table = $this->getTable();
  145. $platform = $this->getPlatform();
  146. $script .= "
  147. /**
  148. * Initialize the table attributes, columns and validators
  149. * Relations are not initialized by this method since they are lazy loaded
  150. *
  151. * @return void
  152. * @throws PropelException
  153. */
  154. public function initialize()
  155. {
  156. // attributes
  157. \$this->setName('" . $table->getName() . "');
  158. \$this->setPhpName('" . $table->getPhpName() . "');
  159. \$this->setClassname('" . addslashes($this->getStubObjectBuilder()->getFullyQualifiedClassname()) . "');
  160. \$this->setPackage('" . parent::getPackage() . "');";
  161. if ($table->getIdMethod() == "native") {
  162. $script .= "
  163. \$this->setUseIdGenerator(true);";
  164. } else {
  165. $script .= "
  166. \$this->setUseIdGenerator(false);";
  167. }
  168. if ($table->getIdMethodParameters()) {
  169. $params = $table->getIdMethodParameters();
  170. $imp = $params[0];
  171. $script .= "
  172. \$this->setPrimaryKeyMethodInfo('" . $imp->getValue() . "');";
  173. } elseif ($table->getIdMethod() == IDMethod::NATIVE && ($platform->getNativeIdMethod() == PropelPlatformInterface::SEQUENCE || $platform->getNativeIdMethod() == PropelPlatformInterface::SERIAL)) {
  174. $script .= "
  175. \$this->setPrimaryKeyMethodInfo('" . $platform->getSequenceName($table) . "');";
  176. }
  177. if ($this->getTable()->getChildrenColumn()) {
  178. $script .= "
  179. \$this->setSingleTableInheritance(true);";
  180. }
  181. if ($this->getTable()->getIsCrossRef()) {
  182. $script .= "
  183. \$this->setIsCrossRef(true);";
  184. }
  185. // Add columns to map
  186. $script .= "
  187. // columns";
  188. foreach ($table->getColumns() as $col) {
  189. $cup = $col->getName();
  190. $cfc = $col->getPhpName();
  191. if (!$col->getSize()) {
  192. $size = "null";
  193. } else {
  194. $size = $col->getSize();
  195. }
  196. $default = $col->getDefaultValueString();
  197. if ($col->isPrimaryKey()) {
  198. if ($col->isForeignKey()) {
  199. foreach ($col->getForeignKeys() as $fk) {
  200. $script .= "
  201. \$this->addForeignPrimaryKey('$cup', '$cfc', '" . $col->getType() . "' , '" . $fk->getForeignTableName() . "', '" . $fk->getMappedForeignColumn($col->getName()) . "', " . ($col->isNotNull() ? 'true' : 'false') . ", " . $size . ", $default);";
  202. }
  203. } else {
  204. $script .= "
  205. \$this->addPrimaryKey('$cup', '$cfc', '" . $col->getType() . "', " . var_export($col->isNotNull(), true) . ", " . $size . ", $default);";
  206. }
  207. } else {
  208. if ($col->isForeignKey()) {
  209. foreach ($col->getForeignKeys() as $fk) {
  210. $script .= "
  211. \$this->addForeignKey('$cup', '$cfc', '" . $col->getType() . "', '" . $fk->getForeignTableName() . "', '" . $fk->getMappedForeignColumn($col->getName()) . "', " . ($col->isNotNull() ? 'true' : 'false') . ", " . $size . ", $default);";
  212. }
  213. } else {
  214. $script .= "
  215. \$this->addColumn('$cup', '$cfc', '" . $col->getType() . "', " . var_export($col->isNotNull(), true) . ", " . $size . ", $default);";
  216. }
  217. } // if col-is prim key
  218. if ($col->getValueSet()) {
  219. $script .= "
  220. \$this->getColumn('$cup', false)->setValueSet(" . var_export($col->getValueSet(), true) . ");";
  221. }
  222. if ($col->isPrimaryString()) {
  223. $script .= "
  224. \$this->getColumn('$cup', false)->setPrimaryString(true);";
  225. }
  226. } // foreach
  227. // validators
  228. $script .= "
  229. // validators";
  230. foreach ($table->getValidators() as $val) {
  231. $col = $val->getColumn();
  232. $cup = $col->getName();
  233. foreach ($val->getRules() as $rule) {
  234. if ($val->getTranslate() !== Validator::TRANSLATE_NONE) {
  235. $script .= "
  236. \$this->addValidator('$cup', '" . $rule->getName() . "', '" . $rule->getClass() . "', '" . str_replace("'", "\'", $rule->getValue()) . "', " . $val->getTranslate() . "('" . str_replace("'", "\'", $rule->getMessage()) . "'));";
  237. } else {
  238. $script .= "
  239. \$this->addValidator('$cup', '" . $rule->getName() . "', '" . $rule->getClass() . "', '" . str_replace("'", "\'", $rule->getValue()) . "', '" . str_replace("'", "\'", $rule->getMessage()) . "');";
  240. } // if ($rule->getTranslation() ...
  241. } // foreach rule
  242. } // foreach validator
  243. $script .= "
  244. } // initialize()
  245. ";
  246. }
  247. /**
  248. * Adds the method that build the RelationMap objects
  249. *
  250. * @param string &$script The script will be modified in this method.
  251. */
  252. protected function addBuildRelations(&$script)
  253. {
  254. $script .= "
  255. /**
  256. * Build the RelationMap objects for this table relationships
  257. */
  258. public function buildRelations()
  259. {";
  260. foreach ($this->getTable()->getForeignKeys() as $fkey) {
  261. $columnMapping = 'array(';
  262. foreach ($fkey->getLocalForeignMapping() as $key => $value) {
  263. $columnMapping .= "'$key' => '$value', ";
  264. }
  265. $columnMapping .= ')';
  266. $onDelete = $fkey->hasOnDelete() ? "'" . $fkey->getOnDelete() . "'" : 'null';
  267. $onUpdate = $fkey->hasOnUpdate() ? "'" . $fkey->getOnUpdate() . "'" : 'null';
  268. $script .= "
  269. \$this->addRelation('" . $this->getFKPhpNameAffix($fkey) . "', '" . addslashes($this->getNewStubObjectBuilder($fkey->getForeignTable())->getFullyQualifiedClassname()) . "', RelationMap::MANY_TO_ONE, $columnMapping, $onDelete, $onUpdate);";
  270. }
  271. foreach ($this->getTable()->getReferrers() as $fkey) {
  272. $relationName = $this->getRefFKPhpNameAffix($fkey);
  273. $columnMapping = 'array(';
  274. foreach ($fkey->getForeignLocalMapping() as $key => $value) {
  275. $columnMapping .= "'$key' => '$value', ";
  276. }
  277. $columnMapping .= ')';
  278. $onDelete = $fkey->hasOnDelete() ? "'" . $fkey->getOnDelete() . "'" : 'null';
  279. $onUpdate = $fkey->hasOnUpdate() ? "'" . $fkey->getOnUpdate() . "'" : 'null';
  280. $script .= "
  281. \$this->addRelation('$relationName', '" . addslashes($this->getNewStubObjectBuilder($fkey->getTable())->getFullyQualifiedClassname()) . "', RelationMap::ONE_TO_" . ($fkey->isLocalPrimaryKey() ? "ONE" : "MANY") . ", $columnMapping, $onDelete, $onUpdate";
  282. if ($fkey->isLocalPrimaryKey()) {
  283. $script .= ");";
  284. } else {
  285. $script .= ", '" . $this->getRefFKPhpNameAffix($fkey, true) . "');";
  286. }
  287. }
  288. foreach ($this->getTable()->getCrossFks() as $fkList) {
  289. list($refFK, $crossFK) = $fkList;
  290. $relationName = $this->getFKPhpNameAffix($crossFK);
  291. $pluralName = "'" . $this->getFKPhpNameAffix($crossFK, true) . "'";
  292. $onDelete = $crossFK->hasOnDelete() ? "'" . $crossFK->getOnDelete() . "'" : 'null';
  293. $onUpdate = $crossFK->hasOnUpdate() ? "'" . $crossFK->getOnUpdate() . "'" : 'null';
  294. $script .= "
  295. \$this->addRelation('$relationName', '" . addslashes($this->getNewStubObjectBuilder($crossFK->getForeignTable())->getFullyQualifiedClassname()) . "', RelationMap::MANY_TO_MANY, array(), $onDelete, $onUpdate, $pluralName);";
  296. }
  297. $script .= "
  298. } // buildRelations()
  299. ";
  300. }
  301. /**
  302. * Adds the behaviors getter
  303. *
  304. * @param string &$script The script will be modified in this method.
  305. */
  306. protected function addGetBehaviors(&$script)
  307. {
  308. if ($behaviors = $this->getTable()->getBehaviors()) {
  309. $script .= "
  310. /**
  311. *
  312. * Gets the list of behaviors registered for this table
  313. *
  314. * @return array Associative array (name => parameters) of behaviors
  315. */
  316. public function getBehaviors()
  317. {
  318. return array(";
  319. foreach ($behaviors as $behavior) {
  320. $script .= "
  321. '{$behavior->getName()}' => " . var_export($behavior->getParameters(), true) . ",";
  322. }
  323. $script .= "
  324. );
  325. } // getBehaviors()
  326. ";
  327. }
  328. }
  329. /**
  330. * Checks whether any registered behavior on that table has a modifier for a hook
  331. *
  332. * @param string $hookName The name of the hook as called from one of this class methods, e.g. "preSave"
  333. *
  334. * @return boolean
  335. */
  336. public function hasBehaviorModifier($hookName, $modifier = null)
  337. {
  338. return parent::hasBehaviorModifier($hookName, 'TableMapBuilderModifier');
  339. }
  340. /**
  341. * Checks whether any registered behavior on that table has a modifier for a hook
  342. *
  343. * @param string $hookName The name of the hook as called from one of this class methods, e.g. "preSave"
  344. * @param string &$script The script will be modified in this method.
  345. */
  346. public function applyBehaviorModifier($hookName, &$script, $tab = " ")
  347. {
  348. return $this->applyBehaviorModifierBase($hookName, 'TableMapBuilderModifier', $script, $tab);
  349. }
  350. } // PHP5TableMapBuilder