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

/src/vendor/propel/generator/lib/builder/om/PHP5ObjectBuilder.php

https://github.com/vjousse/devorigin-symfony2
PHP | 4254 lines | 2412 code | 421 blank | 1421 comment | 319 complexity | 7960399720859539e954434173af5a0f MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0, BSD-3-Clause, ISC

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. $this->declareClasses($dateTimeClass);
  667. $defaultfmt = null;
  668. // Default date/time formatter strings are specified in build.properties
  669. if ($col->getType() === PropelTypes::DATE) {
  670. $defaultfmt = $this->getBuildProperty('defaultDateFormat');
  671. } elseif ($col->getType() === PropelTypes::TIME) {
  672. $defaultfmt = $this->getBuildProperty('defaultTimeFormat');
  673. } elseif ($col->getType() === PropelTypes::TIMESTAMP) {
  674. $defaultfmt = $this->getBuildProperty('defaultTimeStampFormat');
  675. }
  676. if (empty($defaultfmt)) { $defaultfmt = null; }
  677. $handleMysqlDate = false;
  678. if ($this->getPlatform() instanceof MysqlPlatform) {
  679. if ($col->getType() === PropelTypes::TIMESTAMP) {
  680. $handleMysqlDate = true;
  681. $mysqlInvalidDateString = '0000-00-00 00:00:00';
  682. } elseif ($col->getType() === PropelTypes::DATE) {
  683. $handleMysqlDate = true;
  684. $mysqlInvalidDateString = '0000-00-00';
  685. }
  686. // 00:00:00 is a valid time, so no need to check for that.
  687. }
  688. if ($col->isLazyLoad()) {
  689. $script .= "
  690. if (!\$this->".$clo."_isLoaded && \$this->$clo === null && !\$this->isNew()) {
  691. \$this->load$cfc(\$con);
  692. }
  693. ";
  694. }
  695. $script .= "
  696. if (\$this->$clo === null) {
  697. return null;
  698. }
  699. ";
  700. if ($handleMysqlDate) {
  701. $script .= "
  702. if (\$this->$clo === '$mysqlInvalidDateString') {
  703. // while technically this is not a default value of NULL,
  704. // this seems to be closest in meaning.
  705. return null;
  706. } else {
  707. try {
  708. \$dt = new $dateTimeClass(\$this->$clo);
  709. } catch (Exception \$x) {
  710. throw new PropelException(\"Internally stored date/time/timestamp value could not be converted to $dateTimeClass: \" . var_export(\$this->$clo, true), \$x);
  711. }
  712. }
  713. ";
  714. } else {
  715. $script .= "
  716. try {
  717. \$dt = new $dateTimeClass(\$this->$clo);
  718. } catch (Exception \$x) {
  719. throw new PropelException(\"Internally stored date/time/timestamp value could not be converted to $dateTimeClass: \" . var_export(\$this->$clo, true), \$x);
  720. }
  721. ";
  722. } // if handleMyqlDate
  723. $script .= "
  724. if (\$format === null) {";
  725. if ($useDateTime) {
  726. $script .= "
  727. // Because propel.useDateTimeClass is TRUE, we return a $dateTimeClass object.
  728. return \$dt;";
  729. } else {
  730. $script .= "
  731. // We cast here to maintain BC in API; obviously we will lose data if we're dealing with pre-/post-epoch dates.
  732. return (int) \$dt->format('U');";
  733. }
  734. $script .= "
  735. } elseif (strpos(\$format, '%') !== false) {
  736. return strftime(\$format, \$dt->format('U'));
  737. } else {
  738. return \$dt->format(\$format);
  739. }";
  740. }
  741. /**
  742. * Adds the body of the temporal accessor
  743. * @param string &$script The script will be modified in this method.
  744. * @param Column $col The current column.
  745. * @see addTemporalAccessorClose
  746. **/
  747. protected function addTemporalAccessorClose(&$script, Column $col) {
  748. $script .= "
  749. }
  750. ";
  751. }
  752. /**
  753. * Adds a normal (non-temporal) getter method.
  754. * @param string &$script The script will be modified in this method.
  755. * @param Column $col The current column.
  756. * @see parent::addColumnAccessors()
  757. */
  758. protected function addDefaultAccessor(&$script, Column $col)
  759. {
  760. $this->addDefaultAccessorComment($script, $col);
  761. $this->addDefaultAccessorOpen($script, $col);
  762. $this->addDefaultAccessorBody($script, $col);
  763. $this->addDefaultAccessorClose($script, $col);
  764. }
  765. /**
  766. * Add the comment for a default accessor method (a getter)
  767. * @param string &$script The script will be modified in this method.
  768. * @param Column $col The current column.
  769. * @see addDefaultAccessor()
  770. **/
  771. protected function addDefaultAccessorComment(&$script, Column $col) {
  772. $clo=strtolower($col->getName());
  773. $script .= "
  774. /**
  775. * Get the [$clo] column value.
  776. * ".$col->getDescription();
  777. if ($col->isLazyLoad()) {
  778. $script .= "
  779. * @param PropelPDO An optional PropelPDO connection to use for fetching this lazy-loaded column.";
  780. }
  781. $script .= "
  782. * @return ".$col->getPhpType()."
  783. */";
  784. }
  785. /**
  786. * Adds the function declaration for a default accessor
  787. * @param string &$script The script will be modified in this method.
  788. * @param Column $col The current column.
  789. * @see addDefaultAccessor()
  790. **/
  791. protected function addDefaultAccessorOpen(&$script, Column $col) {
  792. $cfc = $col->getPhpName();
  793. $visibility = $col->getAccessorVisibility();
  794. $script .= "
  795. ".$visibility." function get$cfc(";
  796. if ($col->isLazyLoad()) $script .= "PropelPDO \$con = null";
  797. $script .= ")
  798. {";
  799. }
  800. /**
  801. * Adds the function body for a default accessor method
  802. * @param string &$script The script will be modified in this method.
  803. * @param Column $col The current column.
  804. * @see addDefaultAccessor()
  805. **/
  806. protected function addDefaultAccessorBody(&$script, Column $col) {
  807. $cfc = $col->getPhpName();
  808. $clo = strtolower($col->getName());
  809. if ($col->isLazyLoad()) {
  810. $script .= "
  811. if (!\$this->".$clo."_isLoaded && \$this->$clo === null && !\$this->isNew()) {
  812. \$this->load$cfc(\$con);
  813. }
  814. ";
  815. }
  816. $script .= "
  817. return \$this->$clo;";
  818. }
  819. /**
  820. * Adds the function close for a default accessor method
  821. * @param string &$script The script will be modified in this method.
  822. * @param Column $col The current column.
  823. * @see addDefaultAccessor()
  824. **/
  825. protected function addDefaultAccessorClose(&$script, Column $col) {
  826. $script .= "
  827. }
  828. ";
  829. }
  830. /**
  831. * Adds the lazy loader method.
  832. * @param string &$script The script will be modified in this method.
  833. * @param Column $col The current column.
  834. * @see parent::addColumnAccessors()
  835. */
  836. protected function addLazyLoader(&$script, Column $col)
  837. {
  838. $this->addLazyLoaderComment($script, $col);
  839. $this->addLazyLoaderOpen($script, $col);
  840. $this->addLazyLoaderBody($script, $col);
  841. $this->addLazyLoaderClose($script, $col);
  842. }
  843. /**
  844. * Adds the comment for the lazy loader method
  845. * @param string &$script The script will be modified in this method.
  846. * @param Column $col The current column.
  847. * @see addLazyLoader()
  848. **/
  849. protected function addLazyLoaderComment(&$script, Column $col) {
  850. $clo = strtolower($col->getName());
  851. $script .= "
  852. /**
  853. * Load the value for the lazy-loaded [$clo] column.
  854. *
  855. * This method performs an additional query to return the value for
  856. * the [$clo] column, since it is not populated by
  857. * the hydrate() method.
  858. *
  859. * @param \$con PropelPDO (optional) The PropelPDO connection to use.
  860. * @return void
  861. * @throws PropelException - any underlying error will be wrapped and re-thrown.
  862. */";
  863. }
  864. /**
  865. * Adds the function declaration for the lazy loader method
  866. * @param string &$script The script will be modified in this method.
  867. * @param Column $col The current column.
  868. * @see addLazyLoader()
  869. **/
  870. protected function addLazyLoaderOpen(&$script, Column $col) {
  871. $cfc = $col->getPhpName();
  872. $script .= "
  873. protected function load$cfc(PropelPDO \$con = null)
  874. {";
  875. }
  876. /**
  877. * Adds the function body for the lazy loader method
  878. * @param string &$script The script will be modified in this method.
  879. * @param Column $col The current column.
  880. * @see addLazyLoader()
  881. **/
  882. protected function addLazyLoaderBody(&$script, Column $col) {
  883. $platform = $this->getPlatform();
  884. $clo = strtolower($col->getName());
  885. $script .= "
  886. \$c = \$this->buildPkeyCriteria();
  887. \$c->addSelectColumn(".$this->getColumnConstant($col).");
  888. try {
  889. \$stmt = ".$this->getPeerClassname()."::doSelectStmt(\$c, \$con);
  890. \$row = \$stmt->fetch(PDO::FETCH_NUM);
  891. \$stmt->closeCursor();";
  892. if ($col->getType() === PropelTypes::CLOB && $this->getPlatform() instanceof OraclePlatform) {
  893. // PDO_OCI returns a stream for CLOB objects, while other PDO adapters return a string...
  894. $script .= "
  895. \$this->$clo = stream_get_contents(\$row[0]);";
  896. } elseif ($col->isLobType() && !$platform->hasStreamBlobImpl()) {
  897. $script .= "
  898. if (\$row[0] !== null) {
  899. \$this->$clo = fopen('php://memory', 'r+');
  900. fwrite(\$this->$clo, \$row[0]);
  901. rewind(\$this->$clo);
  902. } else {
  903. \$this->$clo = null;
  904. }";
  905. } elseif ($col->isPhpPrimitiveType()) {
  906. $script .= "
  907. \$this->$clo = (\$row[0] !== null) ? (".$col->getPhpType().") \$row[0] : null;";
  908. } elseif ($col->isPhpObjectType()) {
  909. $script .= "
  910. \$this->$clo = (\$row[0] !== null) ? new ".$col->getPhpType()."(\$row[0]) : null;";
  911. } else {
  912. $script .= "
  913. \$this->$clo = \$row[0];";
  914. }
  915. $script .= "
  916. \$this->".$clo."_isLoaded = true;
  917. } catch (Exception \$e) {
  918. throw new PropelException(\"Error loading value for [$clo] column on demand.\", \$e);
  919. }";
  920. }
  921. /**
  922. * Adds the function close for the lazy loader
  923. * @param string &$script The script will be modified in this method.
  924. * @param Column $col The current column.
  925. * @see addLazyLoader()
  926. **/
  927. protected function addLazyLoaderClose(&$script, Column $col) {
  928. $script .= "
  929. }";
  930. } // addLazyLoader()
  931. // --------------------------------------------------------------
  932. //
  933. // M U T A T O R M E T H O D S
  934. //
  935. // --------------------------------------------------------------
  936. /**
  937. * Adds the open of the mutator (setter) method for a column.
  938. * @param string &$script The script will be modified in this method.
  939. * @param Column $col The current column.
  940. */
  941. protected function addMutatorOpen(&$script, Column $col)
  942. {
  943. $this->addMutatorComment($script, $col);
  944. $this->addMutatorOpenOpen($script, $col);
  945. $this->addMutatorOpenBody($script, $col);
  946. }
  947. /**
  948. * Adds the comment for a mutator
  949. * @param string &$script The script will be modified in this method.
  950. * @param Column $col The current column.
  951. * @see addMutatorOpen()
  952. **/
  953. protected function addMutatorComment(&$script, Column $col) {
  954. $clo = strtolower($col->getName());
  955. $script .= "
  956. /**
  957. * Set the value of [$clo] column.
  958. * ".$col->getDescription()."
  959. * @param ".$col->getPhpType()." \$v new value
  960. * @return ".$this->getObjectClassname()." The current object (for fluent API support)
  961. */";
  962. }
  963. /**
  964. * Adds the mutator function declaration
  965. * @param string &$script The script will be modified in this method.
  966. * @param Column $col The current column.
  967. * @see addMutatorOpen()
  968. **/
  969. protected function addMutatorOpenOpen(&$script, Column $col) {
  970. $cfc = $col->getPhpName();
  971. $visibility = $col->getMutatorVisibility();
  972. $script .= "
  973. ".$visibility." function set$cfc(\$v)
  974. {";
  975. }
  976. /**
  977. * Adds the mutator open body part
  978. * @param string &$script The script will be modified in this method.
  979. * @param Column $col The current column.
  980. * @see addMutatorOpen()
  981. **/
  982. protected function addMutatorOpenBody(&$script, Column $col) {
  983. $clo = strtolower($col->getName());
  984. $cfc = $col->getPhpName();
  985. if ($col->isLazyLoad()) {
  986. $script .= "
  987. // explicitly set the is-loaded flag to true for this lazy load col;
  988. // it doesn't matter if the value is actually set or not (logic below) as
  989. // any attempt to set the value means that no db lookup should be performed
  990. // when the get$cfc() method is called.
  991. \$this->".$clo."_isLoaded = true;
  992. ";
  993. }
  994. }
  995. /**
  996. * Adds the close of the mutator (setter) method for a column.
  997. *
  998. * @param string &$script The script will be modified in this method.
  999. * @param Column $col The current column.
  1000. */
  1001. protected function addMutatorClose(&$script, Column $col)
  1002. {
  1003. $this->addMutatorCloseBody($script, $col);
  1004. $this->addMutatorCloseClose($script, $col);
  1005. }
  1006. /**
  1007. * Adds the body of the close part of a mutator
  1008. * @param string &$script The script will be modified in this method.
  1009. * @param Column $col The current column.
  1010. * @see addMutatorClose()
  1011. **/
  1012. protected function addMutatorCloseBody(&$script, Column $col) {
  1013. $table = $this->getTable();
  1014. $cfc = $col->getPhpName();
  1015. $clo = strtolower($col->getName());
  1016. if ($col->isForeignKey()) {
  1017. foreach ($col->getForeignKeys() as $fk) {
  1018. $tblFK = $table->getDatabase()->getTable($fk->getForeignTableName());
  1019. $colFK = $tblFK->getColumn($fk->getMappedForeignColumn($col->getName()));
  1020. $varName = $this->getFKVarName($fk);
  1021. $script .= "
  1022. if (\$this->$varName !== null && \$this->".$varName."->get".$colFK->getPhpName()."() !== \$v) {
  1023. \$this->$varName = null;
  1024. }
  1025. ";
  1026. } // foreach fk
  1027. } /* if col is foreign key */
  1028. foreach ($col->getReferrers() as $refFK) {
  1029. $tblFK = $this->getDatabase()->getTable($refFK->getForeignTableName());
  1030. if ( $tblFK->getName() != $table->getName() ) {
  1031. foreach ($col->getForeignKeys() as $fk) {
  1032. $tblFK = $table->getDatabase()->getTable($fk->getForeignTableName());
  1033. $colFK = $tblFK->getColumn($fk->getMappedForeignColumn($col->getName()));
  1034. if ($refFK->isLocalPrimaryKey()) {
  1035. $varName = $this->getPKRefFKVarName($refFK);
  1036. $script .= "
  1037. // update associated ".$tblFK->getPhpName()."
  1038. if (\$this->$varName !== null) {
  1039. \$this->{$varName}->set".$colFK->getPhpName()."(\$v);
  1040. }
  1041. ";
  1042. } else {
  1043. $collName = $this->getRefFKCollVarName($refFK);
  1044. $script .= "
  1045. // update associated ".$tblFK->getPhpName()."
  1046. if (\$this->$collName !== null) {
  1047. foreach (\$this->$collName as \$referrerObject) {
  1048. \$referrerObject->set".$colFK->getPhpName()."(\$v);
  1049. }
  1050. }
  1051. ";
  1052. } // if (isLocalPrimaryKey
  1053. } // foreach col->getPrimaryKeys()
  1054. } // if tablFk != table
  1055. } // foreach
  1056. }
  1057. /**
  1058. * Adds the close for the mutator close
  1059. * @param string &$script The script will be modified in this method.
  1060. * @param Column $col The current column.
  1061. * @see addMutatorClose()
  1062. **/
  1063. protected function addMutatorCloseClose(&$script, Column $col) {
  1064. $cfc = $col->getPhpName();
  1065. $script .= "
  1066. return \$this;
  1067. } // set$cfc()
  1068. ";
  1069. }
  1070. /**
  1071. * Adds a setter for BLOB columns.
  1072. * @param string &$script The script will be modified in this method.
  1073. * @param Column $col The current column.
  1074. * @see parent::addColumnMutators()
  1075. */
  1076. protected function addLobMutator(&$script, Column $col)
  1077. {
  1078. $this->addMutatorOpen($script, $col);
  1079. $clo = strtolower($col->getName());
  1080. $script .= "
  1081. // Because BLOB columns are streams in PDO we have to assume that they are
  1082. // always modified when a new value is passed in. For example, the contents
  1083. // of the stream itself may have changed externally.
  1084. if (!is_resource(\$v) && \$v !== null) {
  1085. \$this->$clo = fopen('php://memory', 'r+');
  1086. fwrite(\$this->$clo, \$v);
  1087. rewind(\$this->$clo);
  1088. } else { // it's already a stream
  1089. \$this->$clo = \$v;
  1090. }
  1091. \$this->modifiedColumns[] = ".$this->getColumnConstant($col).";
  1092. ";
  1093. $this->addMutatorClose($script, $col);
  1094. } // addLobMutatorSnippet
  1095. /**
  1096. * Adds a setter method for date/time/timestamp columns.
  1097. * @param string &$script The script will be modified in this method.
  1098. * @param Column $col The current column.
  1099. * @see parent::addColumnMutators()
  1100. */
  1101. protected function addTemporalMutator(&$script, Column $col)
  1102. {
  1103. $cfc = $col->getPhpName();
  1104. $clo = strtolower($col->getName());
  1105. $visibility = $col->getMutatorVisibility();
  1106. $dateTimeClass = $this->getBuildProperty('dateTimeClass');
  1107. if (!$dateTimeClass) {
  1108. $dateTimeClass = 'DateTime';
  1109. }
  1110. $this->declareClasses($dateTimeClass);
  1111. $script .= "
  1112. /**
  1113. * Sets the value of [$clo] column to a normalized version of the date/time value specified.
  1114. * ".$col->getDescription()."
  1115. * @param mixed \$v string, integer (timestamp), or DateTime value. Empty string will
  1116. * be treated as NULL for temporal objects.
  1117. * @return ".$this->getObjectClassname()." The current object (for fluent API support)
  1118. */
  1119. ".$visibility." function set$cfc(\$v)
  1120. {";
  1121. if ($col->isLazyLoad()) {
  1122. $script .= "
  1123. // explicitly set the is-loaded flag to true for this lazy load col;
  1124. // it doesn't matter if the value is actually set or not (logic below) as
  1125. // any attempt to set the value means that no db lookup should be performed
  1126. // when the get$cfc() method is called.
  1127. \$this->".$clo."_isLoaded = true;
  1128. ";
  1129. }
  1130. $fmt = var_export($this->getTemporalFormatter($col), true);
  1131. $script .= "
  1132. // we treat '' as NULL for temporal objects because DateTime('') == DateTime('now')
  1133. // -- which is unexpected, to say the least.
  1134. if (\$v === null || \$v === '') {
  1135. \$dt = null;
  1136. } elseif (\$v instanceof DateTime) {
  1137. \$dt = \$v;
  1138. } else {
  1139. // some string/numeric value passed; we normalize that so that we can
  1140. // validate it.
  1141. try {
  1142. if (is_numeric(\$v)) { // if it's a unix timestamp
  1143. \$dt = new $dateTimeClass('@'.\$v, new DateTimeZone('UTC'));
  1144. // We have to explicitly specify and then change the time zone because of a
  1145. // DateTime bug: http://bugs.php.net/bug.php?id=43003
  1146. \$dt->setTimeZone(new DateTimeZone(date_default_timezone_get()));
  1147. } else {
  1148. \$dt = new $dateTimeClass(\$v);
  1149. }
  1150. } catch (Exception \$x) {
  1151. throw new PropelException('Error parsing date/time value: ' . var_export(\$v, true), \$x);
  1152. }
  1153. }
  1154. if ( \$this->$clo !== null || \$dt !== null ) {
  1155. // (nested ifs are a little easier to read in this case)
  1156. \$currNorm = (\$this->$clo !== null && \$tmpDt = new $dateTimeClass(\$this->$clo)) ? \$tmpDt->format($fmt) : null;
  1157. \$newNorm = (\$dt !== null) ? \$dt->format($fmt) : null;
  1158. if ( (\$currNorm !== \$newNorm) // normalized values don't match ";
  1159. if (($def = $col->getDefaultValue()) !== null && !$def->isExpression()) {
  1160. $defaultValue = $this->getDefaultValueString($col);
  1161. $script .= "
  1162. || (\$dt->format($fmt) === $defaultValue) // or the entered value matches the default";
  1163. }
  1164. $script .= "
  1165. )
  1166. {
  1167. \$this->$clo = (\$dt ? \$dt->format($fmt) : null);
  1168. \$this->modifiedColumns[] = ".$this->getColumnConstant($col).";
  1169. }
  1170. } // if either are not null
  1171. ";
  1172. $this->addMutatorClose($script, $col);
  1173. }
  1174. /**
  1175. * Adds setter method for "normal" columns.
  1176. * @param string &$script The script will be modified in this method.
  1177. * @param Column $col The current column.
  1178. * @see parent::addColumnMutators()
  1179. */
  1180. protected function addDefaultMutator(&$script, Column $col)
  1181. {
  1182. $clo = strtolower($col->getName());
  1183. $this->addMutatorOpen($script, $col);
  1184. // Perform type-casting to ensure that we can use type-sensitive
  1185. // checking in mutators.
  1186. if ($col->isPhpPrimitiveType()) {
  1187. $script .= "
  1188. if (\$v !== null) {
  1189. \$v = (".$col->getPhpType().") \$v;
  1190. }
  1191. ";
  1192. }
  1193. $script .= "
  1194. if (\$this->$clo !== \$v";
  1195. if (($def = $col->getDefaultValue()) !== null && !$def->isExpression()) {
  1196. $script .= " || \$this->isNew()";
  1197. }
  1198. $script .= ") {
  1199. \$this->$clo = \$v;
  1200. \$this->modifiedColumns[] = ".$this->getColumnConstant($col).";
  1201. }
  1202. ";
  1203. $this->addMutatorClose($script, $col);
  1204. }
  1205. /**
  1206. * Adds the hasOnlyDefaultValues() method.
  1207. * @param string &$script The script will be modified in this method.
  1208. */
  1209. protected function addHasOnlyDefaultValues(&$script)
  1210. {
  1211. $this->addHasOnlyDefaultValuesComment($script);
  1212. $this->addHasOnlyDefaultValuesOpen($script);
  1213. $this->addHasOnlyDefaultValuesBody($script);
  1214. $this->addHasOnlyDefaultValuesClose($script);
  1215. }
  1216. /**
  1217. * Adds the comment for the hasOnlyDefaultValues method
  1218. * @param string &$script The script will be modified in this method.
  1219. * @see addHasOnlyDefaultValues
  1220. **/
  1221. protected function addHasOnlyDefaultValuesComment(&$script) {
  1222. $script .= "
  1223. /**
  1224. * Indicates whether the columns in this object are only set to default values.
  1225. *
  1226. * This method can be used in conjunction with isModified() to indicate whether an object is both
  1227. * modified _and_ has some values set which are non-default.
  1228. *
  1229. * @return boolean Whether the columns in this object are only been set with default values.
  1230. */";
  1231. }
  1232. /**
  1233. * Adds the function declaration for the hasOnlyDefaultValues method
  1234. * @param string &$script The script will be modified in this method.
  1235. * @see addHasOnlyDefaultValues
  1236. **/
  1237. protected function addHasOnlyDefaultValuesOpen(&$script) {
  1238. $script .= "
  1239. public function hasOnlyDefaultValues()
  1240. {";
  1241. }
  1242. /**
  1243. * Adds the function body for the hasOnlyDefaultValues method
  1244. * @param string &$script The script will be modified in this method.
  1245. * @see addHasOnlyDefaultValues
  1246. **/
  1247. protected function addHasOnlyDefaultValuesBody(&$script) {
  1248. $table = $this->getTable();
  1249. $colsWithDefaults = array();
  1250. foreach ($table->getColumns() as $col) {
  1251. $def = $col->getDefaultValue();
  1252. if ($def !== null && !$def->isExpression()) {
  1253. $colsWithDefaults[] = $col;
  1254. }
  1255. }
  1256. foreach ($colsWithDefaults as $col) {
  1257. $clo = strtolower($col->getName());
  1258. $def = $col->getDefaultValue();
  1259. $script .= "
  1260. if (\$this->$clo !== " . $this->getDefaultValueString($col).") {
  1261. return false;
  1262. }
  1263. ";
  1264. }
  1265. }
  1266. /**
  1267. * Adds the function close for the hasOnlyDefaultValues method
  1268. * @param string &$script The script will be modified in this method.
  1269. * @see addHasOnlyDefaultValues
  1270. **/
  1271. protected function addHasOnlyDefaultValuesClose(&$script) {
  1272. $script .= "
  1273. // otherwise, everything was equal, so return TRUE
  1274. return true;";
  1275. $script .= "
  1276. } // hasOnlyDefaultValues()
  1277. ";
  1278. }
  1279. /**
  1280. * Adds the hydrate() method, which sets attributes of the object based on a ResultSet.
  1281. * @param string &$script The script will be modified in this method.
  1282. */
  1283. protected function addHydrate(&$script)
  1284. {
  1285. $this->addHydrateComment($script);
  1286. $this->addHydrateOpen($script);
  1287. $this->addHydrateBody($script);
  1288. $this->addHydrateClose($script);
  1289. }
  1290. /**
  1291. * Adds the comment for the hydrate method
  1292. * @param string &$script The script will be modified in this method.
  1293. * @see addHydrate()
  1294. */
  1295. protected function addHydrateComment(&$script) {
  1296. $script .= "
  1297. /**
  1298. * Hydrates (populates) the object variables with values from the database resultset.
  1299. *
  1300. * An offset (0-based \"start column\") is specified so that objects can be hydrated
  1301. * with a subset of the columns in the resultset rows. This is needed, for example,
  1302. * for results of JOIN queries where the resultset row includes columns from two or
  1303. * more tables.
  1304. *
  1305. * @param array \$row The row returned by PDOStatement->fetch(PDO::FETCH_NUM)
  1306. * @param int \$startcol 0-based offset column which indicates which restultset column to start with.
  1307. * @param boolean \$rehydrate Whether this object is being re-hydrated from the database.
  1308. * @return int next starting column
  1309. * @throws PropelException - Any caught Exception will be rewrapped as a PropelException.
  1310. */";
  1311. }
  1312. /**
  1313. * Adds the function declaration for the hydrate method
  1314. * @param string &$script The script will be modified in this method.
  1315. * @see addHydrate()
  1316. */
  1317. protected function addHydrateOpen(&$script) {
  1318. $script .= "
  1319. public function hydrate(\$row, \$startcol = 0, \$rehydrate = false)
  1320. {";
  1321. }
  1322. /**
  1323. * Adds the function body for the hydrate method
  1324. * @param string &$script The script will be modified in this method.
  1325. * @see addHydrate()
  1326. */
  1327. protected function addHydrateBody(&$script) {
  1328. $table = $this->getTable();
  1329. $platform = $this->getPlatform();
  1330. $script .= "
  1331. try {
  1332. ";
  1333. $n = 0;
  1334. foreach ($table->getColumns() as $col) {
  1335. if (!$col->isLazyLoad()) {
  1336. $clo = strtolower($col->getName());
  1337. if ($col->getType() === PropelTypes::CLOB_EMU && $this->getPlatform() instanceof OraclePlatform) {
  1338. // PDO_OCI returns a stream for CLOB objects, while other PDO adapters return a string...
  1339. $script .= "
  1340. \$this->$clo = stream_get_contents(\$row[\$startcol + $n]);";
  1341. } elseif ($col->isLobType() && !$platform->hasStreamBlobImpl()) {
  1342. $script .= "
  1343. if (\$row[\$startcol + $n] !== null) {
  1344. \$this->$clo = fopen('php://memory', 'r+');
  1345. fwrite(\$this->$clo, \$row[\$startcol + $n]);
  1346. rewind(\$this->$clo);
  1347. } else {
  1348. \$this->$clo = null;
  1349. }";
  1350. } elseif ($col->isPhpPrimitiveType()) {
  1351. $script .= "
  1352. \$this->$clo = (\$row[\$startcol + $n] !== null) ? (".$col->getPhpType().") \$row[\$startcol + $n] : null;";
  1353. } elseif ($col->isPhpObjectType()) {
  1354. $script .= "
  1355. \$this->$clo = (\$row[\$startcol + $n] !== null) ? new ".$col->getPhpType()."(\$row[\$startcol + $n]) : null;";
  1356. } else {
  1357. $script .= "
  1358. \$this->$clo = \$row[\$startcol + $n];";
  1359. }
  1360. $n++;
  1361. } // if col->isLazyLoad()
  1362. } /* foreach */
  1363. if ($this->getBuildProperty("addSaveMethod")) {
  1364. $script .= "
  1365. \$this->resetModified();
  1366. ";
  1367. }
  1368. $script .= "
  1369. \$this->setNew(false);
  1370. if (\$rehydrate) {
  1371. \$this->ensureConsistency();
  1372. }
  1373. return \$startcol + $n; // $n = ".$this->getPeerClassname()."::NUM_COLUMNS - ".$this->getPeerClassname()."::NUM_LAZY_LOAD_COLUMNS).
  1374. } catch (Exception \$e) {
  1375. throw new PropelException(\"Error populating ".$this->getStubObjectBuilder()->getClassname()." object\", \$e);
  1376. }";
  1377. }
  1378. /**
  1379. * Adds the function close for the hydrate method
  1380. * @param string &$script The script will be modified in this method.
  1381. * @see addHydrate()
  1382. */
  1383. protected function addHydrateClose(&$script) {
  1384. $script .= "
  1385. }
  1386. ";
  1387. }
  1388. /**
  1389. * Adds the buildPkeyCriteria method
  1390. * @param string &$script The script will be modified in this method.
  1391. **/
  1392. protected function addBuildPkeyCriteria(&$script) {
  1393. $this->addBuildPkeyCriteriaComment($script);
  1394. $this->addBuildPkeyCriteriaOpen($script);
  1395. $this->addBuildPkeyCriteriaBody($script);
  1396. $this->addBuildPkeyCriteriaClose($script);
  1397. }
  1398. /**
  1399. * Adds the comment for the buildPkeyCriteria method
  1400. * @param string &$script The script will be modified in this method.
  1401. * @see addBuildPkeyCriteria()
  1402. **/
  1403. protected function addBuildPkeyCriteriaComment(&$script) {
  1404. $script .= "
  1405. /**
  1406. * Builds a Criteria object containing the primary key for this object.
  1407. *
  1408. * Unlike buildCriteria() this method includes the primary key values regardless
  1409. * of whether or not they have been modified.
  1410. *
  1411. * @return Criteria The Criteria object containing value(s) for primary key(s).
  1412. */";
  1413. }
  1414. /**
  1415. * Adds the function declaration for the buildPkeyCriteria method
  1416. * @param string &$script The script will be modified in this method.
  1417. * @see addBuildPkeyCriteria()
  1418. **/
  1419. protected function addBuildPkeyCriteriaOpen(&$script) {
  1420. $script .= "
  1421. public function buildPkeyCriteria()
  1422. {";
  1423. }
  1424. /**
  1425. * Adds the function body for the buildPkeyCriteria method
  1426. * @param string &$script The script will be modified in this method.
  1427. * @see addBuildPkeyCriteria()
  1428. **/
  1429. protected function addBuildPkeyCriteriaBody(&$script) {
  1430. $script .= "
  1431. \$criteria = new Criteria(".$this->getPeerClassname()."::DATABASE_NAME);";
  1432. foreach ($this->getTable()->getPrimaryKey() as $col) {
  1433. $clo = strtolower($col->getName());
  1434. $script .= "
  1435. \$criteria->add(".$this->getColumnConstant($col).", \$this->$clo);";
  1436. }
  1437. }
  1438. /**
  1439. * Adds the function close for the buildPkeyCriteria method
  1440. * @param string &$script The script will be modified in this method.
  1441. * @see addBuildPkeyCriteria()
  1442. **/
  1443. protected function addBuildPkeyC

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