PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/runtime/lib/map/TableMap.php

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