PageRenderTime 74ms CodeModel.GetById 40ms RepoModel.GetById 1ms app.codeStats 0ms

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

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