PageRenderTime 47ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/generator/lib/model/Column.php

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