PageRenderTime 66ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/vendor/symfony/lib/plugins/sfPropelPlugin/lib/vendor/propel-generator/classes/propel/engine/builder/om/php5/PHP5ObjectBuilder.php

https://bitbucket.org/ealexandru/jobeet
PHP | 4071 lines | 2268 code | 415 blank | 1388 comment | 286 complexity | 9dcb40eacd1614f65f1a784fa9649370 MD5 | raw file
Possible License(s): ISC, AGPL-3.0, LGPL-2.1, BSD-3-Clause, LGPL-3.0

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

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

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