PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/vendor/symfony/lib/plugins/sfPropelPlugin/lib/vendor/propel-generator/classes/propel/engine/database/model/Column.php

https://github.com/jczulian/blogito
PHP | 1179 lines | 695 code | 119 blank | 365 comment | 67 complexity | 89568a484a96fa0de3441d5cbf3f1ee3 MD5 | raw file
Possible License(s): AGPL-3.0, LGPL-2.1, BSD-3-Clause, LGPL-3.0, ISC
  1. <?php
  2. /*
  3. * $Id: Column.php 1295 2009-11-07 23:22:15Z 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. require_once 'propel/engine/database/model/XMLElement.php';
  22. include_once 'propel/engine/EngineException.php';
  23. include_once 'propel/engine/database/model/PropelTypes.php';
  24. include_once 'propel/engine/database/model/Inheritance.php';
  25. include_once 'propel/engine/database/model/Domain.php';
  26. include_once 'propel/engine/database/model/ColumnDefaultValue.php';
  27. /**
  28. * A Class for holding data about a column used in an Application.
  29. *
  30. * @author Hans Lellelid <hans@xmpl.org> (Propel)
  31. * @author Leon Messerschmidt <leon@opticode.co.za> (Torque)
  32. * @author Jason van Zyl <jvanzyl@apache.org> (Torque)
  33. * @author Jon S. Stevens <jon@latchkey.com> (Torque)
  34. * @author Daniel Rall <dlr@finemaltcoding.com> (Torque)
  35. * @author Byron Foster <byron_foster@yahoo.com> (Torque)
  36. * @author Bernd Goldschmidt <bgoldschmidt@rapidsoft.de>
  37. * @version $Revision: 1295 $
  38. * @package propel.engine.database.model
  39. */
  40. class Column extends XMLElement {
  41. const DEFAULT_TYPE = "VARCHAR";
  42. const DEFAULT_VISIBILITY = 'public';
  43. public static $valid_visibilities = array('public', 'protected', 'private');
  44. private $name;
  45. private $description;
  46. private $phpName = null;
  47. private $phpNamingMethod;
  48. private $isNotNull = false;
  49. private $size;
  50. private $namePrefix;
  51. private $accessorVisibility;
  52. private $mutatorVisibility;
  53. /**
  54. * The name to use for the Peer constant that identifies this column.
  55. * (Will be converted to all-uppercase in the templates.)
  56. * @var string
  57. */
  58. private $peerName;
  59. /**
  60. * Native PHP type (scalar or class name)
  61. * @var string "string", "boolean", "int", "double"
  62. */
  63. private $phpType;
  64. /**
  65. * @var Table
  66. */
  67. private $parentTable;
  68. private $position;
  69. private $isPrimaryKey = false;
  70. private $isNodeKey = false;
  71. private $nodeKeySep;
  72. private $isNestedSetLeftKey = false;
  73. private $isNestedSetRightKey = false;
  74. private $isTreeScopeKey = false;
  75. private $isUnique = false;
  76. private $isAutoIncrement = false;
  77. private $isLazyLoad = false;
  78. private $defaultValue;
  79. private $referrers;
  80. private $isPrimaryString = false;
  81. // only one type is supported currently, which assumes the
  82. // column either contains the classnames or a key to
  83. // classnames specified in the schema. Others may be
  84. // supported later.
  85. private $inheritanceType;
  86. private $isInheritance;
  87. private $isEnumeratedClasses;
  88. private $inheritanceList;
  89. private $needsTransactionInPostgres; //maybe this can be retrieved from vendorSpecificInfo
  90. /** class name to do input validation on this column */
  91. private $inputValidator = null;
  92. /**
  93. * @var Domain The domain object associated with this Column.
  94. */
  95. private $domain;
  96. /**
  97. * Creates a new column and set the name
  98. *
  99. * @param name column name
  100. */
  101. public function __construct($name = null)
  102. {
  103. $this->name = $name;
  104. }
  105. /**
  106. * Return a comma delimited string listing the specified columns.
  107. *
  108. * @param columns Either a list of <code>Column</code> objects, or
  109. * a list of <code>String</code> objects with column names.
  110. * @deprecated Use the DDLBuilder->getColumnList() method instead; this will be removed in 1.3
  111. */
  112. public static function makeList($columns, Platform $platform)
  113. {
  114. $list = array();
  115. foreach ($columns as $col) {
  116. if ($col instanceof Column) {
  117. $col = $col->getName();
  118. }
  119. $list[] = $platform->quoteIdentifier($col);
  120. }
  121. return implode(", ", $list);
  122. }
  123. /**
  124. * Sets up the Column object based on the attributes that were passed to loadFromXML().
  125. * @see parent::loadFromXML()
  126. */
  127. protected function setupObject()
  128. {
  129. try {
  130. $dom = $this->getAttribute("domain");
  131. if ($dom) {
  132. $this->getDomain()->copy($this->getTable()->getDatabase()->getDomain($dom));
  133. } else {
  134. $type = strtoupper($this->getAttribute("type"));
  135. if ($type) {
  136. $this->getDomain()->copy($this->getPlatform()->getDomainForType($type));
  137. } else {
  138. $this->getDomain()->copy($this->getPlatform()->getDomainForType(self::DEFAULT_TYPE));
  139. }
  140. }
  141. $this->name = $this->getAttribute("name");
  142. $this->phpName = $this->getAttribute("phpName");
  143. $this->phpType = $this->getAttribute("phpType");
  144. if ($this->getAttribute("prefix", null) !== null) {
  145. $this->namePrefix = $this->getAttribute("prefix");
  146. } elseif ($this->getTable()->getAttribute('columnPrefix', null) !== null) {
  147. $this->namePrefix = $this->getTable()->getAttribute('columnPrefix');
  148. } else {
  149. $this->namePrefix = '';
  150. }
  151. // Accessor visibility
  152. if ($this->getAttribute('accessorVisibility', null) !== null) {
  153. $this->setAccessorVisibility($this->getAttribute('accessorVisibility'));
  154. } elseif ($this->getTable()->getAttribute('defaultAccessorVisibility', null) !== null) {
  155. $this->setAccessorVisibility($this->getTable()->getAttribute('defaultAccessorVisibility'));
  156. } elseif ($this->getTable()->getDatabase()->getAttribute('defaultAccessorVisibility', null) !== null) {
  157. $this->setAccessorVisibility($this->getTable()->getDatabase()->getAttribute('defaultAccessorVisibility'));
  158. } else {
  159. $this->setAccessorVisibility(self::DEFAULT_VISIBILITY);
  160. }
  161. // Mutator visibility
  162. if ($this->getAttribute('mutatorVisibility', null) !== null) {
  163. $this->setMutatorVisibility($this->getAttribute('mutatorVisibility'));
  164. } elseif ($this->getTable()->getAttribute('defaultMutatorVisibility', null) !== null) {
  165. $this->setMutatorVisibility($this->getTable()->getAttribute('defaultMutatorVisibility'));
  166. } elseif ($this->getTable()->getDatabase()->getAttribute('defaultMutatorVisibility', null) !== null) {
  167. $this->setMutatorVisibility($this->getTable()->getDatabase()->getAttribute('defaultMutatorVisibility'));
  168. } else {
  169. $this->setMutatorVisibility(self::DEFAULT_VISIBILITY);
  170. }
  171. $this->peerName = $this->getAttribute("peerName");
  172. // retrieves the method for converting from specified name to a PHP name, defaulting to parent tables default method
  173. $this->phpNamingMethod = $this->getAttribute("phpNamingMethod", $this->parentTable->getDatabase()->getDefaultPhpNamingMethod());
  174. $this->isPrimaryString = $this->booleanValue($this->getAttribute("primaryString"));
  175. $this->isPrimaryKey = $this->booleanValue($this->getAttribute("primaryKey"));
  176. $this->isNodeKey = $this->booleanValue($this->getAttribute("nodeKey"));
  177. $this->nodeKeySep = $this->getAttribute("nodeKeySep", ".");
  178. $this->isNestedSetLeftKey = $this->booleanValue($this->getAttribute("nestedSetLeftKey"));
  179. $this->isNestedSetRightKey = $this->booleanValue($this->getAttribute("nestedSetRightKey"));
  180. $this->isTreeScopeKey = $this->booleanValue($this->getAttribute("treeScopeKey"));
  181. $this->isNotNull = ($this->booleanValue($this->getAttribute("required"), false) || $this->isPrimaryKey); // primary keys are required
  182. //AutoIncrement/Sequences
  183. $this->isAutoIncrement = $this->booleanValue($this->getAttribute("autoIncrement"));
  184. $this->isLazyLoad = $this->booleanValue($this->getAttribute("lazyLoad"));
  185. // Add type, size information to associated Domain object
  186. $this->getDomain()->replaceSqlType($this->getAttribute("sqlType"));
  187. $this->getDomain()->replaceSize($this->getAttribute("size", $type == 'VARCHAR' ? 255 : null));
  188. $this->getDomain()->replaceScale($this->getAttribute("scale"));
  189. $defval = $this->getAttribute("defaultValue", $this->getAttribute("default"));
  190. if ($defval !== null && strtolower($defval) !== 'null') {
  191. $this->getDomain()->setDefaultValue(new ColumnDefaultValue($defval, ColumnDefaultValue::TYPE_VALUE));
  192. } elseif ($this->getAttribute("defaultExpr") !== null) {
  193. $this->getDomain()->setDefaultValue(new ColumnDefaultValue($this->getAttribute("defaultExpr"), ColumnDefaultValue::TYPE_EXPR));
  194. }
  195. $this->inheritanceType = $this->getAttribute("inheritance");
  196. $this->isInheritance = ($this->inheritanceType !== null
  197. && $this->inheritanceType !== "false"); // here we are only checking for 'false', so don't
  198. // use boleanValue()
  199. $this->inputValidator = $this->getAttribute("inputValidator");
  200. $this->description = $this->getAttribute("description");
  201. } catch (Exception $e) {
  202. throw new EngineException("Error setting up column " . var_export($this->getAttribute("name"), true) . ": " . $e->getMessage());
  203. }
  204. }
  205. /**
  206. * Gets domain for this column, creating a new empty domain object if none is set.
  207. * @return Domain
  208. */
  209. public function getDomain()
  210. {
  211. if ($this->domain === null) {
  212. $this->domain = new Domain();
  213. }
  214. return $this->domain;
  215. }
  216. /**
  217. * Returns table.column
  218. */
  219. public function getFullyQualifiedName()
  220. {
  221. return ($this->parentTable->getName() . '.' . name);
  222. }
  223. /**
  224. * Get the name of the column
  225. */
  226. public function getName()
  227. {
  228. return $this->name;
  229. }
  230. /**
  231. * Set the name of the column
  232. */
  233. public function setName($newName)
  234. {
  235. $this->name = $newName;
  236. }
  237. /**
  238. * Get the description for the Table
  239. */
  240. public function getDescription()
  241. {
  242. return $this->description;
  243. }
  244. /**
  245. * Set the description for the Table
  246. *
  247. * @param newDescription description for the Table
  248. */
  249. public function setDescription($newDescription)
  250. {
  251. $this->description = $newDescription;
  252. }
  253. /**
  254. * Get name to use in PHP sources. It will set & return
  255. * a self-generated phpName from it's name if it's
  256. * not already set.
  257. * @return string
  258. */
  259. public function getPhpName()
  260. {
  261. if ($this->phpName === null) {
  262. $this->setPhpName();
  263. }
  264. return $this->phpName;
  265. }
  266. /**
  267. * Set name to use in PHP sources.
  268. *
  269. * It will generate a phpName from it's name if no
  270. * $phpName is passed.
  271. *
  272. * @param String $phpName PhpName to be set
  273. */
  274. public function setPhpName($phpName = null)
  275. {
  276. if ($phpName == null) {
  277. $this->phpName = self::generatePhpName($this->name, $this->phpNamingMethod, $this->namePrefix);
  278. } else {
  279. $this->phpName = $phpName;
  280. }
  281. }
  282. /**
  283. * Get studly version of PHP name.
  284. *
  285. * The studly name is the PHP name with the first character lowercase.
  286. *
  287. * @return string
  288. */
  289. public function getStudlyPhpName()
  290. {
  291. $phpname = $this->getPhpName();
  292. if (strlen($phpname) > 1) {
  293. return strtolower(substr($phpname, 0, 1)) . substr($phpname, 1);
  294. } else { // 0 or 1 chars (I suppose that's rare)
  295. return strtolower($phpname);
  296. }
  297. }
  298. /**
  299. * Get the visibility of the accessors of this column / attribute
  300. * @return string
  301. */
  302. public function getAccessorVisibility() {
  303. if ($this->accessorVisibility !== null) {
  304. return $this->accessorVisibility;
  305. } else {
  306. return self::DEFAULT_VISIBILITY;
  307. }
  308. }
  309. /**
  310. * Set the visibility of the accessor methods for this column / attribute
  311. * @param $newVisibility string
  312. */
  313. public function setAccessorVisibility($newVisibility) {
  314. if (in_array($newVisibility, self::$valid_visibilities)) {
  315. $this->accessorVisibility = $newVisibility;
  316. } else {
  317. $this->accessorVisibility = self::DEFAULT_VISIBILITY;
  318. }
  319. }
  320. /**
  321. * Get the visibility of the mutator of this column / attribute
  322. * @return string
  323. */
  324. public function getMutatorVisibility() {
  325. if ($this->mutatorVisibility !== null) {
  326. return $this->mutatorVisibility;
  327. } else {
  328. return self::DEFAULT_VISIBILITY;
  329. }
  330. }
  331. /**
  332. * Set the visibility of the mutator methods for this column / attribute
  333. * @param $newVisibility string
  334. */
  335. public function setMutatorVisibility($newVisibility) {
  336. if (in_array($newVisibility, self::$valid_visibilities)) {
  337. $this->mutatorVisibility = $newVisibility;
  338. } else {
  339. $this->mutatorVisibility = self::DEFAULT_VISIBILITY;
  340. }
  341. }
  342. /**
  343. * Get the column constant name (e.g. PeerName::COLUMN_NAME).
  344. *
  345. * @return string A column constant name for insertion into PHP code
  346. */
  347. public function getConstantName()
  348. {
  349. $classname = $this->getTable()->getPhpName() . 'Peer';
  350. // was it overridden in schema.xml ?
  351. if ($this->getPeerName()) {
  352. $const = strtoupper($this->getPeerName());
  353. } else {
  354. $const = strtoupper($this->getName());
  355. }
  356. return $classname.'::'.$const;
  357. }
  358. /**
  359. * Get the Peer constant name that will identify this column.
  360. * @return string
  361. */
  362. public function getPeerName() {
  363. return $this->peerName;
  364. }
  365. /**
  366. * Set the Peer constant name that will identify this column.
  367. * @param $name string
  368. */
  369. public function setPeerName($name) {
  370. $this->peerName = $name;
  371. }
  372. /**
  373. * Get type to use in PHP sources.
  374. *
  375. * If no type has been specified, then uses results of getPhpNative().
  376. *
  377. * @return string The type name.
  378. * @see getPhpNative()
  379. */
  380. public function getPhpType()
  381. {
  382. if ($this->phpType !== null) {
  383. return $this->phpType;
  384. }
  385. return $this->getPhpNative();
  386. }
  387. /**
  388. * Get the location of this column within the table (one-based).
  389. * @return int value of position.
  390. */
  391. public function getPosition()
  392. {
  393. return $this->position;
  394. }
  395. /**
  396. * Get the location of this column within the table (one-based).
  397. * @param int $v Value to assign to position.
  398. */
  399. public function setPosition($v)
  400. {
  401. $this->position = $v;
  402. }
  403. /**
  404. * Set the parent Table of the column
  405. */
  406. public function setTable(Table $parent)
  407. {
  408. $this->parentTable = $parent;
  409. }
  410. /**
  411. * Get the parent Table of the column
  412. */
  413. public function getTable()
  414. {
  415. return $this->parentTable;
  416. }
  417. /**
  418. * Returns the Name of the table the column is in
  419. */
  420. public function getTableName()
  421. {
  422. return $this->parentTable->getName();
  423. }
  424. /**
  425. * Adds a new inheritance definition to the inheritance list and set the
  426. * parent column of the inheritance to the current column
  427. * @param mixed $inhdata Inheritance or XML data.
  428. */
  429. public function addInheritance($inhdata)
  430. {
  431. if ($inhdata instanceof Inheritance) {
  432. $inh = $inhdata;
  433. $inh->setColumn($this);
  434. if ($this->inheritanceList === null) {
  435. $this->inheritanceList = array();
  436. $this->isEnumeratedClasses = true;
  437. }
  438. $this->inheritanceList[] = $inh;
  439. return $inh;
  440. } else {
  441. $inh = new Inheritance();
  442. $inh->loadFromXML($inhdata);
  443. return $this->addInheritance($inh);
  444. }
  445. }
  446. /**
  447. * Get the inheritance definitions.
  448. */
  449. public function getChildren()
  450. {
  451. return $this->inheritanceList;
  452. }
  453. /**
  454. * Determine if this column is a normal property or specifies a
  455. * the classes that are represented in the table containing this column.
  456. */
  457. public function isInheritance()
  458. {
  459. return $this->isInheritance;
  460. }
  461. /**
  462. * Determine if possible classes have been enumerated in the xml file.
  463. */
  464. public function isEnumeratedClasses()
  465. {
  466. return $this->isEnumeratedClasses;
  467. }
  468. /**
  469. * Return the isNotNull property of the column
  470. */
  471. public function isNotNull()
  472. {
  473. return $this->isNotNull;
  474. }
  475. /**
  476. * Set the isNotNull property of the column
  477. */
  478. public function setNotNull($status)
  479. {
  480. $this->isNotNull = (boolean) $status;
  481. }
  482. /**
  483. * Return NOT NULL String for this column
  484. *
  485. * @return "NOT NULL" if null values are not allowed or an empty string.
  486. */
  487. public function getNotNullString()
  488. {
  489. return $this->getTable()->getDatabase()->getPlatform()->getNullString($this->isNotNull());
  490. }
  491. /**
  492. * Set whether the column is the primary string,
  493. * i.e. whether its value is the default string representation of the table
  494. * @param boolean $v
  495. */
  496. public function setPrimaryString($v)
  497. {
  498. $this->isPrimaryString = (boolean) $v;
  499. }
  500. /**
  501. * Return true if the column is the primary string,
  502. * i.e. if its value is the default string representation of the table
  503. */
  504. public function isPrimaryString()
  505. {
  506. return $this->isPrimaryString;
  507. }
  508. /**
  509. * Set whether the column is a primary key or not.
  510. * @param boolean $v
  511. */
  512. public function setPrimaryKey($v)
  513. {
  514. $this->isPrimaryKey = (boolean) $v;
  515. }
  516. /**
  517. * Return true if the column is a primary key
  518. */
  519. public function isPrimaryKey()
  520. {
  521. return $this->isPrimaryKey;
  522. }
  523. /**
  524. * Set if the column is the node key of a tree
  525. */
  526. public function setNodeKey($nk)
  527. {
  528. $this->isNodeKey = (boolean) $nk;
  529. }
  530. /**
  531. * Return true if the column is a node key of a tree
  532. */
  533. public function isNodeKey()
  534. {
  535. return $this->isNodeKey;
  536. }
  537. /**
  538. * Set if the column is the node key of a tree
  539. */
  540. public function setNodeKeySep($sep)
  541. {
  542. $this->nodeKeySep = (string) $sep;
  543. }
  544. /**
  545. * Return true if the column is a node key of a tree
  546. */
  547. public function getNodeKeySep()
  548. {
  549. return $this->nodeKeySep;
  550. }
  551. /**
  552. * Set if the column is the nested set left key of a tree
  553. */
  554. public function setNestedSetLeftKey($nslk)
  555. {
  556. $this->isNestedSetLeftKey = (boolean) $nslk;
  557. }
  558. /**
  559. * Return true if the column is a nested set key of a tree
  560. */
  561. public function isNestedSetLeftKey()
  562. {
  563. return $this->isNestedSetLeftKey;
  564. }
  565. /**
  566. * Set if the column is the nested set right key of a tree
  567. */
  568. public function setNestedSetRightKey($nsrk)
  569. {
  570. $this->isNestedSetRightKey = (boolean) $nsrk;
  571. }
  572. /**
  573. * Return true if the column is a nested set right key of a tree
  574. */
  575. public function isNestedSetRightKey()
  576. {
  577. return $this->isNestedSetRightKey;
  578. }
  579. /**
  580. * Set if the column is the scope key of a tree
  581. */
  582. public function setTreeScopeKey($tsk)
  583. {
  584. $this->isTreeScopeKey = (boolean) $tsk;
  585. }
  586. /**
  587. * Return true if the column is a scope key of a tree
  588. * @return boolean
  589. */
  590. public function isTreeScopeKey()
  591. {
  592. return $this->isTreeScopeKey;
  593. }
  594. /**
  595. * Set true if the column is UNIQUE
  596. * @param boolean $u
  597. */
  598. public function setUnique($u)
  599. {
  600. $this->isUnique = $u;
  601. }
  602. /**
  603. * Get the UNIQUE property.
  604. * @return boolean
  605. */
  606. public function isUnique()
  607. {
  608. return $this->isUnique;
  609. }
  610. /**
  611. * Return true if the column requires a transaction in Postgres
  612. * @return boolean
  613. */
  614. public function requiresTransactionInPostgres()
  615. {
  616. return $this->needsTransactionInPostgres;
  617. }
  618. /**
  619. * Utility method to determine if this column is a foreign key.
  620. * @return boolean
  621. */
  622. public function isForeignKey()
  623. {
  624. return (count($this->getForeignKeys()) > 0);
  625. }
  626. /**
  627. * Whether this column is a part of more than one foreign key.
  628. * @return boolean
  629. */
  630. public function hasMultipleFK()
  631. {
  632. return (count($this->getForeignKeys()) > 1);
  633. }
  634. /**
  635. * Get the foreign key objects for this column (if it is a foreign key or part of a foreign key)
  636. * @return array
  637. */
  638. public function getForeignKeys()
  639. {
  640. return $this->parentTable->getColumnForeignKeys($this->name);
  641. }
  642. /**
  643. * Adds the foreign key from another table that refers to this column.
  644. */
  645. public function addReferrer(ForeignKey $fk)
  646. {
  647. if ($this->referrers === null) {
  648. $this->referrers = array();
  649. }
  650. $this->referrers[] = $fk;
  651. }
  652. /**
  653. * Get list of references to this column.
  654. */
  655. public function getReferrers()
  656. {
  657. if ($this->referrers === null) {
  658. $this->referrers = array();
  659. }
  660. return $this->referrers;
  661. }
  662. /**
  663. * Sets the domain up for specified Propel type.
  664. *
  665. * Calling this method will implicitly overwrite any previously set type,
  666. * size, scale (or other domain attributes).
  667. *
  668. * @param string $propelType
  669. */
  670. public function setDomainForType($propelType)
  671. {
  672. $this->getDomain()->copy($this->getPlatform()->getDomainForType($propelType));
  673. }
  674. /**
  675. * Sets the propel colunm type.
  676. * @param string $propelType
  677. * @see Domain::setType()
  678. */
  679. public function setType($propelType)
  680. {
  681. $this->getDomain()->setType($propelType);
  682. if ($propelType == PropelTypes::VARBINARY|| $propelType == PropelTypes::LONGVARBINARY || $propelType == PropelTypes::BLOB) {
  683. $this->needsTransactionInPostgres = true;
  684. }
  685. }
  686. /**
  687. * Returns the Propel column type as a string.
  688. * @return string The constant representing Creole type: e.g. "VARCHAR".
  689. * @see Domain::getType()
  690. */
  691. public function getType()
  692. {
  693. return $this->getDomain()->getType();
  694. }
  695. /**
  696. * Returns the column PDO type integer for this column's Propel type.
  697. * @return int The integer value representing PDO type param: e.g. PDO::PARAM_INT
  698. */
  699. public function getPDOType()
  700. {
  701. return PropelTypes::getPDOType($this->getType());
  702. }
  703. /**
  704. * Returns the column type as given in the schema as an object
  705. */
  706. public function getPropelType()
  707. {
  708. return $this->getType();
  709. }
  710. /**
  711. * Utility method to know whether column needs Blob/Lob handling.
  712. * @return boolean
  713. */
  714. public function isLobType()
  715. {
  716. return PropelTypes::isLobType($this->getType());
  717. }
  718. /**
  719. * Utility method to see if the column is text type.
  720. */
  721. public function isTextType()
  722. {
  723. return PropelTypes::isTextType($this->getType());
  724. }
  725. /**
  726. * Utility method to see if the column is numeric type.
  727. * @return boolean
  728. */
  729. public function isNumericType()
  730. {
  731. return PropelTypes::isNumericType($this->getType());
  732. }
  733. /**
  734. * Utility method to know whether column is a temporal column.
  735. * @return boolean
  736. */
  737. public function isTemporalType()
  738. {
  739. return PropelTypes::isTemporalType($this->getType());
  740. }
  741. /**
  742. * @see XMLElement::appendXml(DOMNode)
  743. */
  744. public function appendXml(DOMNode $node)
  745. {
  746. $doc = ($node instanceof DOMDocument) ? $node : $node->ownerDocument;
  747. $colNode = $node->appendChild($doc->createElement('column'));
  748. $colNode->setAttribute('name', $this->name);
  749. if ($this->phpName !== null) {
  750. $colNode->setAttribute('phpName', $this->getPhpName());
  751. }
  752. $colNode->setAttribute('type', $this->getType());
  753. $domain = $this->getDomain();
  754. if ($domain->getSize() !== null) {
  755. $colNode->setAttribute('size', $domain->getSize());
  756. }
  757. if ($domain->getScale() !== null) {
  758. $colNode->setAttribute('scale', $domain->getScale());
  759. }
  760. if ($this->isPrimaryKey) {
  761. $colNode->setAttribute('primaryKey', var_export($this->isPrimaryKey, true));
  762. }
  763. if ($this->isAutoIncrement) {
  764. $colNode->setAttribute('autoIncrement', var_export($this->isAutoIncrement, true));
  765. }
  766. if ($this->isNotNull) {
  767. $colNode->setAttribute('required', 'true');
  768. } else {
  769. $colNode->setAttribute('required', 'false');
  770. }
  771. if ($domain->getDefaultValue() !== null) {
  772. $def = $domain->getDefaultValue();
  773. if ($def->isExpression()) {
  774. $colNode->setAttribute('defaultExpr', $def->getValue());
  775. } else {
  776. $colNode->setAttribute('defaultValue', $def->getValue());
  777. }
  778. }
  779. if ($this->isInheritance()) {
  780. $colNode->setAttribute('inheritance', $this->inheritanceType);
  781. foreach ($this->inheritanceList as $inheritance) {
  782. $inheritance->appendXml($colNode);
  783. }
  784. }
  785. if ($this->isNodeKey()) {
  786. $colNode->setAttribute('nodeKey', 'true');
  787. if ($this->getNodeKeySep() !== null) {
  788. $colNode->setAttribute('nodeKeySep', $this->nodeKeySep);
  789. }
  790. }
  791. foreach ($this->vendorInfos as $vi) {
  792. $vi->appendXml($colNode);
  793. }
  794. }
  795. /**
  796. * Returns the size of the column
  797. * @return string
  798. */
  799. public function getSize()
  800. {
  801. return $this->domain->getSize();
  802. }
  803. /**
  804. * Set the size of the column
  805. * @param string $newSize
  806. */
  807. public function setSize($newSize)
  808. {
  809. $this->domain->setSize($newSize);
  810. }
  811. /**
  812. * Returns the scale of the column
  813. * @return string
  814. */
  815. public function getScale()
  816. {
  817. return $this->domain->getScale();
  818. }
  819. /**
  820. * Set the scale of the column
  821. * @param string $newScale
  822. */
  823. public function setScale($newScale)
  824. {
  825. $this->domain->setScale($newScale);
  826. }
  827. /**
  828. * Return the size in brackets for use in an sql
  829. * schema if the type is String. Otherwise return an empty string
  830. */
  831. public function printSize()
  832. {
  833. return $this->domain->printSize();
  834. }
  835. /**
  836. * Return a string that will give this column a default value.
  837. * @return string
  838. */
  839. public function getDefaultSetting()
  840. {
  841. $dflt = "";
  842. $defaultValue = $this->getDefaultValue();
  843. if ($defaultValue !== null) {
  844. $dflt .= "default ";
  845. if ($this->getDefaultValue()->isExpression()) {
  846. $dflt .= $this->getDefaultValue()->getValue();
  847. } else {
  848. if ($this->isTextType()) {
  849. $dflt .= $this->getPlatform()->quote($defaultValue->getValue());
  850. } elseif ($this->getType() == PropelTypes::BOOLEAN) {
  851. $dflt .= $this->getPlatform()->getBooleanString($defaultValue->getValue());
  852. } else {
  853. $dflt .= $defaultValue->getValue();
  854. }
  855. }
  856. }
  857. return $dflt;
  858. }
  859. /**
  860. * Return a string that will give this column a default value in PHP
  861. * @return string
  862. */
  863. public function getDefaultValueString()
  864. {
  865. $defaultValue = $this->getDefaultValue();
  866. if ($defaultValue !== null) {
  867. if ($this->isNumericType()) {
  868. $dflt = (float) $defaultValue->getValue();
  869. } elseif ($this->isTextType() || $this->getDefaultValue()->isExpression()) {
  870. $dflt = "'" . str_replace("'", "\'", $defaultValue->getValue()) . "'";
  871. } elseif ($this->getType() == PropelTypes::BOOLEAN) {
  872. $dflt = $this->booleanValue($defaultValue->getValue()) ? 'true' : 'false';
  873. } else {
  874. $dflt = "'" . $defaultValue->getValue() . "'";
  875. }
  876. } else {
  877. $dflt = "null";
  878. }
  879. return $dflt;
  880. }
  881. /**
  882. * Set a string that will give this column a default value.
  883. */
  884. public function setDefaultValue($def)
  885. {
  886. $this->domain->setDefaultValue($def);
  887. }
  888. /**
  889. * Get the default value object for this column.
  890. * @return ColumnDefaultValue
  891. * @see Domain::getDefaultValue()
  892. */
  893. public function getDefaultValue()
  894. {
  895. return $this->domain->getDefaultValue();
  896. }
  897. /**
  898. * Get the default value suitable for use in PHP.
  899. * @return mixed
  900. * @see Domain::getPhpDefaultValue()
  901. */
  902. public function getPhpDefaultValue()
  903. {
  904. return $this->domain->getPhpDefaultValue();
  905. }
  906. /**
  907. * Returns the class name to do input validation
  908. */
  909. public function getInputValidator()
  910. {
  911. return $this->inputValidator;
  912. }
  913. /**
  914. * Return auto increment/sequence string for the target database. We need to
  915. * pass in the props for the target database!
  916. */
  917. public function isAutoIncrement()
  918. {
  919. return $this->isAutoIncrement;
  920. }
  921. /**
  922. * Return auto increment/sequence string for the target database. We need to
  923. * pass in the props for the target database!
  924. */
  925. public function isLazyLoad()
  926. {
  927. return $this->isLazyLoad;
  928. }
  929. /**
  930. * Gets the auto-increment string.
  931. * @return string
  932. */
  933. public function getAutoIncrementString()
  934. {
  935. if ($this->isAutoIncrement()&& IDMethod::NATIVE === $this->getTable()->getIdMethod()) {
  936. return $this->getPlatform()->getAutoIncrement();
  937. } elseif ($this->isAutoIncrement()) {
  938. throw new EngineException("You have specified autoIncrement for column '" . $this->name . "' but you have not specified idMethod=\"native\" for table '" . $this->getTable()->getName() . "'.");
  939. }
  940. return "";
  941. }
  942. /**
  943. * Set the auto increment value.
  944. * Use isAutoIncrement() to find out if it is set or not.
  945. */
  946. public function setAutoIncrement($value)
  947. {
  948. $this->isAutoIncrement = (boolean) $value;
  949. }
  950. /**
  951. * Set the column type from a string property
  952. * (normally a string from an sql input file)
  953. *
  954. * @deprecated Do not use; this will be removed in next release.
  955. */
  956. public function setTypeFromString($typeName, $size)
  957. {
  958. $tn = strtoupper($typeName);
  959. $this->setType($tn);
  960. if ($size !== null) {
  961. $this->size = $size;
  962. }
  963. if (strpos($tn, "CHAR") !== false) {
  964. $this->domain->setType(PropelTypes::VARCHAR);
  965. } elseif (strpos($tn, "INT") !== false) {
  966. $this->domain->setType(PropelTypes::INTEGER);
  967. } elseif (strpos($tn, "FLOAT") !== false) {
  968. $this->domain->setType(PropelTypes::FLOAT);
  969. } elseif (strpos($tn, "DATE") !== false) {
  970. $this->domain->setType(PropelTypes::DATE);
  971. } elseif (strpos($tn, "TIME") !== false) {
  972. $this->domain->setType(PropelTypes::TIMESTAMP);
  973. } else if (strpos($tn, "BINARY") !== false) {
  974. $this->domain->setType(PropelTypes::LONGVARBINARY);
  975. } else {
  976. $this->domain->setType(PropelTypes::VARCHAR);
  977. }
  978. }
  979. /**
  980. * Return a string representation of the native PHP type which corresponds
  981. * to the propel type of this column. Use in the generation of Base objects.
  982. *
  983. * @return string PHP datatype used by propel.
  984. */
  985. public function getPhpNative()
  986. {
  987. return PropelTypes::getPhpNative($this->getType());
  988. }
  989. /**
  990. * Returns true if the column's PHP native type is an boolean, int, long, float, double, string.
  991. * @return boolean
  992. * @see PropelTypes::isPhpPrimitiveType()
  993. */
  994. public function isPhpPrimitiveType()
  995. {
  996. return PropelTypes::isPhpPrimitiveType($this->getPhpType());
  997. }
  998. /**
  999. * Return true if column's PHP native type is an boolean, int, long, float, double.
  1000. * @return boolean
  1001. * @see PropelTypes::isPhpPrimitiveNumericType()
  1002. */
  1003. public function isPhpPrimitiveNumericType()
  1004. {
  1005. return PropelTypes::isPhpPrimitiveNumericType($this->getPhpType());
  1006. }
  1007. /**
  1008. * Returns true if the column's PHP native type is a class name.
  1009. * @return boolean
  1010. * @see PropelTypes::isPhpObjectType()
  1011. */
  1012. public function isPhpObjectType()
  1013. {
  1014. return PropelTypes::isPhpObjectType($this->getPhpType());
  1015. }
  1016. /**
  1017. * Get the platform/adapter impl.
  1018. *
  1019. * @return Platform
  1020. */
  1021. public function getPlatform()
  1022. {
  1023. return $this->getTable()->getDatabase()->getPlatform();
  1024. }
  1025. /**
  1026. *
  1027. * @return string
  1028. * @deprecated Use DDLBuilder->getColumnDDL() instead; this will be removed in 1.3
  1029. */
  1030. public function getSqlString()
  1031. {
  1032. $sb = "";
  1033. $sb .= $this->getPlatform()->quoteIdentifier($this->getName()) . " ";
  1034. $sb .= $this->getDomain()->getSqlType();
  1035. if ($this->getPlatform()->hasSize($this->getDomain()->getSqlType())) {
  1036. $sb .= $this->getDomain()->printSize();
  1037. }
  1038. $sb .= " ";
  1039. $sb .= $this->getDefaultSetting() . " ";
  1040. $sb .= $this->getNotNullString() . " ";
  1041. $sb .= $this->getAutoIncrementString();
  1042. return trim($sb);
  1043. }
  1044. public static function generatePhpName($name, $phpNamingMethod = PhpNameGenerator::CONV_METHOD_CLEAN, $namePrefix = null) {
  1045. return NameFactory::generateName(NameFactory::PHP_GENERATOR, array($name, $phpNamingMethod, $namePrefix));
  1046. }
  1047. }