PageRenderTime 45ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/vendor/symfony/lib/plugins/sfPropelPlugin/lib/vendor/propel/map/TableMap.php

https://bitbucket.org/ealexandru/jobeet
PHP | 654 lines | 261 code | 63 blank | 330 comment | 21 complexity | e691e371cd031777cc6976b34aa59959 MD5 | raw file
Possible License(s): ISC, AGPL-3.0, LGPL-2.1, BSD-3-Clause, LGPL-3.0
  1. <?php
  2. /*
  3. * $Id: TableMap.php 1262 2009-10-26 20:54:39Z francois $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information please see
  19. * <http://propel.phpdb.org>.
  20. */
  21. /**
  22. * TableMap is used to model a table in a database.
  23. *
  24. * GENERAL NOTE
  25. * ------------
  26. * The propel.map classes are abstract building-block classes for modeling
  27. * the database at runtime. These classes are similar (a lite version) to the
  28. * propel.engine.database.model classes, which are build-time modeling classes.
  29. * These classes in themselves do not do any database metadata lookups.
  30. *
  31. * @author Hans Lellelid <hans@xmpl.org> (Propel)
  32. * @author John D. McNally <jmcnally@collab.net> (Torque)
  33. * @author Daniel Rall <dlr@finemaltcoding.com> (Torque)
  34. * @version $Revision: 1262 $
  35. * @package propel.map
  36. */
  37. class TableMap {
  38. // The columns in the table
  39. protected $columns = array();
  40. // The database this table belongs to
  41. protected $dbMap;
  42. // The name of the table
  43. protected $tableName;
  44. // The PHP name of the table
  45. protected $phpName;
  46. // The Classname for this table
  47. protected $classname;
  48. // The Package for this table
  49. protected $package;
  50. // Whether to use an id generator for pkey
  51. protected $useIdGenerator;
  52. // The primary key columns in the table
  53. protected $primaryKeys = array();
  54. // The foreign key columns in the table
  55. protected $foreignKeys = array();
  56. // The relationships in the table
  57. protected $relations = array();
  58. // Relations are lazy loaded. This property tells if the relations are loaded or not
  59. protected $relationsBuilt = false;
  60. // Object to store information that is needed if the for generating primary keys
  61. protected $pkInfo;
  62. /**
  63. * Construct a new TableMap.
  64. *
  65. */
  66. public function __construct($name = null, $dbMap = null)
  67. {
  68. if(!is_null($name)) $this->setName($name);
  69. if(!is_null($dbMap)) $this->setDatabaseMap($dbMap);
  70. $this->initialize();
  71. }
  72. /**
  73. * Initialize the TableMap to build columns, relations, etc
  74. * This method should be overridden by descendents
  75. */
  76. public function initialize()
  77. {
  78. }
  79. /**
  80. * Set the DatabaseMap containing this TableMap.
  81. *
  82. * @param DatabaseMap $dbMap A DatabaseMap.
  83. */
  84. public function setDatabaseMap(DatabaseMap $dbMap)
  85. {
  86. $this->dbMap = $dbMap;
  87. }
  88. /**
  89. * Get the DatabaseMap containing this TableMap.
  90. *
  91. * @return DatabaseMap A DatabaseMap.
  92. */
  93. public function getDatabaseMap()
  94. {
  95. return $this->dbMap;
  96. }
  97. /**
  98. * Set the name of the Table.
  99. *
  100. * @param string $name The name of the table.
  101. */
  102. public function setName($name)
  103. {
  104. $this->tableName = $name;
  105. }
  106. /**
  107. * Get the name of the Table.
  108. *
  109. * @return string A String with the name of the table.
  110. */
  111. public function getName()
  112. {
  113. return $this->tableName;
  114. }
  115. /**
  116. * Set the PHP name of the Table.
  117. *
  118. * @param string $phpName The PHP Name for this table
  119. */
  120. public function setPhpName($phpName)
  121. {
  122. $this->phpName = $phpName;
  123. }
  124. /**
  125. * Get the PHP name of the Table.
  126. *
  127. * @return string A String with the name of the table.
  128. */
  129. public function getPhpName()
  130. {
  131. return $this->phpName;
  132. }
  133. /**
  134. * Set the Classname of the Table. Could be useful for calling
  135. * Peer and Object methods dynamically.
  136. * @param string $classname The Classname
  137. */
  138. public function setClassname($classname)
  139. {
  140. $this->classname = $classname;
  141. }
  142. /**
  143. * Get the Classname of the Propel-Classes belonging to this table.
  144. * @return string
  145. */
  146. public function getClassname()
  147. {
  148. return $this->classname;
  149. }
  150. /**
  151. * Set the Package of the Table
  152. *
  153. * @param string $package The Package
  154. */
  155. public function setPackage($package)
  156. {
  157. $this->package = $package;
  158. }
  159. /**
  160. * Get the Package of the table.
  161. * @return string
  162. */
  163. public function getPackage()
  164. {
  165. return $this->package;
  166. }
  167. /**
  168. * Set whether or not to use Id generator for primary key.
  169. * @param boolean $bit
  170. */
  171. public function setUseIdGenerator($bit)
  172. {
  173. $this->useIdGenerator = $bit;
  174. }
  175. /**
  176. * Whether to use Id generator for primary key.
  177. * @return boolean
  178. */
  179. public function isUseIdGenerator()
  180. {
  181. return $this->useIdGenerator;
  182. }
  183. /**
  184. * Sets the pk information needed to generate a key
  185. *
  186. * @param $pkInfo information needed to generate a key
  187. */
  188. public function setPrimaryKeyMethodInfo($pkInfo)
  189. {
  190. $this->pkInfo = $pkInfo;
  191. }
  192. /**
  193. * Get the information used to generate a primary key
  194. *
  195. * @return An Object.
  196. */
  197. public function getPrimaryKeyMethodInfo()
  198. {
  199. return $this->pkInfo;
  200. }
  201. /**
  202. * Add a column to the table.
  203. *
  204. * @param string name A String with the column name.
  205. * @param string $type A string specifying the Propel type.
  206. * @param boolean $isNotNull Whether column does not allow NULL values.
  207. * @param int $size An int specifying the size.
  208. * @param boolean $pk True if column is a primary key.
  209. * @param string $fkTable A String with the foreign key table name.
  210. * @param $fkColumn A String with the foreign key column name.
  211. * @param string $defaultValue The default value for this column.
  212. * @return ColumnMap The newly created column.
  213. */
  214. public function addColumn($name, $phpName, $type, $isNotNull = false, $size = null, $defaultValue = null, $pk = false, $fkTable = null, $fkColumn = null)
  215. {
  216. $col = new ColumnMap($name, $this);
  217. if ($fkTable && $fkColumn) {
  218. if (strpos($fkColumn, '.') > 0 && strpos($fkColumn, $fkTable) !== false) {
  219. $fkColumn = substr($fkColumn, strlen($fkTable) + 1);
  220. }
  221. $col->setForeignKey($fkTable, $fkColumn);
  222. $this->foreignKeys[$name] = $col;
  223. }
  224. $col->setType($type);
  225. $col->setSize($size);
  226. $col->setPhpName($phpName);
  227. $col->setNotNull($isNotNull);
  228. $col->setDefaultValue($defaultValue);
  229. if ($pk) {
  230. $col->setPrimaryKey(true);
  231. $this->primaryKeys[$name] = $col;
  232. }
  233. $this->columns[$name] = $col;
  234. return $this->columns[$name];
  235. }
  236. /**
  237. * Add a pre-created column to this table. It will replace any
  238. * existing column.
  239. *
  240. * @param ColumnMap $cmap A ColumnMap.
  241. * @return ColumnMap The added column map.
  242. */
  243. public function addConfiguredColumn($cmap)
  244. {
  245. $this->columns[ $cmap->getColumnName() ] = $cmap;
  246. return $cmap;
  247. }
  248. /**
  249. * Does this table contain the specified column?
  250. *
  251. * @param mixed $name name of the column or ColumnMap instance
  252. * @param boolean $normalize Normalize the column name (if column name not like FIRST_NAME)
  253. * @return boolean True if the table contains the column.
  254. */
  255. public function hasColumn($name, $normalize = true)
  256. {
  257. if ($name instanceof ColumnMap) {
  258. $name = $name->getColumnName();
  259. } else if($normalize) {
  260. $name = ColumnMap::normalizeName($name);
  261. }
  262. return isset($this->columns[$name]);
  263. }
  264. /**
  265. * Get a ColumnMap for the named table.
  266. *
  267. * @param string $name A String with the name of the table.
  268. * @param boolean $normalize Normalize the column name (if column name not like FIRST_NAME)
  269. * @return ColumnMap A ColumnMap.
  270. * @throws PropelException if the column is undefined
  271. */
  272. public function getColumn($name, $normalize = true)
  273. {
  274. if ($normalize) {
  275. $name = ColumnMap::normalizeName($name);
  276. }
  277. if (!$this->containsColumn($name, false)) {
  278. throw new PropelException("Cannot fetch ColumnMap for undefined column: " . $name);
  279. }
  280. return $this->columns[$name];
  281. }
  282. /**
  283. * Get a ColumnMap[] of the columns in this table.
  284. *
  285. * @return array A ColumnMap[].
  286. */
  287. public function getColumns()
  288. {
  289. return $this->columns;
  290. }
  291. /**
  292. * Add a primary key column to this Table.
  293. *
  294. * @param string $columnName A String with the column name.
  295. * @param string $type A string specifying the Propel type.
  296. * @param boolean $isNotNull Whether column does not allow NULL values.
  297. * @param $size An int specifying the size.
  298. * @return ColumnMap Newly added PrimaryKey column.
  299. */
  300. public function addPrimaryKey($columnName, $phpName, $type, $isNotNull = false, $size = null, $defaultValue = null)
  301. {
  302. return $this->addColumn($columnName, $phpName, $type, $isNotNull, $size, $defaultValue, true, null, null);
  303. }
  304. /**
  305. * Add a foreign key column to the table.
  306. *
  307. * @param string $columnName A String with the column name.
  308. * @param string $type A string specifying the Propel type.
  309. * @param string $fkTable A String with the foreign key table name.
  310. * @param string $fkColumn A String with the foreign key column name.
  311. * @param boolean $isNotNull Whether column does not allow NULL values.
  312. * @param int $size An int specifying the size.
  313. * @param string $defaultValue The default value for this column.
  314. * @return ColumnMap Newly added ForeignKey column.
  315. */
  316. public function addForeignKey($columnName, $phpName, $type, $fkTable, $fkColumn, $isNotNull = false, $size = 0, $defaultValue = null)
  317. {
  318. return $this->addColumn($columnName, $phpName, $type, $isNotNull, $size, $defaultValue, false, $fkTable, $fkColumn);
  319. }
  320. /**
  321. * Add a foreign primary key column to the table.
  322. *
  323. * @param string $columnName A String with the column name.
  324. * @param string $type A string specifying the Propel type.
  325. * @param string $fkTable A String with the foreign key table name.
  326. * @param string $fkColumn A String with the foreign key column name.
  327. * @param boolean $isNotNull Whether column does not allow NULL values.
  328. * @param int $size An int specifying the size.
  329. * @param string $defaultValue The default value for this column.
  330. * @return ColumnMap Newly created foreign pkey column.
  331. */
  332. public function addForeignPrimaryKey($columnName, $phpName, $type, $fkTable, $fkColumn, $isNotNull = false, $size = 0, $defaultValue = null)
  333. {
  334. return $this->addColumn($columnName, $phpName, $type, $isNotNull, $size, $defaultValue, true, $fkTable, $fkColumn);
  335. }
  336. /**
  337. * Returns array of ColumnMap objects that make up the primary key for this table
  338. *
  339. * @return array ColumnMap[]
  340. */
  341. public function getPrimaryKeys()
  342. {
  343. return $this->primaryKeys;
  344. }
  345. /**
  346. * Returns array of ColumnMap objects that are foreign keys for this table
  347. *
  348. * @return array ColumnMap[]
  349. */
  350. public function getForeignKeys()
  351. {
  352. return $this->foreignKeys;
  353. }
  354. /**
  355. * Add a validator to a table's column
  356. *
  357. * @param string $columnName The name of the validator's column
  358. * @param string $name The rule name of this validator
  359. * @param string $classname The dot-path name of class to use (e.g. myapp.propel.MyValidator)
  360. * @param string $value
  361. * @param string $message The error message which is returned on invalid values
  362. * @return void
  363. */
  364. public function addValidator($columnName, $name, $classname, $value, $message)
  365. {
  366. if (false !== ($pos = strpos($columnName, '.'))) {
  367. $columnName = substr($columnName, $pos + 1);
  368. }
  369. $col = $this->getColumn($columnName);
  370. if ($col !== null) {
  371. $validator = new ValidatorMap($col);
  372. $validator->setName($name);
  373. $validator->setClass($classname);
  374. $validator->setValue($value);
  375. $validator->setMessage($message);
  376. $col->addValidator($validator);
  377. }
  378. }
  379. /**
  380. * Build relations
  381. * Relations are lazy loaded for performance reasons
  382. * This method should be overridden by descendents
  383. */
  384. public function buildRelations()
  385. {
  386. }
  387. /**
  388. * Adds a RelationMap to the table
  389. *
  390. * @param string $name The relation name
  391. * @param string $tablePhpName The related table name
  392. * @param integer $type The relation type (either RelationMap::MANY_TO_ONE, RelationMap::ONE_TO_MANY, or RelationMAp::ONE_TO_ONE)
  393. * @param array $columnMapping An associative array mapping column names (local => foreign)
  394. * @return RelationMap the built RelationMap object
  395. */
  396. public function addRelation($name, $tablePhpName, $type, $columnMapping = array(), $onDelete = null, $onUpdate = null)
  397. {
  398. // note: using phpName for the second table allows the use of DatabaseMap::getTableByPhpName()
  399. // and this method autoloads the TableMap if the table isn't loaded yet
  400. $relation = new RelationMap($name);
  401. $relation->setType($type);
  402. $relation->setOnUpdate($onUpdate);
  403. $relation->setOnDelete($onDelete);
  404. // set tables
  405. if ($type == RelationMap::MANY_TO_ONE) {
  406. $relation->setLocalTable($this);
  407. $relation->setForeignTable($this->dbMap->getTableByPhpName($tablePhpName));
  408. } else {
  409. $relation->setLocalTable($this->dbMap->getTableByPhpName($tablePhpName));
  410. $relation->setForeignTable($this);
  411. $columnMapping = array_flip($columnMapping);
  412. }
  413. // set columns
  414. foreach ($columnMapping as $key => $value)
  415. {
  416. $relation->addColumnMapping(
  417. $relation->getLocalTable()->getColumn($key),
  418. $relation->getForeignTable()->getColumn($value)
  419. );
  420. }
  421. $this->relations[$name] = $relation;
  422. return $relation;
  423. }
  424. /**
  425. * Gets a RelationMap of the table by relation name
  426. *
  427. * @param String $name The relation name
  428. * @return boolean true if the relation exists
  429. */
  430. public function hasRelation($name)
  431. {
  432. return array_key_exists($name, $this->getRelations());
  433. }
  434. /**
  435. * Gets a RelationMap of the table by relation name
  436. *
  437. * @param String $name The relation name
  438. * @return RelationMap The relation object
  439. * @throws PropelException When called on an inexistent relation
  440. */
  441. public function getRelation($name)
  442. {
  443. if (!array_key_exists($name, $this->getRelations()))
  444. {
  445. throw new PropelException('Calling getRelation() on an unknown relation, ' . $name);
  446. }
  447. return $this->relations[$name];
  448. }
  449. /**
  450. * Gets the RelationMap objects of the table
  451. * This method will build the relations if they are not built yet
  452. *
  453. * @return Array list of RelationMap objects
  454. */
  455. public function getRelations()
  456. {
  457. if(!$this->relationsBuilt)
  458. {
  459. $this->buildRelations();
  460. $this->relationsBuilt = true;
  461. }
  462. return $this->relations;
  463. }
  464. /**
  465. *
  466. * Gets the list of behaviors registered for this table
  467. *
  468. * @return array
  469. */
  470. public function getBehaviors()
  471. {
  472. return array();
  473. }
  474. // Deprecated methods and attributres, to be removed
  475. /**
  476. * Does this table contain the specified column?
  477. *
  478. * @deprecated Use hasColumn instead
  479. * @param mixed $name name of the column or ColumnMap instance
  480. * @param boolean $normalize Normalize the column name (if column name not like FIRST_NAME)
  481. * @return boolean True if the table contains the column.
  482. */
  483. public function containsColumn($name, $normalize = true)
  484. {
  485. return $this->hasColumn($name, $normalize);
  486. }
  487. /**
  488. * Normalizes the column name, removing table prefix and uppercasing.
  489. * article.first_name becomes FIRST_NAME
  490. *
  491. * @deprecated Use ColumnMap::normalizeColumName() instead
  492. * @param string $name
  493. * @return string Normalized column name.
  494. */
  495. protected function normalizeColName($name)
  496. {
  497. return ColumnMap::normalizeName($name);
  498. }
  499. /**
  500. * Returns array of ColumnMap objects that make up the primary key for this table.
  501. *
  502. * @deprecated Use getPrimaryKeys instead
  503. * @return array ColumnMap[]
  504. */
  505. public function getPrimaryKeyColumns()
  506. {
  507. return array_values($this->primaryKeys);
  508. }
  509. //---Utility methods for doing intelligent lookup of table names
  510. /**
  511. * The prefix on the table name.
  512. * @deprecated Not used anywhere in Propel
  513. */
  514. private $prefix;
  515. /**
  516. * Get table prefix name.
  517. *
  518. * @deprecated Not used anywhere in Propel
  519. * @return string A String with the prefix.
  520. */
  521. public function getPrefix()
  522. {
  523. return $this->prefix;
  524. }
  525. /**
  526. * Set table prefix name.
  527. *
  528. * @deprecated Not used anywhere in Propel
  529. * @param string $prefix The prefix for the table name (ie: SCARAB for
  530. * SCARAB_PROJECT).
  531. * @return void
  532. */
  533. public function setPrefix($prefix)
  534. {
  535. $this->prefix = $prefix;
  536. }
  537. /**
  538. * Tell me if i have PREFIX in my string.
  539. *
  540. * @deprecated Not used anywhere in Propel
  541. * @param data A String.
  542. * @return boolean True if prefix is contained in data.
  543. */
  544. protected function hasPrefix($data)
  545. {
  546. return (strpos($data, $this->prefix) === 0);
  547. }
  548. /**
  549. * Removes the PREFIX if found
  550. *
  551. * @deprecated Not used anywhere in Propel
  552. * @param string $data A String.
  553. * @return string A String with data, but with prefix removed.
  554. */
  555. protected function removePrefix($data)
  556. {
  557. return $this->hasPrefix($data) ? substr($data, strlen($this->prefix)) : $data;
  558. }
  559. /**
  560. * Removes the PREFIX, removes the underscores and makes
  561. * first letter caps.
  562. *
  563. * SCARAB_FOO_BAR becomes FooBar.
  564. *
  565. * @deprecated Not used anywhere in Propel. At buildtime, use Column::generatePhpName() for that purpose
  566. * @param data A String.
  567. * @return string A String with data processed.
  568. */
  569. public final function removeUnderScores($data)
  570. {
  571. $out = '';
  572. $tmp = $this->removePrefix($data);
  573. $tok = strtok($tmp, '_');
  574. while ($tok) {
  575. $out .= ucfirst($tok);
  576. $tok = strtok('_');
  577. }
  578. return $out;
  579. }
  580. /**
  581. * Makes the first letter caps and the rest lowercase.
  582. *
  583. * @deprecated Not used anywhere in Propel.
  584. * @param string $data A String.
  585. * @return string A String with data processed.
  586. */
  587. private function firstLetterCaps($data)
  588. {
  589. return(ucfirst(strtolower($data)));
  590. }
  591. }