PageRenderTime 53ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/vendor/propel/runtime/lib/map/TableMap.php

https://bitbucket.org/bayrock/gw2spidy
PHP | 791 lines | 325 code | 88 blank | 378 comment | 23 complexity | d26e634c4730165b3a8161aeb9d2bf84 MD5 | raw file
Possible License(s): BSD-3-Clause, BSD-2-Clause
  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. /**
  10. * TableMap is used to model a table in a database.
  11. *
  12. * GENERAL NOTE
  13. * ------------
  14. * The propel.map classes are abstract building-block classes for modeling
  15. * the database at runtime. These classes are similar (a lite version) to the
  16. * propel.engine.database.model classes, which are build-time modeling classes.
  17. * These classes in themselves do not do any database metadata lookups.
  18. *
  19. * @author Hans Lellelid <hans@xmpl.org> (Propel)
  20. * @author John D. McNally <jmcnally@collab.net> (Torque)
  21. * @author Daniel Rall <dlr@finemaltcoding.com> (Torque)
  22. * @version $Revision$
  23. * @package propel.runtime.map
  24. */
  25. class TableMap
  26. {
  27. /**
  28. * Columns in the table
  29. * @var array TableMap[]
  30. */
  31. protected $columns = array();
  32. /**
  33. * Columns in the table, using table phpName as key
  34. * @var array TableMap[]
  35. */
  36. protected $columnsByPhpName = array();
  37. /**
  38. * Columns in the table, using as key
  39. * @var array TableMap[]
  40. */
  41. protected $columnsByInsensitiveCase = array();
  42. // The database this table belongs to
  43. protected $dbMap;
  44. // The name of the table
  45. protected $tableName;
  46. // The PHP name of the table
  47. protected $phpName;
  48. // The Classname for this table
  49. protected $classname;
  50. // The Package for this table
  51. protected $package;
  52. // Whether to use an id generator for pkey
  53. protected $useIdGenerator;
  54. // Whether the table uses single table inheritance
  55. protected $isSingleTableInheritance = false;
  56. // Whether the table is a Many to Many table
  57. protected $isCrossRef = false;
  58. // The primary key columns in the table
  59. protected $primaryKeys = array();
  60. // The foreign key columns in the table
  61. protected $foreignKeys = array();
  62. // The relationships in the table
  63. protected $relations = array();
  64. // Relations are lazy loaded. This property tells if the relations are loaded or not
  65. protected $relationsBuilt = false;
  66. // Object to store information that is needed if the for generating primary keys
  67. protected $pkInfo;
  68. /**
  69. * Construct a new TableMap.
  70. *
  71. */
  72. public function __construct($name = null, $dbMap = null)
  73. {
  74. if (null !== $name) {
  75. $this->setName($name);
  76. }
  77. if (null !== $dbMap) {
  78. $this->setDatabaseMap($dbMap);
  79. }
  80. $this->initialize();
  81. }
  82. /**
  83. * Initialize the TableMap to build columns, relations, etc
  84. * This method should be overridden by descendents
  85. */
  86. public function initialize()
  87. {
  88. }
  89. /**
  90. * Set the DatabaseMap containing this TableMap.
  91. *
  92. * @param DatabaseMap $dbMap A DatabaseMap.
  93. */
  94. public function setDatabaseMap(DatabaseMap $dbMap)
  95. {
  96. $this->dbMap = $dbMap;
  97. }
  98. /**
  99. * Get the DatabaseMap containing this TableMap.
  100. *
  101. * @return DatabaseMap A DatabaseMap.
  102. */
  103. public function getDatabaseMap()
  104. {
  105. return $this->dbMap;
  106. }
  107. /**
  108. * Set the name of the Table.
  109. *
  110. * @param string $name The name of the table.
  111. */
  112. public function setName($name)
  113. {
  114. $this->tableName = $name;
  115. }
  116. /**
  117. * Get the name of the Table.
  118. *
  119. * @return string A String with the name of the table.
  120. */
  121. public function getName()
  122. {
  123. return $this->tableName;
  124. }
  125. /**
  126. * Set the PHP name of the Table.
  127. *
  128. * @param string $phpName The PHP Name for this table
  129. */
  130. public function setPhpName($phpName)
  131. {
  132. $this->phpName = $phpName;
  133. }
  134. /**
  135. * Get the PHP name of the Table.
  136. *
  137. * @return string A String with the name of the table.
  138. */
  139. public function getPhpName()
  140. {
  141. return $this->phpName;
  142. }
  143. /**
  144. * Set the Classname of the Table. Could be useful for calling
  145. * Peer and Object methods dynamically.
  146. * @param string $classname The Classname
  147. */
  148. public function setClassname($classname)
  149. {
  150. $this->classname = $classname;
  151. }
  152. /**
  153. * Get the Classname of the Propel Class belonging to this table.
  154. * @return string
  155. */
  156. public function getClassname()
  157. {
  158. return $this->classname;
  159. }
  160. /**
  161. * Get the Peer Classname of the Propel Class belonging to this table.
  162. * @return string
  163. */
  164. public function getPeerClassname()
  165. {
  166. return constant($this->classname . '::PEER');
  167. }
  168. /**
  169. * Set the Package of the Table
  170. *
  171. * @param string $package The Package
  172. */
  173. public function setPackage($package)
  174. {
  175. $this->package = $package;
  176. }
  177. /**
  178. * Get the Package of the table.
  179. * @return string
  180. */
  181. public function getPackage()
  182. {
  183. return $this->package;
  184. }
  185. /**
  186. * Set whether or not to use Id generator for primary key.
  187. * @param boolean $bit
  188. */
  189. public function setUseIdGenerator($bit)
  190. {
  191. $this->useIdGenerator = $bit;
  192. }
  193. /**
  194. * Whether to use Id generator for primary key.
  195. * @return boolean
  196. */
  197. public function isUseIdGenerator()
  198. {
  199. return $this->useIdGenerator;
  200. }
  201. /**
  202. * Set whether or not to this table uses single table inheritance
  203. * @param boolean $bit
  204. */
  205. public function setSingleTableInheritance($bit)
  206. {
  207. $this->isSingleTableInheritance = $bit;
  208. }
  209. /**
  210. * Whether this table uses single table inheritance
  211. * @return boolean
  212. */
  213. public function isSingleTableInheritance()
  214. {
  215. return $this->isSingleTableInheritance;
  216. }
  217. /**
  218. * Sets the name of the sequencxe used to generate a key
  219. *
  220. * @param $pkInfo information needed to generate a key
  221. */
  222. public function setPrimaryKeyMethodInfo($pkInfo)
  223. {
  224. $this->pkInfo = $pkInfo;
  225. }
  226. /**
  227. * Get the name of the sequence used to generate a primary key
  228. *
  229. * @return An Object.
  230. */
  231. public function getPrimaryKeyMethodInfo()
  232. {
  233. return $this->pkInfo;
  234. }
  235. /**
  236. * Add a column to the table.
  237. *
  238. * @param string name A String with the column name.
  239. * @param string $type A string specifying the Propel type.
  240. * @param boolean $isNotNull Whether column does not allow NULL values.
  241. * @param int $size An int specifying the size.
  242. * @param boolean $pk True if column is a primary key.
  243. * @param string $fkTable A String with the foreign key table name.
  244. * @param $fkColumn A String with the foreign key column name.
  245. * @param string $defaultValue The default value for this column.
  246. * @return ColumnMap The newly created column.
  247. */
  248. public function addColumn($name, $phpName, $type, $isNotNull = false, $size = null, $defaultValue = null, $pk = false, $fkTable = null, $fkColumn = null)
  249. {
  250. $col = new ColumnMap($name, $this);
  251. $col->setType($type);
  252. $col->setSize($size);
  253. $col->setPhpName($phpName);
  254. $col->setNotNull($isNotNull);
  255. $col->setDefaultValue($defaultValue);
  256. if ($pk) {
  257. $col->setPrimaryKey(true);
  258. $this->primaryKeys[$name] = $col;
  259. }
  260. if ($fkTable && $fkColumn) {
  261. $col->setForeignKey($fkTable, $fkColumn);
  262. $this->foreignKeys[$name] = $col;
  263. }
  264. $this->columns[$name] = $col;
  265. $this->columnsByPhpName[$phpName] = $col;
  266. $this->columnsByInsensitiveCase[strtolower($phpName)] = $col;
  267. return $col;
  268. }
  269. /**
  270. * Add a pre-created column to this table. It will replace any
  271. * existing column.
  272. *
  273. * @param ColumnMap $cmap A ColumnMap.
  274. * @return ColumnMap The added column map.
  275. */
  276. public function addConfiguredColumn($cmap)
  277. {
  278. $this->columns[ $cmap->getName() ] = $cmap;
  279. return $cmap;
  280. }
  281. /**
  282. * Does this table contain the specified column?
  283. *
  284. * @param mixed $name name of the column or ColumnMap instance
  285. * @param boolean $normalize Normalize the column name (if column name not like FIRST_NAME)
  286. * @return boolean True if the table contains the column.
  287. */
  288. public function hasColumn($name, $normalize = true)
  289. {
  290. if ($name instanceof ColumnMap) {
  291. $name = $name->getColumnName();
  292. } elseif ($normalize) {
  293. $name = ColumnMap::normalizeName($name);
  294. }
  295. return isset($this->columns[$name]);
  296. }
  297. /**
  298. * Get a ColumnMap for the table.
  299. *
  300. * @param string $name A String with the name of the table.
  301. * @param boolean $normalize Normalize the column name (if column name not like FIRST_NAME)
  302. * @return ColumnMap A ColumnMap.
  303. * @throws PropelException if the column is undefined
  304. */
  305. public function getColumn($name, $normalize = true)
  306. {
  307. if ($normalize) {
  308. $name = ColumnMap::normalizeName($name);
  309. }
  310. if (!$this->hasColumn($name, false)) {
  311. throw new PropelException("Cannot fetch ColumnMap for undefined column: " . $name);
  312. }
  313. return $this->columns[$name];
  314. }
  315. /**
  316. * Does this table contain the specified column?
  317. *
  318. * @param mixed $phpName name of the column
  319. * @return boolean True if the table contains the column.
  320. */
  321. public function hasColumnByPhpName($phpName)
  322. {
  323. return isset($this->columnsByPhpName[$phpName]);
  324. }
  325. /**
  326. * Get a ColumnMap for the table.
  327. *
  328. * @param string $phpName A String with the name of the table.
  329. * @return ColumnMap A ColumnMap.
  330. * @throws PropelException if the column is undefined
  331. */
  332. public function getColumnByPhpName($phpName)
  333. {
  334. if (!isset($this->columnsByPhpName[$phpName])) {
  335. throw new PropelException("Cannot fetch ColumnMap for undefined column phpName: " . $phpName);
  336. }
  337. return $this->columnsByPhpName[$phpName];
  338. }
  339. public function hasColumnByInsensitiveCase($colName)
  340. {
  341. return isset($this->columnsByInsensitiveCase[strtolower($colName)]);
  342. }
  343. public function getColumnByInsensitiveCase($colName)
  344. {
  345. $colName = strtolower($colName);
  346. if (!isset($this->columnsByInsensitiveCase[$colName])) {
  347. throw new PropelException("Cannot fetch ColumnMap for undefined column name: " . $colName);
  348. }
  349. return $this->columnsByInsensitiveCase[$colName];
  350. }
  351. /**
  352. * Get a ColumnMap[] of the columns in this table.
  353. *
  354. * @return array A ColumnMap[].
  355. */
  356. public function getColumns()
  357. {
  358. return $this->columns;
  359. }
  360. /**
  361. * Add a primary key column to this Table.
  362. *
  363. * @param string $columnName A String with the column name.
  364. * @param string $type A string specifying the Propel type.
  365. * @param boolean $isNotNull Whether column does not allow NULL values.
  366. * @param $size An int specifying the size.
  367. * @return ColumnMap Newly added PrimaryKey column.
  368. */
  369. public function addPrimaryKey($columnName, $phpName, $type, $isNotNull = false, $size = null, $defaultValue = null)
  370. {
  371. return $this->addColumn($columnName, $phpName, $type, $isNotNull, $size, $defaultValue, true, null, null);
  372. }
  373. /**
  374. * Add a foreign key column to the table.
  375. *
  376. * @param string $columnName A String with the column name.
  377. * @param string $type A string specifying the Propel type.
  378. * @param string $fkTable A String with the foreign key table name.
  379. * @param string $fkColumn A String with the foreign key column name.
  380. * @param boolean $isNotNull Whether column does not allow NULL values.
  381. * @param int $size An int specifying the size.
  382. * @param string $defaultValue The default value for this column.
  383. * @return ColumnMap Newly added ForeignKey column.
  384. */
  385. public function addForeignKey($columnName, $phpName, $type, $fkTable, $fkColumn, $isNotNull = false, $size = 0, $defaultValue = null)
  386. {
  387. return $this->addColumn($columnName, $phpName, $type, $isNotNull, $size, $defaultValue, false, $fkTable, $fkColumn);
  388. }
  389. /**
  390. * Add a foreign primary key column to the table.
  391. *
  392. * @param string $columnName A String with the column name.
  393. * @param string $type A string specifying the Propel type.
  394. * @param string $fkTable A String with the foreign key table name.
  395. * @param string $fkColumn A String with the foreign key column name.
  396. * @param boolean $isNotNull Whether column does not allow NULL values.
  397. * @param int $size An int specifying the size.
  398. * @param string $defaultValue The default value for this column.
  399. * @return ColumnMap Newly created foreign pkey column.
  400. */
  401. public function addForeignPrimaryKey($columnName, $phpName, $type, $fkTable, $fkColumn, $isNotNull = false, $size = 0, $defaultValue = null)
  402. {
  403. return $this->addColumn($columnName, $phpName, $type, $isNotNull, $size, $defaultValue, true, $fkTable, $fkColumn);
  404. }
  405. /**
  406. * @return boolean true if the table is a many to many
  407. */
  408. public function isCrossRef()
  409. {
  410. return $this->isCrossRef;
  411. }
  412. /**
  413. * set the isCrossRef
  414. * @param boolean $isCrossRef
  415. */
  416. public function setIsCrossRef($isCrossRef)
  417. {
  418. $this->isCrossRef = $isCrossRef;
  419. }
  420. /**
  421. * Returns array of ColumnMap objects that make up the primary key for this table
  422. *
  423. * @return array ColumnMap[]
  424. */
  425. public function getPrimaryKeys()
  426. {
  427. return $this->primaryKeys;
  428. }
  429. /**
  430. * Returns array of ColumnMap objects that are foreign keys for this table
  431. *
  432. * @return array ColumnMap[]
  433. */
  434. public function getForeignKeys()
  435. {
  436. return $this->foreignKeys;
  437. }
  438. /**
  439. * Add a validator to a table's column
  440. *
  441. * @param string $columnName The name of the validator's column
  442. * @param string $name The rule name of this validator
  443. * @param string $classname The dot-path name of class to use (e.g. myapp.propel.MyValidator)
  444. * @param string $value
  445. * @param string $message The error message which is returned on invalid values
  446. * @return void
  447. */
  448. public function addValidator($columnName, $name, $classname, $value, $message)
  449. {
  450. if (false !== ($pos = strpos($columnName, '.'))) {
  451. $columnName = substr($columnName, $pos + 1);
  452. }
  453. $col = $this->getColumn($columnName);
  454. if ($col !== null) {
  455. $validator = new ValidatorMap($col);
  456. $validator->setName($name);
  457. $validator->setClass($classname);
  458. $validator->setValue($value);
  459. $validator->setMessage($message);
  460. $col->addValidator($validator);
  461. }
  462. }
  463. /**
  464. * Build relations
  465. * Relations are lazy loaded for performance reasons
  466. * This method should be overridden by descendents
  467. */
  468. public function buildRelations()
  469. {
  470. }
  471. /**
  472. * Adds a RelationMap to the table
  473. *
  474. * @param string $name The relation name
  475. * @param string $tablePhpName The related table name
  476. * @param integer $type The relation type (either RelationMap::MANY_TO_ONE, RelationMap::ONE_TO_MANY, or RelationMAp::ONE_TO_ONE)
  477. * @param array $columnMapping An associative array mapping column names (local => foreign)
  478. * @param string $onDelete SQL behavior upon deletion ('SET NULL', 'CASCADE', ...)
  479. * @param string $onUpdate SQL behavior upon update ('SET NULL', 'CASCADE', ...)
  480. * @param string $pluralName Optional plural name for *_TO_MANY relationships
  481. * @return RelationMap the built RelationMap object
  482. */
  483. public function addRelation($name, $tablePhpName, $type, $columnMapping = array(), $onDelete = null, $onUpdate = null, $pluralName = null)
  484. {
  485. // note: using phpName for the second table allows the use of DatabaseMap::getTableByPhpName()
  486. // and this method autoloads the TableMap if the table isn't loaded yet
  487. $relation = new RelationMap($name);
  488. $relation->setType($type);
  489. $relation->setOnUpdate($onUpdate);
  490. $relation->setOnDelete($onDelete);
  491. if (null !== $pluralName) {
  492. $relation->setPluralName($pluralName);
  493. }
  494. // set tables
  495. if ($type == RelationMap::MANY_TO_ONE) {
  496. $relation->setLocalTable($this);
  497. $relation->setForeignTable($this->dbMap->getTableByPhpName($tablePhpName));
  498. } else {
  499. $relation->setLocalTable($this->dbMap->getTableByPhpName($tablePhpName));
  500. $relation->setForeignTable($this);
  501. $columnMapping = array_flip($columnMapping);
  502. }
  503. // set columns
  504. foreach ($columnMapping as $local => $foreign) {
  505. $relation->addColumnMapping(
  506. $relation->getLocalTable()->getColumn($local),
  507. $relation->getForeignTable()->getColumn($foreign)
  508. );
  509. }
  510. $this->relations[$name] = $relation;
  511. return $relation;
  512. }
  513. /**
  514. * Gets a RelationMap of the table by relation name
  515. * This method will build the relations if they are not built yet
  516. *
  517. * @param String $name The relation name
  518. * @return boolean true if the relation exists
  519. */
  520. public function hasRelation($name)
  521. {
  522. return array_key_exists($name, $this->getRelations());
  523. }
  524. /**
  525. * Gets a RelationMap of the table by relation name
  526. * This method will build the relations if they are not built yet
  527. *
  528. * @param String $name The relation name
  529. * @return RelationMap The relation object
  530. * @throws PropelException When called on an inexistent relation
  531. */
  532. public function getRelation($name)
  533. {
  534. if (!array_key_exists($name, $this->getRelations())) {
  535. throw new PropelException('Calling getRelation() on an unknown relation, ' . $name);
  536. }
  537. return $this->relations[$name];
  538. }
  539. /**
  540. * Gets the RelationMap objects of the table
  541. * This method will build the relations if they are not built yet
  542. *
  543. * @return Array list of RelationMap objects
  544. */
  545. public function getRelations()
  546. {
  547. if (!$this->relationsBuilt) {
  548. $this->buildRelations();
  549. $this->relationsBuilt = true;
  550. }
  551. return $this->relations;
  552. }
  553. /**
  554. *
  555. * Gets the list of behaviors registered for this table
  556. *
  557. * @return array
  558. */
  559. public function getBehaviors()
  560. {
  561. return array();
  562. }
  563. /**
  564. * Does this table has a primaryString column?
  565. *
  566. * @return boolean True if the table has a primaryString column.
  567. */
  568. public function hasPrimaryStringColumn()
  569. {
  570. return null !== $this->getPrimaryStringColumn();
  571. }
  572. /**
  573. * Gets the ColumnMap for the primary string column.
  574. *
  575. * @return ColumnMap|null The primary string column, null if none given.
  576. */
  577. public function getPrimaryStringColumn()
  578. {
  579. foreach ($this->getColumns() as $column) {
  580. if ($column->isPrimaryString()) {
  581. return $column;
  582. }
  583. }
  584. return null;
  585. }
  586. // Deprecated methods and attributres, to be removed
  587. /**
  588. * Does this table contain the specified column?
  589. *
  590. * @deprecated Use hasColumn instead
  591. * @param mixed $name name of the column or ColumnMap instance
  592. * @param boolean $normalize Normalize the column name (if column name not like FIRST_NAME)
  593. * @return boolean True if the table contains the column.
  594. */
  595. public function containsColumn($name, $normalize = true)
  596. {
  597. return $this->hasColumn($name, $normalize);
  598. }
  599. /**
  600. * Normalizes the column name, removing table prefix and uppercasing.
  601. * article.first_name becomes FIRST_NAME
  602. *
  603. * @deprecated Use ColumnMap::normalizeColumName() instead
  604. * @param string $name
  605. * @return string Normalized column name.
  606. */
  607. protected function normalizeColName($name)
  608. {
  609. return ColumnMap::normalizeName($name);
  610. }
  611. /**
  612. * Returns array of ColumnMap objects that make up the primary key for this table.
  613. *
  614. * @deprecated Use getPrimaryKeys instead
  615. * @return array ColumnMap[]
  616. */
  617. public function getPrimaryKeyColumns()
  618. {
  619. return array_values($this->primaryKeys);
  620. }
  621. //---Utility methods for doing intelligent lookup of table names
  622. /**
  623. * The prefix on the table name.
  624. * @deprecated Not used anywhere in Propel
  625. */
  626. private $prefix;
  627. /**
  628. * Get table prefix name.
  629. *
  630. * @deprecated Not used anywhere in Propel
  631. * @return string A String with the prefix.
  632. */
  633. public function getPrefix()
  634. {
  635. return $this->prefix;
  636. }
  637. /**
  638. * Set table prefix name.
  639. *
  640. * @deprecated Not used anywhere in Propel
  641. * @param string $prefix The prefix for the table name (ie: SCARAB for
  642. * SCARAB_PROJECT).
  643. * @return void
  644. */
  645. public function setPrefix($prefix)
  646. {
  647. $this->prefix = $prefix;
  648. }
  649. /**
  650. * Tell me if i have PREFIX in my string.
  651. *
  652. * @deprecated Not used anywhere in Propel
  653. * @param data A String.
  654. * @return boolean True if prefix is contained in data.
  655. */
  656. protected function hasPrefix($data)
  657. {
  658. return (strpos($data, $this->prefix) === 0);
  659. }
  660. /**
  661. * Removes the PREFIX if found
  662. *
  663. * @deprecated Not used anywhere in Propel
  664. * @param string $data A String.
  665. * @return string A String with data, but with prefix removed.
  666. */
  667. protected function removePrefix($data)
  668. {
  669. return $this->hasPrefix($data) ? substr($data, strlen($this->prefix)) : $data;
  670. }
  671. /**
  672. * Removes the PREFIX, removes the underscores and makes
  673. * first letter caps.
  674. *
  675. * SCARAB_FOO_BAR becomes FooBar.
  676. *
  677. * @deprecated Not used anywhere in Propel. At buildtime, use Column::generatePhpName() for that purpose
  678. * @param data A String.
  679. * @return string A String with data processed.
  680. */
  681. final public function removeUnderScores($data)
  682. {
  683. $out = '';
  684. $tmp = $this->removePrefix($data);
  685. $tok = strtok($tmp, '_');
  686. while ($tok) {
  687. $out .= ucfirst($tok);
  688. $tok = strtok('_');
  689. }
  690. return $out;
  691. }
  692. /**
  693. * Makes the first letter caps and the rest lowercase.
  694. *
  695. * @deprecated Not used anywhere in Propel.
  696. * @param string $data A String.
  697. * @return string A String with data processed.
  698. */
  699. private function firstLetterCaps($data)
  700. {
  701. return(ucfirst(strtolower($data)));
  702. }
  703. }