PageRenderTime 63ms CodeModel.GetById 35ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/ealexandru/jobeet
PHP | 475 lines | 196 code | 51 blank | 228 comment | 59 complexity | 26b9eeb8c18bde26fa381ac5394117d2 MD5 | raw file
Possible License(s): ISC, AGPL-3.0, LGPL-2.1, BSD-3-Clause, LGPL-3.0
  1. <?php
  2. /*
  3. * $Id: ColumnMap.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. * ColumnMap is used to model a column of 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. * @version $Revision: 1262 $
  34. * @package propel.map
  35. */
  36. class ColumnMap {
  37. // Propel type of the column
  38. protected $type;
  39. // Size of the column
  40. protected $size = 0;
  41. // Is it a primary key?
  42. protected $pk = false;
  43. // Is null value allowed?
  44. protected $notNull = false;
  45. // The default value for this column
  46. protected $defaultValue;
  47. // Name of the table that this column is related to
  48. protected $relatedTableName = "";
  49. // Name of the column that this column is related to
  50. protected $relatedColumnName = "";
  51. // The TableMap for this column
  52. protected $table;
  53. // The name of the column
  54. protected $columnName;
  55. // The php name of the column
  56. protected $phpName;
  57. // The validators for this column
  58. protected $validators = array();
  59. /**
  60. * Constructor.
  61. *
  62. * @param string $name The name of the column.
  63. * @param TableMap containingTable TableMap of the table this column is in.
  64. */
  65. public function __construct($name, TableMap $containingTable)
  66. {
  67. $this->columnName = $name;
  68. $this->table = $containingTable;
  69. }
  70. /**
  71. * Get the name of a column.
  72. *
  73. * @return string A String with the column name.
  74. */
  75. public function getName()
  76. {
  77. return $this->columnName;
  78. }
  79. /**
  80. * Get the table map this column belongs to.
  81. * @return TableMap
  82. */
  83. public function getTable()
  84. {
  85. return $this->table;
  86. }
  87. /**
  88. * Get the name of the table this column is in.
  89. *
  90. * @return string A String with the table name.
  91. */
  92. public function getTableName()
  93. {
  94. return $this->table->getName();
  95. }
  96. /**
  97. * Get the table name + column name.
  98. *
  99. * @return string A String with the full column name.
  100. */
  101. public function getFullyQualifiedName()
  102. {
  103. return $this->getTableName() . "." . $this->columnName;
  104. }
  105. /**
  106. * Set the php anme of this column.
  107. *
  108. * @param string $phpName A string representing the PHP name.
  109. * @return void
  110. */
  111. public function setPhpName($phpName)
  112. {
  113. $this->phpName = $phpName;
  114. }
  115. /**
  116. * Get the name of a column.
  117. *
  118. * @return string A String with the column name.
  119. */
  120. public function getPhpName()
  121. {
  122. return $this->phpName;
  123. }
  124. /**
  125. * Set the Propel type of this column.
  126. *
  127. * @param string $type A string representing the Propel type (e.g. PropelColumnTypes::DATE).
  128. * @return void
  129. */
  130. public function setType($type)
  131. {
  132. $this->type = $type;
  133. }
  134. /**
  135. * Get the Propel type of this column.
  136. *
  137. * @return string A string representing the Propel type (e.g. PropelColumnTypes::DATE).
  138. */
  139. public function getType()
  140. {
  141. return $this->type;
  142. }
  143. /**
  144. * Get the PDO type of this column.
  145. *
  146. * @return int The PDO::PARMA_* value
  147. */
  148. public function getPdoType()
  149. {
  150. return PropelColumnTypes::getPdoType($this->type);
  151. }
  152. /**
  153. * Whether this is a BLOB, LONGVARBINARY, or VARBINARY.
  154. * @return boolean
  155. */
  156. public function isLob()
  157. {
  158. return ($this->type == PropelColumnTypes::BLOB || $this->type == PropelColumnTypes::VARBINARY || $this->type == PropelColumnTypes::LONGVARBINARY);
  159. }
  160. /**
  161. * Whether this is a DATE/TIME/TIMESTAMP column.
  162. *
  163. * @return boolean
  164. * @since 1.3
  165. */
  166. public function isTemporal()
  167. {
  168. return ($this->type == PropelColumnTypes::TIMESTAMP || $this->type == PropelColumnTypes::DATE || $this->type == PropelColumnTypes::TIME || $this->type == PropelColumnTypes::BU_DATE || $this->type == PropelColumnTypes::BU_TIMESTAMP);
  169. }
  170. /**
  171. * Whether this is a DATE/TIME/TIMESTAMP column that is post-epoch (1970).
  172. *
  173. * PHP cannot handle pre-epoch timestamps well -- hence the need to differentiate
  174. * between epoch and pre-epoch timestamps.
  175. *
  176. * @return boolean
  177. * @deprecated Propel supports non-epoch dates
  178. */
  179. public function isEpochTemporal()
  180. {
  181. return ($this->type == PropelColumnTypes::TIMESTAMP || $this->type == PropelColumnTypes::DATE || $this->type == PropelColumnTypes::TIME);
  182. }
  183. /**
  184. * Whether this column is numeric (int, decimal, bigint etc).
  185. * @return boolean
  186. */
  187. public function isNumeric()
  188. {
  189. return ($this->type == PropelColumnTypes::NUMERIC || $this->type == PropelColumnTypes::DECIMAL || $this->type == PropelColumnTypes::TINYINT || $this->type == PropelColumnTypes::SMALLINT || $this->type == PropelColumnTypes::INTEGER || $this->type == PropelColumnTypes::BIGINT || $this->type == PropelColumnTypes::REAL || $this->type == PropelColumnTypes::FLOAT || $this->type == PropelColumnTypes::DOUBLE);
  190. }
  191. /**
  192. * Whether this column is a text column (varchar, char, longvarchar).
  193. * @return boolean
  194. */
  195. public function isText()
  196. {
  197. return ($this->type == PropelColumnTypes::VARCHAR || $this->type == PropelColumnTypes::LONGVARCHAR || $this->type == PropelColumnTypes::CHAR);
  198. }
  199. /**
  200. * Set the size of this column.
  201. *
  202. * @param int $size An int specifying the size.
  203. * @return void
  204. */
  205. public function setSize($size)
  206. {
  207. $this->size = $size;
  208. }
  209. /**
  210. * Get the size of this column.
  211. *
  212. * @return int An int specifying the size.
  213. */
  214. public function getSize()
  215. {
  216. return $this->size;
  217. }
  218. /**
  219. * Set if this column is a primary key or not.
  220. *
  221. * @param boolean $pk True if column is a primary key.
  222. * @return void
  223. */
  224. public function setPrimaryKey($pk)
  225. {
  226. $this->pk = $pk;
  227. }
  228. /**
  229. * Is this column a primary key?
  230. *
  231. * @return boolean True if column is a primary key.
  232. */
  233. public function isPrimaryKey()
  234. {
  235. return $this->pk;
  236. }
  237. /**
  238. * Set if this column may be null.
  239. *
  240. * @param boolean nn True if column may be null.
  241. * @return void
  242. */
  243. public function setNotNull($nn)
  244. {
  245. $this->notNull = $nn;
  246. }
  247. /**
  248. * Is null value allowed ?
  249. *
  250. * @return boolean True if column may not be null.
  251. */
  252. public function isNotNull()
  253. {
  254. return ($this->notNull || $this->isPrimaryKey());
  255. }
  256. /**
  257. * Sets the default value for this column.
  258. * @param mixed $defaultValue the default value for the column
  259. * @return void
  260. */
  261. public function setDefaultValue($defaultValue)
  262. {
  263. $this->defaultValue = $defaultValue;
  264. }
  265. /**
  266. * Gets the default value for this column.
  267. * @return mixed String or NULL
  268. */
  269. public function getDefaultValue()
  270. {
  271. return $this->defaultValue;
  272. }
  273. /**
  274. * Set the foreign key for this column.
  275. *
  276. * @param string tableName The name of the table that is foreign.
  277. * @param string columnName The name of the column that is foreign.
  278. * @return void
  279. */
  280. public function setForeignKey($tableName, $columnName)
  281. {
  282. if ($tableName && $columnName) {
  283. $this->relatedTableName = $tableName;
  284. $this->relatedColumnName = $columnName;
  285. } else {
  286. $this->relatedTableName = "";
  287. $this->relatedColumnName = "";
  288. }
  289. }
  290. /**
  291. * Is this column a foreign key?
  292. *
  293. * @return boolean True if column is a foreign key.
  294. */
  295. public function isForeignKey()
  296. {
  297. if ($this->relatedTableName) {
  298. return true;
  299. } else {
  300. return false;
  301. }
  302. }
  303. /**
  304. * Get the RelationMap object for this foreign key
  305. */
  306. public function getRelation()
  307. {
  308. if(!$this->relatedTableName) return null;
  309. foreach ($this->getTable()->getRelations() as $name => $relation)
  310. {
  311. if($relation->getType() == RelationMap::MANY_TO_ONE)
  312. {
  313. if ($relation->getForeignTable()->getName() == $this->getRelatedTableName()
  314. && array_key_exists($this->getFullyQualifiedName(), $relation->getColumnMappings()))
  315. {
  316. return $relation;
  317. }
  318. }
  319. }
  320. }
  321. /**
  322. * Get the table.column that this column is related to.
  323. *
  324. * @return string A String with the full name for the related column.
  325. */
  326. public function getRelatedName()
  327. {
  328. return $this->relatedTableName . "." . $this->relatedColumnName;
  329. }
  330. /**
  331. * Get the table name that this column is related to.
  332. *
  333. * @return string A String with the name for the related table.
  334. */
  335. public function getRelatedTableName()
  336. {
  337. return $this->relatedTableName;
  338. }
  339. /**
  340. * Get the column name that this column is related to.
  341. *
  342. * @return string A String with the name for the related column.
  343. */
  344. public function getRelatedColumnName()
  345. {
  346. return $this->relatedColumnName;
  347. }
  348. /**
  349. * Get the TableMap object that this column is related to.
  350. *
  351. * @return TableMap The related TableMap object
  352. * @throws PropelException when called on a column with no foreign key
  353. */
  354. public function getRelatedTable()
  355. {
  356. if ($this->relatedTableName) {
  357. return $this->table->getDatabaseMap()->getTable($this->relatedTableName);
  358. } else {
  359. throw new PropelException("Cannot fetch RelatedTable for column with no foreign key: " . $this->columnName);
  360. }
  361. }
  362. /**
  363. * Get the TableMap object that this column is related to.
  364. *
  365. * @return ColumnMap The related ColumnMap object
  366. * @throws PropelException when called on a column with no foreign key
  367. */
  368. public function getRelatedColumn()
  369. {
  370. return $this->getRelatedTable()->getColumn($this->relatedColumnName);
  371. }
  372. public function addValidator($validator)
  373. {
  374. $this->validators[] = $validator;
  375. }
  376. public function hasValidators()
  377. {
  378. return count($this->validators) > 0;
  379. }
  380. public function getValidators()
  381. {
  382. return $this->validators;
  383. }
  384. /**
  385. * Performs DB-specific ignore case, but only if the column type necessitates it.
  386. * @param string $str The expression we want to apply the ignore case formatting to (e.g. the column name).
  387. * @param DBAdapter $db
  388. */
  389. public function ignoreCase($str, DBAdapter $db)
  390. {
  391. if ($this->isText()) {
  392. return $db->ignoreCase($str);
  393. } else {
  394. return $str;
  395. }
  396. }
  397. /**
  398. * Normalizes the column name, removing table prefix and uppercasing.
  399. *
  400. * article.first_name becomes FIRST_NAME
  401. *
  402. * @param string $name
  403. * @return string Normalized column name.
  404. */
  405. public static function normalizeName($name)
  406. {
  407. if (false !== ($pos = strpos($name, '.'))) {
  408. $name = substr($name, $pos + 1);
  409. }
  410. $name = strtoupper($name);
  411. return $name;
  412. }
  413. // deprecated methods
  414. /**
  415. * Gets column name
  416. * @deprecated Use getName() instead
  417. * @return string
  418. * @deprecated Use getName() instead.
  419. */
  420. public function getColumnName()
  421. {
  422. return $this->getName();
  423. }
  424. }