PageRenderTime 72ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 1ms

/generator/lib/builder/om/PHP5ObjectBuilder.php

https://github.com/nextbigsound/propel-orm
PHP | 4250 lines | 2411 code | 419 blank | 1420 comment | 321 complexity | da834125b9dea35050b6d9a3d31b553c MD5 | raw file

Large files files are truncated, but you can click here to view the full 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 'builder/om/ObjectBuilder.php';
  10. /**
  11. * Generates a PHP5 base Object class for user object model (OM).
  12. *
  13. * This class produces the base object class (e.g. BaseMyTable) which contains all
  14. * the custom-built accessor and setter methods.
  15. *
  16. * @author Hans Lellelid <hans@xmpl.org>
  17. * @package propel.generator.builder.om
  18. */
  19. class PHP5ObjectBuilder extends ObjectBuilder
  20. {
  21. /**
  22. * Gets the package for the [base] object classes.
  23. * @return string
  24. */
  25. public function getPackage()
  26. {
  27. return parent::getPackage() . ".om";
  28. }
  29. public function getNamespace()
  30. {
  31. if ($namespace = parent::getNamespace()) {
  32. if ($this->getGeneratorConfig() && $omns = $this->getGeneratorConfig()->getBuildProperty('namespaceOm')) {
  33. return $namespace . '\\' . $omns;
  34. } else {
  35. return $namespace;
  36. }
  37. }
  38. }
  39. /**
  40. * Returns the name of the current class being built.
  41. * @return string
  42. */
  43. public function getUnprefixedClassname()
  44. {
  45. return $this->getBuildProperty('basePrefix') . $this->getStubObjectBuilder()->getUnprefixedClassname();
  46. }
  47. /**
  48. * Validates the current table to make sure that it won't
  49. * result in generated code that will not parse.
  50. *
  51. * This method may emit warnings for code which may cause problems
  52. * and will throw exceptions for errors that will definitely cause
  53. * problems.
  54. */
  55. protected function validateModel()
  56. {
  57. parent::validateModel();
  58. $table = $this->getTable();
  59. // Check to see whether any generated foreign key names
  60. // will conflict with column names.
  61. $colPhpNames = array();
  62. $fkPhpNames = array();
  63. foreach ($table->getColumns() as $col) {
  64. $colPhpNames[] = $col->getPhpName();
  65. }
  66. foreach ($table->getForeignKeys() as $fk) {
  67. $fkPhpNames[] = $this->getFKPhpNameAffix($fk, $plural = false);
  68. }
  69. $intersect = array_intersect($colPhpNames, $fkPhpNames);
  70. if (!empty($intersect)) {
  71. throw new EngineException("One or more of your column names for [" . $table->getName() . "] table conflict with foreign key names (" . implode(", ", $intersect) . ")");
  72. }
  73. // Check foreign keys to see if there are any foreign keys that
  74. // are also matched with an inversed referencing foreign key
  75. // (this is currently unsupported behavior)
  76. // see: http://propel.phpdb.org/trac/ticket/549
  77. foreach ($table->getForeignKeys() as $fk) {
  78. if ($fk->isMatchedByInverseFK()) {
  79. throw new EngineException("The 1:1 relationship expressed by foreign key " . $fk->getName() . " is defined in both directions; Propel does not currently support this (if you must have both foreign key constraints, consider adding this constraint with a custom SQL file.)" );
  80. }
  81. }
  82. }
  83. /**
  84. * Returns the appropriate formatter (from platform) for a date/time column.
  85. * @param Column $col
  86. * @return string
  87. */
  88. protected function getTemporalFormatter(Column $col)
  89. {
  90. $fmt = null;
  91. if ($col->getType() === PropelTypes::DATE) {
  92. $fmt = $this->getPlatform()->getDateFormatter();
  93. } elseif ($col->getType() === PropelTypes::TIME) {
  94. $fmt = $this->getPlatform()->getTimeFormatter();
  95. } elseif ($col->getType() === PropelTypes::TIMESTAMP) {
  96. $fmt = $this->getPlatform()->getTimestampFormatter();
  97. }
  98. return $fmt;
  99. }
  100. /**
  101. * Returns the type-casted and stringified default value for the specified Column.
  102. * This only works for scalar default values currently.
  103. * @return string The default value or 'NULL' if there is none.
  104. */
  105. protected function getDefaultValueString(Column $col)
  106. {
  107. $defaultValue = var_export(null, true);
  108. if (($val = $col->getPhpDefaultValue()) !== null) {
  109. if ($col->isTemporalType()) {
  110. $fmt = $this->getTemporalFormatter($col);
  111. try {
  112. if (!($this->getPlatform() instanceof MysqlPlatform &&
  113. ($val === '0000-00-00 00:00:00' || $val === '0000-00-00'))) {
  114. // while technically this is not a default value of NULL,
  115. // this seems to be closest in meaning.
  116. $defDt = new DateTime($val);
  117. $defaultValue = var_export($defDt->format($fmt), true);
  118. }
  119. } catch (Exception $x) {
  120. // prevent endless loop when timezone is undefined
  121. date_default_timezone_set('America/Los_Angeles');
  122. throw new EngineException("Unable to parse default temporal value for " . $col->getFullyQualifiedName() . ": " .$this->getDefaultValueString($col), $x);
  123. }
  124. } else {
  125. if ($col->isPhpPrimitiveType()) {
  126. settype($val, $col->getPhpType());
  127. $defaultValue = var_export($val, true);
  128. } elseif ($col->isPhpObjectType()) {
  129. $defaultValue = 'new '.$col->getPhpType().'(' . var_export($val, true) . ')';
  130. } else {
  131. throw new EngineException("Cannot get default value string for " . $col->getFullyQualifiedName());
  132. }
  133. }
  134. }
  135. return $defaultValue;
  136. }
  137. /**
  138. * Adds the include() statements for files that this class depends on or utilizes.
  139. * @param string &$script The script will be modified in this method.
  140. */
  141. protected function addIncludes(&$script)
  142. {
  143. } // addIncludes()
  144. /**
  145. * Adds class phpdoc comment and openning of class.
  146. * @param string &$script The script will be modified in this method.
  147. */
  148. protected function addClassOpen(&$script)
  149. {
  150. $table = $this->getTable();
  151. $tableName = $table->getName();
  152. $tableDesc = $table->getDescription();
  153. $interface = $this->getInterface();
  154. $parentClass = $this->getBehaviorContent('parentClass');
  155. $parentClass = (null !== $parentClass) ? $parentClass : ClassTools::classname($this->getBaseClass());
  156. $script .= "
  157. /**
  158. * Base class that represents a row from the '$tableName' table.
  159. *
  160. * $tableDesc
  161. *";
  162. if ($this->getBuildProperty('addTimeStamp')) {
  163. $now = strftime('%c');
  164. $script .= "
  165. * This class was autogenerated by Propel " . $this->getBuildProperty('version') . " on:
  166. *
  167. * $now
  168. *";
  169. }
  170. $script .= "
  171. * @package propel.generator.".$this->getPackage()."
  172. */
  173. abstract class ".$this->getClassname()." extends ".$parentClass." ";
  174. $interface = ClassTools::getInterface($table);
  175. if ($interface) {
  176. $script .= " implements " . ClassTools::classname($interface);
  177. }
  178. if ($this->getTable()->getInterface()) {
  179. $this->declareClassFromBuilder($this->getInterfaceBuilder());
  180. }
  181. $script .= "
  182. {
  183. ";
  184. }
  185. /**
  186. * Specifies the methods that are added as part of the basic OM class.
  187. * This can be overridden by subclasses that wish to add more methods.
  188. * @see ObjectBuilder::addClassBody()
  189. */
  190. protected function addClassBody(&$script)
  191. {
  192. $this->declareClassFromBuilder($this->getStubPeerBuilder());
  193. $this->declareClassFromBuilder($this->getStubQueryBuilder());
  194. $this->declareClasses('Propel', 'PropelException', 'PDO', 'PropelPDO', 'Criteria', 'BaseObject', 'Persistent', 'BasePeer', 'PropelObjectcollection');
  195. $table = $this->getTable();
  196. if (!$table->isAlias()) {
  197. $this->addConstants($script);
  198. $this->addAttributes($script);
  199. }
  200. if ($this->hasDefaultValues()) {
  201. $this->addApplyDefaultValues($script);
  202. $this->addConstructor($script);
  203. }
  204. $this->addColumnAccessorMethods($script);
  205. $this->addColumnMutatorMethods($script);
  206. $this->addHasOnlyDefaultValues($script);
  207. $this->addHydrate($script);
  208. $this->addEnsureConsistency($script);
  209. if (!$table->isReadOnly()) {
  210. $this->addManipulationMethods($script);
  211. }
  212. if ($this->isAddValidateMethod()) {
  213. $this->addValidationMethods($script);
  214. }
  215. if ($this->isAddGenericAccessors()) {
  216. $this->addGetByName($script);
  217. $this->addGetByPosition($script);
  218. $this->addToArray($script);
  219. }
  220. if ($this->isAddGenericMutators()) {
  221. $this->addSetByName($script);
  222. $this->addSetByPosition($script);
  223. $this->addFromArray($script);
  224. }
  225. $this->addBuildCriteria($script);
  226. $this->addBuildPkeyCriteria($script);
  227. $this->addGetPrimaryKey($script);
  228. $this->addSetPrimaryKey($script);
  229. $this->addIsPrimaryKeyNull($script);
  230. $this->addCopy($script);
  231. if (!$table->isAlias()) {
  232. $this->addGetPeer($script);
  233. }
  234. $this->addFKMethods($script);
  235. $this->addRefFKMethods($script);
  236. $this->addCrossFKMethods($script);
  237. $this->addClear($script);
  238. $this->addClearAllReferences($script);
  239. $this->addPrimaryString($script);
  240. // apply behaviors
  241. $this->applyBehaviorModifier('objectMethods', $script, " ");
  242. $this->addMagicCall($script);
  243. }
  244. /**
  245. * Closes class.
  246. * @param string &$script The script will be modified in this method.
  247. */
  248. protected function addClassClose(&$script)
  249. {
  250. $script .= "
  251. } // " . $this->getClassname() . "
  252. ";
  253. $this->applyBehaviorModifier('objectFilter', $script, "");
  254. }
  255. /**
  256. * Adds any constants to the class.
  257. * @param string &$script The script will be modified in this method.
  258. */
  259. protected function addConstants(&$script)
  260. {
  261. $script .= "
  262. /**
  263. * Peer class name
  264. */
  265. const PEER = '" . addslashes($this->getStubPeerBuilder()->getFullyQualifiedClassname()) . "';
  266. ";
  267. }
  268. /**
  269. * Adds class attributes.
  270. * @param string &$script The script will be modified in this method.
  271. */
  272. protected function addAttributes(&$script)
  273. {
  274. $table = $this->getTable();
  275. $script .= "
  276. /**
  277. * The Peer class.
  278. * Instance provides a convenient way of calling static methods on a class
  279. * that calling code may not be able to identify.
  280. * @var ".$this->getPeerClassname()."
  281. */
  282. protected static \$peer;
  283. ";
  284. if (!$table->isAlias()) {
  285. $this->addColumnAttributes($script);
  286. }
  287. foreach ($table->getForeignKeys() as $fk) {
  288. $this->addFKAttributes($script, $fk);
  289. }
  290. foreach ($table->getReferrers() as $refFK) {
  291. $this->addRefFKAttributes($script, $refFK);
  292. }
  293. // many-to-many relationships
  294. foreach ($table->getCrossFks() as $fkList) {
  295. $crossFK = $fkList[1];
  296. $this->addCrossFKAttributes($script, $crossFK);
  297. }
  298. $this->addAlreadyInSaveAttribute($script);
  299. $this->addAlreadyInValidationAttribute($script);
  300. // apply behaviors
  301. $this->applyBehaviorModifier('objectAttributes', $script, " ");
  302. }
  303. /**
  304. * Adds variables that store column values.
  305. * @param string &$script The script will be modified in this method.
  306. * @see addColumnNameConstants()
  307. */
  308. protected function addColumnAttributes(&$script)
  309. {
  310. $table = $this->getTable();
  311. foreach ($table->getColumns() as $col) {
  312. $this->addColumnAttributeComment($script, $col);
  313. $this->addColumnAttributeDeclaration($script, $col);
  314. if ($col->isLazyLoad() ) {
  315. $this->addColumnAttributeLoaderComment($script, $col);
  316. $this->addColumnAttributeLoaderDeclaration($script, $col);
  317. }
  318. }
  319. }
  320. /**
  321. * Add comment about the attribute (variable) that stores column values
  322. * @param string &$script The script will be modified in this method.
  323. * @param Column $col
  324. **/
  325. protected function addColumnAttributeComment(&$script, Column $col)
  326. {
  327. $cptype = $col->getPhpType();
  328. $clo = strtolower($col->getName());
  329. $script .= "
  330. /**
  331. * The value for the $clo field.";
  332. if ($col->getDefaultValue()) {
  333. if ($col->getDefaultValue()->isExpression()) {
  334. $script .= "
  335. * Note: this column has a database default value of: (expression) ".$col->getDefaultValue()->getValue();
  336. } else {
  337. $script .= "
  338. * Note: this column has a database default value of: ". $this->getDefaultValueString($col);
  339. }
  340. }
  341. $script .= "
  342. * @var $cptype
  343. */";
  344. }
  345. /**
  346. * Adds the declaration of a column value storage attribute
  347. * @param string &$script The script will be modified in this method.
  348. * @param Column $col
  349. **/
  350. protected function addColumnAttributeDeclaration(&$script, Column $col)
  351. {
  352. $clo = strtolower($col->getName());
  353. $script .= "
  354. protected \$" . $clo . ";
  355. ";
  356. }
  357. /**
  358. * Adds the comment about the attribute keeping track if an attribute value has been loaded
  359. * @param string &$script The script will be modified in this method.
  360. * @param Column $col
  361. **/
  362. protected function addColumnAttributeLoaderComment(&$script, Column $col)
  363. {
  364. $clo = strtolower($col->getName());
  365. $script .= "
  366. /**
  367. * Whether the lazy-loaded \$$clo value has been loaded from database.
  368. * This is necessary to avoid repeated lookups if \$$clo column is NULL in the db.
  369. * @var boolean
  370. */";
  371. }
  372. /**
  373. * Adds the declaration of the attribute keeping track of an attribute's loaded state
  374. * @param string &$script The script will be modified in this method.
  375. * @param Column $col
  376. **/
  377. protected function addColumnAttributeLoaderDeclaration(&$script, Column $col)
  378. {
  379. $clo = strtolower($col->getName());
  380. $script .= "
  381. protected \$".$clo."_isLoaded = false;
  382. ";
  383. }
  384. /**
  385. * Adds the getPeer() method.
  386. * This is a convenient, non introspective way of getting the Peer class for a particular object.
  387. * @param string &$script The script will be modified in this method.
  388. */
  389. protected function addGetPeer(&$script)
  390. {
  391. $this->addGetPeerComment($script);
  392. $this->addGetPeerFunctionOpen($script);
  393. $this->addGetPeerFunctionBody($script);
  394. $this->addGetPeerFunctionClose($script);
  395. }
  396. /**
  397. * Add the comment for the getPeer method
  398. * @param string &$script The script will be modified in this method.
  399. **/
  400. protected function addGetPeerComment(&$script) {
  401. $script .= "
  402. /**
  403. * Returns a peer instance associated with this om.
  404. *
  405. * Since Peer classes are not to have any instance attributes, this method returns the
  406. * same instance for all member of this class. The method could therefore
  407. * be static, but this would prevent one from overriding the behavior.
  408. *
  409. * @return ".$this->getPeerClassname()."
  410. */";
  411. }
  412. /**
  413. * Adds the function declaration (function opening) for the getPeer method
  414. * @param string &$script The script will be modified in this method.
  415. **/
  416. protected function addGetPeerFunctionOpen(&$script) {
  417. $script .= "
  418. public function getPeer()
  419. {";
  420. }
  421. /**
  422. * Adds the body of the getPeer method
  423. * @param string &$script The script will be modified in this method.
  424. **/
  425. protected function addGetPeerFunctionBody(&$script) {
  426. $script .= "
  427. if (self::\$peer === null) {
  428. " . $this->buildObjectInstanceCreationCode('self::$peer', $this->getPeerClassname()) . "
  429. }
  430. return self::\$peer;";
  431. }
  432. /**
  433. * Add the function close for the getPeer method
  434. * Note: this is just a } and the body ends with a return statement, so it's quite useless. But it's here anyway for consisency, cause there's a close function for all functions and in some other instances, they are useful
  435. * @param string &$script The script will be modified in this method.
  436. **/
  437. protected function addGetPeerFunctionClose(&$script) {
  438. $script .= "
  439. }
  440. ";
  441. }
  442. /**
  443. * Adds the constructor for this object.
  444. * @param string &$script The script will be modified in this method.
  445. * @see addConstructor()
  446. */
  447. protected function addConstructor(&$script)
  448. {
  449. $this->addConstructorComment($script);
  450. $this->addConstructorOpen($script);
  451. $this->addConstructorBody($script);
  452. $this->addConstructorClose($script);
  453. }
  454. /**
  455. * Adds the comment for the constructor
  456. * @param string &$script The script will be modified in this method.
  457. **/
  458. protected function addConstructorComment(&$script) {
  459. $script .= "
  460. /**
  461. * Initializes internal state of ".$this->getClassname()." object.
  462. * @see applyDefaults()
  463. */";
  464. }
  465. /**
  466. * Adds the function declaration for the constructor
  467. * @param string &$script The script will be modified in this method.
  468. **/
  469. protected function addConstructorOpen(&$script) {
  470. $script .= "
  471. public function __construct()
  472. {";
  473. }
  474. /**
  475. * Adds the function body for the constructor
  476. * @param string &$script The script will be modified in this method.
  477. **/
  478. protected function addConstructorBody(&$script) {
  479. $script .= "
  480. parent::__construct();
  481. \$this->applyDefaultValues();";
  482. }
  483. /**
  484. * Adds the function close for the constructor
  485. * @param string &$script The script will be modified in this method.
  486. **/
  487. protected function addConstructorClose(&$script) {
  488. $script .= "
  489. }
  490. ";
  491. }
  492. /**
  493. * Adds the applyDefaults() method, which is called from the constructor.
  494. * @param string &$script The script will be modified in this method.
  495. * @see addConstructor()
  496. */
  497. protected function addApplyDefaultValues(&$script)
  498. {
  499. $this->addApplyDefaultValuesComment($script);
  500. $this->addApplyDefaultValuesOpen($script);
  501. $this->addApplyDefaultValuesBody($script);
  502. $this->addApplyDefaultValuesClose($script);
  503. }
  504. /**
  505. * Adds the comment for the applyDefaults method
  506. * @param string &$script The script will be modified in this method.
  507. * @see addApplyDefaultValues()
  508. **/
  509. protected function addApplyDefaultValuesComment(&$script) {
  510. $script .= "
  511. /**
  512. * Applies default values to this object.
  513. * This method should be called from the object's constructor (or
  514. * equivalent initialization method).
  515. * @see __construct()
  516. */";
  517. }
  518. /**
  519. * Adds the function declaration for the applyDefaults method
  520. * @param string &$script The script will be modified in this method.
  521. * @see addApplyDefaultValues()
  522. **/
  523. protected function addApplyDefaultValuesOpen(&$script) {
  524. $script .= "
  525. public function applyDefaultValues()
  526. {";
  527. }
  528. /**
  529. * Adds the function body of the applyDefault method
  530. * @param string &$script The script will be modified in this method.
  531. * @see addApplyDefaultValues()
  532. **/
  533. protected function addApplyDefaultValuesBody(&$script) {
  534. $table = $this->getTable();
  535. // FIXME - Apply support for PHP default expressions here
  536. // see: http://propel.phpdb.org/trac/ticket/378
  537. $colsWithDefaults = array();
  538. foreach ($table->getColumns() as $col) {
  539. $def = $col->getDefaultValue();
  540. if ($def !== null && !$def->isExpression()) {
  541. $colsWithDefaults[] = $col;
  542. }
  543. }
  544. $colconsts = array();
  545. foreach ($colsWithDefaults as $col) {
  546. $clo = strtolower($col->getName());
  547. $script .= "
  548. \$this->".$clo." = ".$this->getDefaultValueString($col).";";
  549. }
  550. }
  551. /**
  552. * Adds the function close for the applyDefaults method
  553. * @param string &$script The script will be modified in this method.
  554. * @see addApplyDefaultValues()
  555. **/
  556. protected function addApplyDefaultValuesClose(&$script) {
  557. $script .= "
  558. }
  559. ";
  560. }
  561. // --------------------------------------------------------------
  562. //
  563. // A C C E S S O R M E T H O D S
  564. //
  565. // --------------------------------------------------------------
  566. /**
  567. * Adds a date/time/timestamp getter method.
  568. * @param string &$script The script will be modified in this method.
  569. * @param Column $col The current column.
  570. * @see parent::addColumnAccessors()
  571. */
  572. protected function addTemporalAccessor(&$script, Column $col)
  573. {
  574. $this->addTemporalAccessorComment($script, $col);
  575. $this->addTemporalAccessorOpen($script, $col);
  576. $this->addTemporalAccessorBody($script, $col);
  577. $this->addTemporalAccessorClose($script, $col);
  578. } // addTemporalAccessor
  579. /**
  580. * Adds the comment for a temporal accessor
  581. * @param string &$script The script will be modified in this method.
  582. * @param Column $col The current column.
  583. * @see addTemporalAccessor
  584. **/
  585. protected function addTemporalAccessorComment(&$script, Column $col) {
  586. $clo = strtolower($col->getName());
  587. $useDateTime = $this->getBuildProperty('useDateTimeClass');
  588. $dateTimeClass = $this->getBuildProperty('dateTimeClass');
  589. if (!$dateTimeClass) {
  590. $dateTimeClass = 'DateTime';
  591. }
  592. $handleMysqlDate = false;
  593. if ($this->getPlatform() instanceof MysqlPlatform) {
  594. if ($col->getType() === PropelTypes::TIMESTAMP) {
  595. $handleMysqlDate = true;
  596. $mysqlInvalidDateString = '0000-00-00 00:00:00';
  597. } elseif ($col->getType() === PropelTypes::DATE) {
  598. $handleMysqlDate = true;
  599. $mysqlInvalidDateString = '0000-00-00';
  600. }
  601. // 00:00:00 is a valid time, so no need to check for that.
  602. }
  603. $script .= "
  604. /**
  605. * Get the [optionally formatted] temporal [$clo] column value.
  606. * ".$col->getDescription();
  607. if (!$useDateTime) {
  608. $script .= "
  609. * This accessor only only work with unix epoch dates. Consider enabling the propel.useDateTimeClass
  610. * option in order to avoid converstions to integers (which are limited in the dates they can express).";
  611. }
  612. $script .= "
  613. *
  614. * @param string \$format The date/time format string (either date()-style or strftime()-style).
  615. * If format is NULL, then the raw ".($useDateTime ? 'DateTime object' : 'unix timestamp integer')." will be returned.";
  616. if ($useDateTime) {
  617. $script .= "
  618. * @return mixed Formatted date/time value as string or $dateTimeClass object (if format is NULL), NULL if column is NULL" .($handleMysqlDate ? ', and 0 if column value is ' . $mysqlInvalidDateString : '');
  619. } else {
  620. $script .= "
  621. * @return mixed Formatted date/time value as string or (integer) unix timestamp (if format is NULL), NULL if column is NULL".($handleMysqlDate ? ', and 0 if column value is ' . $mysqlInvalidDateString : '');
  622. }
  623. $script .= "
  624. * @throws PropelException - if unable to parse/validate the date/time value.
  625. */";
  626. }
  627. /**
  628. * Adds the function declaration for a temporal accessor
  629. * @param string &$script The script will be modified in this method.
  630. * @param Column $col The current column.
  631. * @see addTemporalAccessor
  632. **/
  633. protected function addTemporalAccessorOpen(&$script, Column $col) {
  634. $cfc = $col->getPhpName();
  635. $defaultfmt = null;
  636. $visibility = $col->getAccessorVisibility();
  637. // Default date/time formatter strings are specified in build.properties
  638. if ($col->getType() === PropelTypes::DATE) {
  639. $defaultfmt = $this->getBuildProperty('defaultDateFormat');
  640. } elseif ($col->getType() === PropelTypes::TIME) {
  641. $defaultfmt = $this->getBuildProperty('defaultTimeFormat');
  642. } elseif ($col->getType() === PropelTypes::TIMESTAMP) {
  643. $defaultfmt = $this->getBuildProperty('defaultTimeStampFormat');
  644. }
  645. if (empty($defaultfmt)) { $defaultfmt = null; }
  646. $script .= "
  647. ".$visibility." function get$cfc(\$format = ".var_export($defaultfmt, true)."";
  648. if ($col->isLazyLoad()) $script .= ", \$con = null";
  649. $script .= ")
  650. {";
  651. }
  652. /**
  653. * Adds the body of the temporal accessor
  654. * @param string &$script The script will be modified in this method.
  655. * @param Column $col The current column.
  656. * @see addTemporalAccessor
  657. **/
  658. protected function addTemporalAccessorBody(&$script, Column $col) {
  659. $cfc = $col->getPhpName();
  660. $clo = strtolower($col->getName());
  661. $useDateTime = $this->getBuildProperty('useDateTimeClass');
  662. $dateTimeClass = $this->getBuildProperty('dateTimeClass');
  663. if (!$dateTimeClass) {
  664. $dateTimeClass = 'DateTime';
  665. }
  666. $defaultfmt = null;
  667. // Default date/time formatter strings are specified in build.properties
  668. if ($col->getType() === PropelTypes::DATE) {
  669. $defaultfmt = $this->getBuildProperty('defaultDateFormat');
  670. } elseif ($col->getType() === PropelTypes::TIME) {
  671. $defaultfmt = $this->getBuildProperty('defaultTimeFormat');
  672. } elseif ($col->getType() === PropelTypes::TIMESTAMP) {
  673. $defaultfmt = $this->getBuildProperty('defaultTimeStampFormat');
  674. }
  675. if (empty($defaultfmt)) { $defaultfmt = null; }
  676. $handleMysqlDate = false;
  677. if ($this->getPlatform() instanceof MysqlPlatform) {
  678. if ($col->getType() === PropelTypes::TIMESTAMP) {
  679. $handleMysqlDate = true;
  680. $mysqlInvalidDateString = '0000-00-00 00:00:00';
  681. } elseif ($col->getType() === PropelTypes::DATE) {
  682. $handleMysqlDate = true;
  683. $mysqlInvalidDateString = '0000-00-00';
  684. }
  685. // 00:00:00 is a valid time, so no need to check for that.
  686. }
  687. if ($col->isLazyLoad()) {
  688. $script .= "
  689. if (!\$this->".$clo."_isLoaded && \$this->$clo === null && !\$this->isNew()) {
  690. \$this->load$cfc(\$con);
  691. }
  692. ";
  693. }
  694. $script .= "
  695. if (\$this->$clo === null) {
  696. return null;
  697. }
  698. ";
  699. if ($handleMysqlDate) {
  700. $script .= "
  701. if (\$this->$clo === '$mysqlInvalidDateString') {
  702. // while technically this is not a default value of NULL,
  703. // this seems to be closest in meaning.
  704. return null;
  705. } else {
  706. try {
  707. \$dt = new $dateTimeClass(\$this->$clo);
  708. } catch (Exception \$x) {
  709. throw new PropelException(\"Internally stored date/time/timestamp value could not be converted to $dateTimeClass: \" . var_export(\$this->$clo, true), \$x);
  710. }
  711. }
  712. ";
  713. } else {
  714. $script .= "
  715. try {
  716. \$dt = new $dateTimeClass(\$this->$clo);
  717. } catch (Exception \$x) {
  718. throw new PropelException(\"Internally stored date/time/timestamp value could not be converted to $dateTimeClass: \" . var_export(\$this->$clo, true), \$x);
  719. }
  720. ";
  721. } // if handleMyqlDate
  722. $script .= "
  723. if (\$format === null) {";
  724. if ($useDateTime) {
  725. $script .= "
  726. // Because propel.useDateTimeClass is TRUE, we return a $dateTimeClass object.
  727. return \$dt;";
  728. } else {
  729. $script .= "
  730. // We cast here to maintain BC in API; obviously we will lose data if we're dealing with pre-/post-epoch dates.
  731. return (int) \$dt->format('U');";
  732. }
  733. $script .= "
  734. } elseif (strpos(\$format, '%') !== false) {
  735. return strftime(\$format, \$dt->format('U'));
  736. } else {
  737. return \$dt->format(\$format);
  738. }";
  739. }
  740. /**
  741. * Adds the body of the temporal accessor
  742. * @param string &$script The script will be modified in this method.
  743. * @param Column $col The current column.
  744. * @see addTemporalAccessorClose
  745. **/
  746. protected function addTemporalAccessorClose(&$script, Column $col) {
  747. $script .= "
  748. }
  749. ";
  750. }
  751. /**
  752. * Adds a normal (non-temporal) getter method.
  753. * @param string &$script The script will be modified in this method.
  754. * @param Column $col The current column.
  755. * @see parent::addColumnAccessors()
  756. */
  757. protected function addDefaultAccessor(&$script, Column $col)
  758. {
  759. $this->addDefaultAccessorComment($script, $col);
  760. $this->addDefaultAccessorOpen($script, $col);
  761. $this->addDefaultAccessorBody($script, $col);
  762. $this->addDefaultAccessorClose($script, $col);
  763. }
  764. /**
  765. * Add the comment for a default accessor method (a getter)
  766. * @param string &$script The script will be modified in this method.
  767. * @param Column $col The current column.
  768. * @see addDefaultAccessor()
  769. **/
  770. protected function addDefaultAccessorComment(&$script, Column $col) {
  771. $clo=strtolower($col->getName());
  772. $script .= "
  773. /**
  774. * Get the [$clo] column value.
  775. * ".$col->getDescription();
  776. if ($col->isLazyLoad()) {
  777. $script .= "
  778. * @param PropelPDO An optional PropelPDO connection to use for fetching this lazy-loaded column.";
  779. }
  780. $script .= "
  781. * @return ".$col->getPhpType()."
  782. */";
  783. }
  784. /**
  785. * Adds the function declaration for a default accessor
  786. * @param string &$script The script will be modified in this method.
  787. * @param Column $col The current column.
  788. * @see addDefaultAccessor()
  789. **/
  790. protected function addDefaultAccessorOpen(&$script, Column $col) {
  791. $cfc = $col->getPhpName();
  792. $visibility = $col->getAccessorVisibility();
  793. $script .= "
  794. ".$visibility." function get$cfc(";
  795. if ($col->isLazyLoad()) $script .= "PropelPDO \$con = null";
  796. $script .= ")
  797. {";
  798. }
  799. /**
  800. * Adds the function body for a default accessor method
  801. * @param string &$script The script will be modified in this method.
  802. * @param Column $col The current column.
  803. * @see addDefaultAccessor()
  804. **/
  805. protected function addDefaultAccessorBody(&$script, Column $col) {
  806. $cfc = $col->getPhpName();
  807. $clo = strtolower($col->getName());
  808. if ($col->isLazyLoad()) {
  809. $script .= "
  810. if (!\$this->".$clo."_isLoaded && \$this->$clo === null && !\$this->isNew()) {
  811. \$this->load$cfc(\$con);
  812. }
  813. ";
  814. }
  815. $script .= "
  816. return \$this->$clo;";
  817. }
  818. /**
  819. * Adds the function close for a default accessor method
  820. * @param string &$script The script will be modified in this method.
  821. * @param Column $col The current column.
  822. * @see addDefaultAccessor()
  823. **/
  824. protected function addDefaultAccessorClose(&$script, Column $col) {
  825. $script .= "
  826. }
  827. ";
  828. }
  829. /**
  830. * Adds the lazy loader method.
  831. * @param string &$script The script will be modified in this method.
  832. * @param Column $col The current column.
  833. * @see parent::addColumnAccessors()
  834. */
  835. protected function addLazyLoader(&$script, Column $col)
  836. {
  837. $this->addLazyLoaderComment($script, $col);
  838. $this->addLazyLoaderOpen($script, $col);
  839. $this->addLazyLoaderBody($script, $col);
  840. $this->addLazyLoaderClose($script, $col);
  841. }
  842. /**
  843. * Adds the comment for the lazy loader method
  844. * @param string &$script The script will be modified in this method.
  845. * @param Column $col The current column.
  846. * @see addLazyLoader()
  847. **/
  848. protected function addLazyLoaderComment(&$script, Column $col) {
  849. $clo = strtolower($col->getName());
  850. $script .= "
  851. /**
  852. * Load the value for the lazy-loaded [$clo] column.
  853. *
  854. * This method performs an additional query to return the value for
  855. * the [$clo] column, since it is not populated by
  856. * the hydrate() method.
  857. *
  858. * @param \$con PropelPDO (optional) The PropelPDO connection to use.
  859. * @return void
  860. * @throws PropelException - any underlying error will be wrapped and re-thrown.
  861. */";
  862. }
  863. /**
  864. * Adds the function declaration for the lazy loader method
  865. * @param string &$script The script will be modified in this method.
  866. * @param Column $col The current column.
  867. * @see addLazyLoader()
  868. **/
  869. protected function addLazyLoaderOpen(&$script, Column $col) {
  870. $cfc = $col->getPhpName();
  871. $script .= "
  872. protected function load$cfc(PropelPDO \$con = null)
  873. {";
  874. }
  875. /**
  876. * Adds the function body for the lazy loader method
  877. * @param string &$script The script will be modified in this method.
  878. * @param Column $col The current column.
  879. * @see addLazyLoader()
  880. **/
  881. protected function addLazyLoaderBody(&$script, Column $col) {
  882. $platform = $this->getPlatform();
  883. $clo = strtolower($col->getName());
  884. $script .= "
  885. \$c = \$this->buildPkeyCriteria();
  886. \$c->addSelectColumn(".$this->getColumnConstant($col).");
  887. try {
  888. \$stmt = ".$this->getPeerClassname()."::doSelectStmt(\$c, \$con);
  889. \$row = \$stmt->fetch(PDO::FETCH_NUM);
  890. \$stmt->closeCursor();";
  891. if ($col->getType() === PropelTypes::CLOB && $this->getPlatform() instanceof OraclePlatform) {
  892. // PDO_OCI returns a stream for CLOB objects, while other PDO adapters return a string...
  893. $script .= "
  894. \$this->$clo = stream_get_contents(\$row[0]);";
  895. } elseif ($col->isLobType() && !$platform->hasStreamBlobImpl()) {
  896. $script .= "
  897. if (\$row[0] !== null) {
  898. \$this->$clo = fopen('php://memory', 'r+');
  899. fwrite(\$this->$clo, \$row[0]);
  900. rewind(\$this->$clo);
  901. } else {
  902. \$this->$clo = null;
  903. }";
  904. } elseif ($col->isPhpPrimitiveType()) {
  905. $script .= "
  906. \$this->$clo = (\$row[0] !== null) ? (".$col->getPhpType().") \$row[0] : null;";
  907. } elseif ($col->isPhpObjectType()) {
  908. $script .= "
  909. \$this->$clo = (\$row[0] !== null) ? new ".$col->getPhpType()."(\$row[0]) : null;";
  910. } else {
  911. $script .= "
  912. \$this->$clo = \$row[0];";
  913. }
  914. $script .= "
  915. \$this->".$clo."_isLoaded = true;
  916. } catch (Exception \$e) {
  917. throw new PropelException(\"Error loading value for [$clo] column on demand.\", \$e);
  918. }";
  919. }
  920. /**
  921. * Adds the function close for the lazy loader
  922. * @param string &$script The script will be modified in this method.
  923. * @param Column $col The current column.
  924. * @see addLazyLoader()
  925. **/
  926. protected function addLazyLoaderClose(&$script, Column $col) {
  927. $script .= "
  928. }";
  929. } // addLazyLoader()
  930. // --------------------------------------------------------------
  931. //
  932. // M U T A T O R M E T H O D S
  933. //
  934. // --------------------------------------------------------------
  935. /**
  936. * Adds the open of the mutator (setter) method for a column.
  937. * @param string &$script The script will be modified in this method.
  938. * @param Column $col The current column.
  939. */
  940. protected function addMutatorOpen(&$script, Column $col)
  941. {
  942. $this->addMutatorComment($script, $col);
  943. $this->addMutatorOpenOpen($script, $col);
  944. $this->addMutatorOpenBody($script, $col);
  945. }
  946. /**
  947. * Adds the comment for a mutator
  948. * @param string &$script The script will be modified in this method.
  949. * @param Column $col The current column.
  950. * @see addMutatorOpen()
  951. **/
  952. protected function addMutatorComment(&$script, Column $col) {
  953. $clo = strtolower($col->getName());
  954. $script .= "
  955. /**
  956. * Set the value of [$clo] column.
  957. * ".$col->getDescription()."
  958. * @param ".$col->getPhpType()." \$v new value
  959. * @return ".$this->getObjectClassname()." The current object (for fluent API support)
  960. */";
  961. }
  962. /**
  963. * Adds the mutator function declaration
  964. * @param string &$script The script will be modified in this method.
  965. * @param Column $col The current column.
  966. * @see addMutatorOpen()
  967. **/
  968. protected function addMutatorOpenOpen(&$script, Column $col) {
  969. $cfc = $col->getPhpName();
  970. $visibility = $col->getMutatorVisibility();
  971. $script .= "
  972. ".$visibility." function set$cfc(\$v)
  973. {";
  974. }
  975. /**
  976. * Adds the mutator open body part
  977. * @param string &$script The script will be modified in this method.
  978. * @param Column $col The current column.
  979. * @see addMutatorOpen()
  980. **/
  981. protected function addMutatorOpenBody(&$script, Column $col) {
  982. $clo = strtolower($col->getName());
  983. $cfc = $col->getPhpName();
  984. if ($col->isLazyLoad()) {
  985. $script .= "
  986. // explicitly set the is-loaded flag to true for this lazy load col;
  987. // it doesn't matter if the value is actually set or not (logic below) as
  988. // any attempt to set the value means that no db lookup should be performed
  989. // when the get$cfc() method is called.
  990. \$this->".$clo."_isLoaded = true;
  991. ";
  992. }
  993. }
  994. /**
  995. * Adds the close of the mutator (setter) method for a column.
  996. *
  997. * @param string &$script The script will be modified in this method.
  998. * @param Column $col The current column.
  999. */
  1000. protected function addMutatorClose(&$script, Column $col)
  1001. {
  1002. $this->addMutatorCloseBody($script, $col);
  1003. $this->addMutatorCloseClose($script, $col);
  1004. }
  1005. /**
  1006. * Adds the body of the close part of a mutator
  1007. * @param string &$script The script will be modified in this method.
  1008. * @param Column $col The current column.
  1009. * @see addMutatorClose()
  1010. **/
  1011. protected function addMutatorCloseBody(&$script, Column $col) {
  1012. $table = $this->getTable();
  1013. $cfc = $col->getPhpName();
  1014. $clo = strtolower($col->getName());
  1015. if ($col->isForeignKey()) {
  1016. foreach ($col->getForeignKeys() as $fk) {
  1017. $tblFK = $table->getDatabase()->getTable($fk->getForeignTableName());
  1018. $colFK = $tblFK->getColumn($fk->getMappedForeignColumn($col->getName()));
  1019. $varName = $this->getFKVarName($fk);
  1020. $script .= "
  1021. if (\$this->$varName !== null && \$this->".$varName."->get".$colFK->getPhpName()."() !== \$v) {
  1022. \$this->$varName = null;
  1023. }
  1024. ";
  1025. } // foreach fk
  1026. } /* if col is foreign key */
  1027. foreach ($col->getReferrers() as $refFK) {
  1028. $tblFK = $this->getDatabase()->getTable($refFK->getForeignTableName());
  1029. if ( $tblFK->getName() != $table->getName() ) {
  1030. foreach ($col->getForeignKeys() as $fk) {
  1031. $tblFK = $table->getDatabase()->getTable($fk->getForeignTableName());
  1032. $colFK = $tblFK->getColumn($fk->getMappedForeignColumn($col->getName()));
  1033. if ($refFK->isLocalPrimaryKey()) {
  1034. $varName = $this->getPKRefFKVarName($refFK);
  1035. $script .= "
  1036. // update associated ".$tblFK->getPhpName()."
  1037. if (\$this->$varName !== null) {
  1038. \$this->{$varName}->set".$colFK->getPhpName()."(\$v);
  1039. }
  1040. ";
  1041. } else {
  1042. $collName = $this->getRefFKCollVarName($refFK);
  1043. $script .= "
  1044. // update associated ".$tblFK->getPhpName()."
  1045. if (\$this->$collName !== null) {
  1046. foreach (\$this->$collName as \$referrerObject) {
  1047. \$referrerObject->set".$colFK->getPhpName()."(\$v);
  1048. }
  1049. }
  1050. ";
  1051. } // if (isLocalPrimaryKey
  1052. } // foreach col->getPrimaryKeys()
  1053. } // if tablFk != table
  1054. } // foreach
  1055. }
  1056. /**
  1057. * Adds the close for the mutator close
  1058. * @param string &$script The script will be modified in this method.
  1059. * @param Column $col The current column.
  1060. * @see addMutatorClose()
  1061. **/
  1062. protected function addMutatorCloseClose(&$script, Column $col) {
  1063. $cfc = $col->getPhpName();
  1064. $script .= "
  1065. return \$this;
  1066. } // set$cfc()
  1067. ";
  1068. }
  1069. /**
  1070. * Adds a setter for BLOB columns.
  1071. * @param string &$script The script will be modified in this method.
  1072. * @param Column $col The current column.
  1073. * @see parent::addColumnMutators()
  1074. */
  1075. protected function addLobMutator(&$script, Column $col)
  1076. {
  1077. $this->addMutatorOpen($script, $col);
  1078. $clo = strtolower($col->getName());
  1079. $script .= "
  1080. // Because BLOB columns are streams in PDO we have to assume that they are
  1081. // always modified when a new value is passed in. For example, the contents
  1082. // of the stream itself may have changed externally.
  1083. if (!is_resource(\$v) && \$v !== null) {
  1084. \$this->$clo = fopen('php://memory', 'r+');
  1085. fwrite(\$this->$clo, \$v);
  1086. rewind(\$this->$clo);
  1087. } else { // it's already a stream
  1088. \$this->$clo = \$v;
  1089. }
  1090. \$this->modifiedColumns[] = ".$this->getColumnConstant($col).";
  1091. ";
  1092. $this->addMutatorClose($script, $col);
  1093. } // addLobMutatorSnippet
  1094. /**
  1095. * Adds a setter method for date/time/timestamp columns.
  1096. * @param string &$script The script will be modified in this method.
  1097. * @param Column $col The current column.
  1098. * @see parent::addColumnMutators()
  1099. */
  1100. protected function addTemporalMutator(&$script, Column $col)
  1101. {
  1102. $cfc = $col->getPhpName();
  1103. $clo = strtolower($col->getName());
  1104. $visibility = $col->getMutatorVisibility();
  1105. $dateTimeClass = $this->getBuildProperty('dateTimeClass');
  1106. if (!$dateTimeClass) {
  1107. $dateTimeClass = 'DateTime';
  1108. }
  1109. $script .= "
  1110. /**
  1111. * Sets the value of [$clo] column to a normalized version of the date/time value specified.
  1112. * ".$col->getDescription()."
  1113. * @param mixed \$v string, integer (timestamp), or DateTime value. Empty string will
  1114. * be treated as NULL for temporal objects.
  1115. * @return ".$this->getObjectClassname()." The current object (for fluent API support)
  1116. */
  1117. ".$visibility." function set$cfc(\$v)
  1118. {";
  1119. if ($col->isLazyLoad()) {
  1120. $script .= "
  1121. // explicitly set the is-loaded flag to true for this lazy load col;
  1122. // it doesn't matter if the value is actually set or not (logic below) as
  1123. // any attempt to set the value means that no db lookup should be performed
  1124. // when the get$cfc() method is called.
  1125. \$this->".$clo."_isLoaded = true;
  1126. ";
  1127. }
  1128. $fmt = var_export($this->getTemporalFormatter($col), true);
  1129. $script .= "
  1130. // we treat '' as NULL for temporal objects because DateTime('') == DateTime('now')
  1131. // -- which is unexpected, to say the least.
  1132. if (\$v === null || \$v === '') {
  1133. \$dt = null;
  1134. } elseif (\$v instanceof DateTime) {
  1135. \$dt = \$v;
  1136. } else {
  1137. // some string/numeric value passed; we normalize that so that we can
  1138. // validate it.
  1139. try {
  1140. if (is_numeric(\$v)) { // if it's a unix timestamp
  1141. \$dt = new $dateTimeClass('@'.\$v, new DateTimeZone('UTC'));
  1142. // We have to explicitly specify and then change the time zone because of a
  1143. // DateTime bug: http://bugs.php.net/bug.php?id=43003
  1144. \$dt->setTimeZone(new DateTimeZone(date_default_timezone_get()));
  1145. } else {
  1146. \$dt = new $dateTimeClass(\$v);
  1147. }
  1148. } catch (Exception \$x) {
  1149. throw new PropelException('Error parsing date/time value: ' . var_export(\$v, true), \$x);
  1150. }
  1151. }
  1152. if ( \$this->$clo !== null || \$dt !== null ) {
  1153. // (nested ifs are a little easier to read in this case)
  1154. \$currNorm = (\$this->$clo !== null && \$tmpDt = new $dateTimeClass(\$this->$clo)) ? \$tmpDt->format($fmt) : null;
  1155. \$newNorm = (\$dt !== null) ? \$dt->format($fmt) : null;
  1156. if ( (\$currNorm !== \$newNorm) // normalized values don't match ";
  1157. if (($def = $col->getDefaultValue()) !== null && !$def->isExpression()) {
  1158. $defaultValue = $this->getDefaultValueString($col);
  1159. $script .= "
  1160. || (\$dt->format($fmt) === $defaultValue) // or the entered value matches the default";
  1161. }
  1162. $script .= "
  1163. )
  1164. {
  1165. \$this->$clo = (\$dt ? \$dt->format($fmt) : null);
  1166. \$this->modifiedColumns[] = ".$this->getColumnConstant($col).";
  1167. }
  1168. } // if either are not null
  1169. ";
  1170. $this->addMutatorClose($script, $col);
  1171. }
  1172. /**
  1173. * Adds setter method for "normal" columns.
  1174. * @param string &$script The script will be modified in this method.
  1175. * @param Column $col The current column.
  1176. * @see parent::addColumnMutators()
  1177. */
  1178. protected function addDefaultMutator(&$script, Column $col)
  1179. {
  1180. $clo = strtolower($col->getName());
  1181. $this->addMutatorOpen($script, $col);
  1182. // Perform type-casting to ensure that we can use type-sensitive
  1183. // checking in mutators.
  1184. if ($col->isPhpPrimitiveType()) {
  1185. $script .= "
  1186. if (\$v !== null) {
  1187. \$v = (".$col->getPhpType().") \$v;
  1188. }
  1189. ";
  1190. }
  1191. $script .= "
  1192. if (\$this->$clo !== \$v";
  1193. if (($def = $col->getDefaultValue()) !== null && !$def->isExpression()) {
  1194. $script .= " || \$this->isNew()";
  1195. }
  1196. $script .= ") {
  1197. \$this->$clo = \$v;
  1198. \$this->modifiedColumns[] = ".$this->getColumnConstant($col).";
  1199. }
  1200. ";
  1201. $this->addMutatorClose($script, $col);
  1202. }
  1203. /**
  1204. * Adds the hasOnlyDefaultValues() method.
  1205. * @param string &$script The script will be modified in this method.
  1206. */
  1207. protected function addHasOnlyDefaultValues(&$script)
  1208. {
  1209. $this->addHasOnlyDefaultValuesComment($script);
  1210. $this->addHasOnlyDefaultValuesOpen($script);
  1211. $this->addHasOnlyDefaultValuesBody($script);
  1212. $this->addHasOnlyDefaultValuesClose($script);
  1213. }
  1214. /**
  1215. * Adds the comment for the hasOnlyDefaultValues method
  1216. * @param string &$script The script will be modified in this method.
  1217. * @see addHasOnlyDefaultValues
  1218. **/
  1219. protected function addHasOnlyDefaultValuesComment(&$script) {
  1220. $script .= "
  1221. /**
  1222. * Indicates whether the columns in this object are only set to default values.
  1223. *
  1224. * This method can be used in conjunction with isModified() to indicate whether an object is both
  1225. * modified _and_ has some values set which are non-default.
  1226. *
  1227. * @return boolean Whether the columns in this object are only been set with default values.
  1228. */";
  1229. }
  1230. /**
  1231. * Adds the function declaration for the hasOnlyDefaultValues method
  1232. * @param string &$script The script will be modified in this method.
  1233. * @see addHasOnlyDefaultValues
  1234. **/
  1235. protected function addHasOnlyDefaultValuesOpen(&$script) {
  1236. $script .= "
  1237. public function hasOnlyDefaultValues()
  1238. {";
  1239. }
  1240. /**
  1241. * Adds the function body for the hasOnlyDefaultValues method
  1242. * @param string &$script The script will be modified in this method.
  1243. * @see addHasOnlyDefaultValues
  1244. **/
  1245. protected function addHasOnlyDefaultValuesBody(&$script) {
  1246. $table = $this->getTable();
  1247. $colsWithDefaults = array();
  1248. foreach ($table->getColumns() as $col) {
  1249. $def = $col->getDefaultValue();
  1250. if ($def !== null && !$def->isExpression()) {
  1251. $colsWithDefaults[] = $col;
  1252. }
  1253. }
  1254. foreach ($colsWithDefaults as $col) {
  1255. $clo = strtolower($col->getName());
  1256. $def = $col->getDefaultValue();
  1257. $script .= "
  1258. if (\$this->$clo !== " . $this->getDefaultValueString($col).") {
  1259. return false;
  1260. }
  1261. ";
  1262. }
  1263. }
  1264. /**
  1265. * Adds the function close for the hasOnlyDefaultValues method
  1266. * @param string &$script The script will be modified in this method.
  1267. * @see addHasOnlyDefaultValues
  1268. **/
  1269. protected function addHasOnlyDefaultValuesClose(&$script) {
  1270. $script .= "
  1271. // otherwise, everything was equal, so return TRUE
  1272. return true;";
  1273. $script .= "
  1274. } // hasOnlyDefaultValues()
  1275. ";
  1276. }
  1277. /**
  1278. * Adds the hydrate() method, which sets attributes of the object based on a ResultSet.
  1279. * @param string &$script The script will be modified in this method.
  1280. */
  1281. protected function addHydrate(&$script)
  1282. {
  1283. $this->addHydrateComment($script);
  1284. $this->addHydrateOpen($script);
  1285. $this->addHydrateBody($script);
  1286. $this->addHydrateClose($script);
  1287. }
  1288. /**
  1289. * Adds the comment for the hydrate method
  1290. * @param string &$script The script will be modified in this method.
  1291. * @see addHydrate()
  1292. */
  1293. protected function addHydrateComment(&$script) {
  1294. $script .= "
  1295. /**
  1296. * Hydrates (populates) the object variables with values from the database resultset.
  1297. *
  1298. * An offset (0-based \"start column\") is specified so that objects can be hydrated
  1299. * with a subset of the columns in the resultset rows. This is needed, for example,
  1300. * for results of JOIN queries where the resultset row includes columns from two or
  1301. * more tables.
  1302. *
  1303. * @param array \$row The row returned by PDOStatement->fetch(PDO::FETCH_NUM)
  1304. * @param int \$startcol 0-based offset column which indicates which restultset column to start with.
  1305. * @param boolean \$rehydrate Whether this object is being re-hydrated from the database.
  1306. * @return int next starting column
  1307. * @throws PropelException - Any caught Exception will be rewrapped as a PropelException.
  1308. */";
  1309. }
  1310. /**
  1311. * Adds the function declaration for the hydrate method
  1312. * @param string &$script The script will be modified in this method.
  1313. * @see addHydrate()
  1314. */
  1315. protected function addHydrateOpen(&$script) {
  1316. $script .= "
  1317. public function hydrate(\$row, \$startcol = 0, \$rehydrate = false)
  1318. {";
  1319. }
  1320. /**
  1321. * Adds the function body for the hydrate method
  1322. * @param string &$script The script will be modified in this method.
  1323. * @see addHydrate()
  1324. */
  1325. protected function addHydrateBody(&$script) {
  1326. $table = $this->getTable();
  1327. $platform = $this->getPlatform();
  1328. $script .= "
  1329. try {
  1330. ";
  1331. $n = 0;
  1332. foreach ($table->getColumns() as $col) {
  1333. if (!$col->isLazyLoad()) {
  1334. $clo = strtolower($col->getName());
  1335. if ($col->getType() === PropelTypes::CLOB_EMU && $this->getPlatform() instanceof OraclePlatform) {
  1336. // PDO_OCI returns a stream for CLOB objects, while other PDO adapters return a string...
  1337. $script .= "
  1338. \$this->$clo = stream_get_contents(\$row[\$startcol + $n]);";
  1339. } elseif ($col->isLobType() && !$platform->hasStreamBlobImpl()) {
  1340. $script .= "
  1341. if (\$row[\$startcol + $n] !== null) {
  1342. \$this->$clo = fopen('php://memory', 'r+');
  1343. fwrite(\$this->$clo, \$row[\$startcol + $n]);
  1344. rewind(\$this->$clo);
  1345. } else {
  1346. \$this->$clo = null;
  1347. }";
  1348. } elseif ($col->isPhpPrimitiveType()) {
  1349. $script .= "
  1350. \$this->$clo = (\$row[\$startcol + $n] !== null) ? (".$col->getPhpType().") \$row[\$startcol + $n] : null;";
  1351. } elseif ($col->isPhpObjectType()) {
  1352. $script .= "
  1353. \$this->$clo = (\$row[\$startcol + $n] !== null) ? new ".$col->getPhpType()."(\$row[\$startcol + $n]) : null;";
  1354. } else {
  1355. $script .= "
  1356. \$this->$clo = \$row[\$startcol + $n];";
  1357. }
  1358. $n++;
  1359. } // if col->isLazyLoad()
  1360. } /* foreach */
  1361. if ($this->getBuildProperty("addSaveMethod")) {
  1362. $script .= "
  1363. \$this->resetModified();
  1364. ";
  1365. }
  1366. $script .= "
  1367. \$this->setNew(false);
  1368. if (\$rehydrate) {
  1369. \$this->ensureConsistency();
  1370. }
  1371. return \$startcol + $n; // $n = ".$this->getPeerClassname()."::NUM_COLUMNS - ".$this->getPeerClassname()."::NUM_LAZY_LOAD_COLUMNS).
  1372. } catch (Exception \$e) {
  1373. throw new PropelException(\"Error populating ".$this->getStubObjectBuilder()->getClassname()." object\", \$e);
  1374. }";
  1375. }
  1376. /**
  1377. * Adds the function close for the hydrate method
  1378. * @param string &$script The script will be modified in this method.
  1379. * @see addHydrate()
  1380. */
  1381. protected function addHydrateClose(&$script) {
  1382. $script .= "
  1383. }
  1384. ";
  1385. }
  1386. /**
  1387. * Adds the buildPkeyCriteria method
  1388. * @param string &$script The script will be modified in this method.
  1389. **/
  1390. protected function addBuildPkeyCriteria(&$script) {
  1391. $this->addBuildPkeyCriteriaComment($script);
  1392. $this->addBuildPkeyCriteriaOpen($script);
  1393. $this->addBuildPkeyCriteriaBody($script);
  1394. $this->addBuildPkeyCriteriaClose($script);
  1395. }
  1396. /**
  1397. * Adds the comment for the buildPkeyCriteria method
  1398. * @param string &$script The script will be modified in this method.
  1399. * @see addBuildPkeyCriteria()
  1400. **/
  1401. protected function addBuildPkeyCriteriaComment(&$script) {
  1402. $script .= "
  1403. /**
  1404. * Builds a Criteria object containing the primary key for this object.
  1405. *
  1406. * Unlike buildCriteria() this method includes the primary key values regardless
  1407. * of whether or not they have been modified.
  1408. *
  1409. * @return Criteria The Criteria object containing value(s) for primary key(s).
  1410. */";
  1411. }
  1412. /**
  1413. * Adds the function declaration for the buildPkeyCriteria method
  1414. * @param string &$script The script will be modified in this method.
  1415. * @see addBuildPkeyCriteria()
  1416. **/
  1417. protected function addBuildPkeyCriteriaOpen(&$script) {
  1418. $script .= "
  1419. public function buildPkeyCriteria()
  1420. {";
  1421. }
  1422. /**
  1423. * Adds the function body for the buildPkeyCriteria method
  1424. * @param string &$script The script will be modified in this method.
  1425. * @see addBuildPkeyCriteria()
  1426. **/
  1427. protected function addBuildPkeyCriteriaBody(&$script) {
  1428. $script .= "
  1429. \$criteria = new Criteria(".$this->getPeerClassname()."::DATABASE_NAME);";
  1430. foreach ($this->getTable()->getPrimaryKey() as $col) {
  1431. $clo = strtolower($col->getName());
  1432. $script .= "
  1433. \$criteria->add(".$this->getColumnConstant($col).", \$this->$clo);";
  1434. }
  1435. }
  1436. /**
  1437. * Adds the function close for the buildPkeyCriteria method
  1438. * @param string &$script The script will be modified in this method.
  1439. * @see addBuildPkeyCriteria()
  1440. **/
  1441. protected function addBuildPkeyCriteriaClose(&$script) {
  1442. $script .= "
  1443. return \$criteria;
  1444. }
  1445. ";
  1446. }
  1447. /**
  1448. * …

Large files files are truncated, but you can click here to view the full file