PageRenderTime 49ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/generator/lib/model/Column.php

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