PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/generator/lib/model/Column.php

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