PageRenderTime 37ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/www/application/modules/shop/classes/propel/map/ColumnMap.php

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