PageRenderTime 64ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/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
  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 values.
  1441. */";
  1442. }
  1443. /**
  1444. * Adds the function declaration of the buildCriteria method
  1445. * @param string &$script The script will be modified in this method.
  1446. * @see addBuildCriteria()
  1447. **/
  1448. protected function addBuildCriteriaOpen(&$script) {
  1449. $script .= "
  1450. public function buildCriteria()
  1451. {";
  1452. }
  1453. /**
  1454. * Adds the function body of the buildCriteria method
  1455. * @param string &$script The script will be modified in this method.
  1456. * @see addBuildCriteria()
  1457. **/
  1458. protected function addBuildCriteriaBody(&$script) {
  1459. $script .= "
  1460. \$criteria = new Criteria(".$this->getPeerClassname()."::DATABASE_NAME);
  1461. ";
  1462. foreach ($this->getTable()->getColumns() as $col) {
  1463. $clo = strtolower($col->getName());
  1464. $script .= "
  1465. if (\$this->isColumnModified(".$this->getColumnConstant($col).")) \$criteria->add(".$this->getColumnConstant($col).", \$this->$clo);";
  1466. }
  1467. }
  1468. /**
  1469. * Adds the function close of the buildCriteria method
  1470. * @param string &$script The script will be modified in this method.
  1471. * @see addBuildCriteria()
  1472. **/
  1473. protected function addBuildCriteriaClose(&$script) {
  1474. $script .= "
  1475. return \$criteria;
  1476. }
  1477. ";
  1478. }
  1479. /**
  1480. * Adds the toArray method
  1481. * @param string &$script The script will be modified in this method.
  1482. **/
  1483. protected function addToArray(&$script)
  1484. {
  1485. $this->addToArrayComment($script);
  1486. $this->addToArrayOpen($script);
  1487. $this->addToArrayBody($script);
  1488. $this->addToArrayClose($script);
  1489. }
  1490. /**
  1491. * Adds the comment of the toArray method
  1492. * @param string &$script The script will be modified in this method.
  1493. * @see addToArray()
  1494. **/
  1495. protected function addToArrayComment(&$script) {
  1496. $script .= "
  1497. /**
  1498. * Exports the object as an array.
  1499. *
  1500. * You can specify the key type of the array by passing one of the class
  1501. * type constants.
  1502. *
  1503. * @param string \$keyType (optional) One of the class type constants BasePeer::TYPE_PHPNAME, BasePeer::TYPE_STUDLYPHPNAME
  1504. * BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM. Defaults to BasePeer::TYPE_PHPNAME.
  1505. * @param boolean \$includeLazyLoadColumns (optional) Whether to include lazy loaded columns. Defaults to TRUE.
  1506. * @return an associative array containing the field names (as keys) and field values
  1507. */";
  1508. }
  1509. /**
  1510. * Adds the function declaration of the toArray method
  1511. * @param string &$script The script will be modified in this method.
  1512. * @see addToArray()
  1513. **/
  1514. protected function addToArrayOpen(&$script) {
  1515. $script .= "
  1516. public function toArray(\$keyType = BasePeer::TYPE_PHPNAME, \$includeLazyLoadColumns = true)
  1517. {";
  1518. }
  1519. /**
  1520. * Adds the function body of the toArray method
  1521. * @param string &$script The script will be modified in this method.
  1522. * @see addToArray()
  1523. **/
  1524. protected function addToArrayBody(&$script) {
  1525. $script .= "
  1526. \$keys = ".$this->getPeerClassname()."::getFieldNames(\$keyType);
  1527. \$result = array(";
  1528. foreach ($this->getTable()->getColumns() as $num => $col) {
  1529. if ($col->isLazyLoad()) {
  1530. $script .= "
  1531. \$keys[$num] => (\$includeLazyLoadColumns) ? \$this->get".$col->getPhpName()."() : null,";
  1532. } else {
  1533. $script .= "
  1534. \$keys[$num] => \$this->get".$col->getPhpName()."(),";
  1535. }
  1536. }
  1537. $script .= "
  1538. );";
  1539. }
  1540. /**
  1541. * Adds the function close of the toArray method
  1542. * @param string &$script The script will be modified in this method.
  1543. * @see addToArray()
  1544. **/
  1545. protected function addToArrayClose(&$script) {
  1546. $script .= "
  1547. return \$result;
  1548. }
  1549. ";
  1550. } // addToArray()
  1551. /**
  1552. * Adds the getByName method
  1553. * @param string &$script The script will be modified in this method.
  1554. **/
  1555. protected function addGetByName(&$script)
  1556. {
  1557. $this->addGetByNameComment($script);
  1558. $this->addGetByNameOpen($script);
  1559. $this->addGetByNameBody($script);
  1560. $this->addGetByNameClose($script);
  1561. }
  1562. /**
  1563. * Adds the comment for the getByName method
  1564. * @param string &$script The script will be modified in this method.
  1565. * @see addGetByName
  1566. **/
  1567. protected function addGetByNameComment(&$script) {
  1568. $script .= "
  1569. /**
  1570. * Retrieves a field from the object by name passed in as a string.
  1571. *
  1572. * @param string \$name name
  1573. * @param string \$type The type of fieldname the \$name is of:
  1574. * one of the class type constants BasePeer::TYPE_PHPNAME, BasePeer::TYPE_STUDLYPHPNAME
  1575. * BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM
  1576. * @return mixed Value of field.
  1577. */";
  1578. }
  1579. /**
  1580. * Adds the function declaration for the getByName method
  1581. * @param string &$script The script will be modified in this method.
  1582. * @see addGetByName
  1583. **/
  1584. protected function addGetByNameOpen(&$script) {
  1585. $script .= "
  1586. public function getByName(\$name, \$type = BasePeer::TYPE_PHPNAME)
  1587. {";
  1588. }
  1589. /**
  1590. * Adds the function body for the getByName method
  1591. * @param string &$script The script will be modified in this method.
  1592. * @see addGetByName
  1593. **/
  1594. protected function addGetByNameBody(&$script) {
  1595. $script .= "
  1596. \$pos = ".$this->getPeerClassname()."::translateFieldName(\$name, \$type, BasePeer::TYPE_NUM);
  1597. \$field = \$this->getByPosition(\$pos);";
  1598. }
  1599. /**
  1600. * Adds the function close for the getByName method
  1601. * @param string &$script The script will be modified in this method.
  1602. * @see addGetByName
  1603. **/
  1604. protected function addGetByNameClose(&$script) {
  1605. $script .= "
  1606. return \$field;
  1607. }
  1608. ";
  1609. }
  1610. /**
  1611. * Adds the getByPosition method
  1612. * @param string &$script The script will be modified in this method.
  1613. **/
  1614. protected function addGetByPosition(&$script)
  1615. {
  1616. $this->addGetByPositionComment($script);
  1617. $this->addGetByPositionOpen($script);
  1618. $this->addGetByPositionBody($script);
  1619. $this->addGetByPositionClose($script);
  1620. }
  1621. /**
  1622. * Adds comment for the getByPosition method
  1623. * @param string &$script The script will be modified in this method.
  1624. * @see addGetByPosition
  1625. **/
  1626. protected function addGetByPositionComment(&$script) {
  1627. $script .= "
  1628. /**
  1629. * Retrieves a field from the object by Position as specified in the xml schema.
  1630. * Zero-based.
  1631. *
  1632. * @param int \$pos position in xml schema
  1633. * @return mixed Value of field at \$pos
  1634. */";
  1635. }
  1636. /**
  1637. * Adds the function declaration for the getByPosition method
  1638. * @param string &$script The script will be modified in this method.
  1639. * @see addGetByPosition
  1640. **/
  1641. protected function addGetByPositionOpen(&$script) {
  1642. $script .= "
  1643. public function getByPosition(\$pos)
  1644. {";
  1645. }
  1646. /**
  1647. * Adds the function body for the getByPosition method
  1648. * @param string &$script The script will be modified in this method.
  1649. * @see addGetByPosition
  1650. **/
  1651. protected function addGetByPositionBody(&$script) {
  1652. $table = $this->getTable();
  1653. $script .= "
  1654. switch(\$pos) {";
  1655. $i = 0;
  1656. foreach ($table->getColumns() as $col) {
  1657. $cfc = $col->getPhpName();
  1658. $cptype = $col->getPhpType();// not safe to use it because some methods may return objects (Blob)
  1659. $script .= "
  1660. case $i:
  1661. return \$this->get$cfc();
  1662. break;";
  1663. $i++;
  1664. } /* foreach */
  1665. $script .= "
  1666. default:
  1667. return null;
  1668. break;
  1669. } // switch()";
  1670. }
  1671. /**
  1672. * Adds the function close for the getByPosition method
  1673. * @param string &$script The script will be modified in this method.
  1674. * @see addGetByPosition
  1675. **/
  1676. protected function addGetByPositionClose(&$script) {
  1677. $script .= "
  1678. }
  1679. ";
  1680. }
  1681. protected function addSetByName(&$script)
  1682. {
  1683. $table = $this->getTable();
  1684. $script .= "
  1685. /**
  1686. * Sets a field from the object by name passed in as a string.
  1687. *
  1688. * @param string \$name peer name
  1689. * @param mixed \$value field value
  1690. * @param string \$type The type of fieldname the \$name is of:
  1691. * one of the class type constants BasePeer::TYPE_PHPNAME, BasePeer::TYPE_STUDLYPHPNAME
  1692. * BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM
  1693. * @return void
  1694. */
  1695. public function setByName(\$name, \$value, \$type = BasePeer::TYPE_PHPNAME)
  1696. {
  1697. \$pos = ".$this->getPeerClassname()."::translateFieldName(\$name, \$type, BasePeer::TYPE_NUM);
  1698. return \$this->setByPosition(\$pos, \$value);
  1699. }
  1700. ";
  1701. }
  1702. protected function addSetByPosition(&$script)
  1703. {
  1704. $table = $this->getTable();
  1705. $script .= "
  1706. /**
  1707. * Sets a field from the object by Position as specified in the xml schema.
  1708. * Zero-based.
  1709. *
  1710. * @param int \$pos position in xml schema
  1711. * @param mixed \$value field value
  1712. * @return void
  1713. */
  1714. public function setByPosition(\$pos, \$value)
  1715. {
  1716. switch(\$pos) {";
  1717. $i = 0;
  1718. foreach ($table->getColumns() as $col) {
  1719. $cfc = $col->getPhpName();
  1720. $cptype = $col->getPhpType();
  1721. $script .= "
  1722. case $i:
  1723. \$this->set$cfc(\$value);
  1724. break;";
  1725. $i++;
  1726. } /* foreach */
  1727. $script .= "
  1728. } // switch()
  1729. }
  1730. ";
  1731. } // addSetByPosition()
  1732. protected function addFromArray(&$script)
  1733. {
  1734. $table = $this->getTable();
  1735. $script .= "
  1736. /**
  1737. * Populates the object using an array.
  1738. *
  1739. * This is particularly useful when populating an object from one of the
  1740. * request arrays (e.g. \$_POST). This method goes through the column
  1741. * names, checking to see whether a matching key exists in populated
  1742. * array. If so the setByName() method is called for that column.
  1743. *
  1744. * You can specify the key type of the array by additionally passing one
  1745. * of the class type constants BasePeer::TYPE_PHPNAME, BasePeer::TYPE_STUDLYPHPNAME,
  1746. * BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM.
  1747. * The default key type is the column's phpname (e.g. 'AuthorId')
  1748. *
  1749. * @param array \$arr An array to populate the object from.
  1750. * @param string \$keyType The type of keys the array uses.
  1751. * @return void
  1752. */
  1753. public function fromArray(\$arr, \$keyType = BasePeer::TYPE_PHPNAME)
  1754. {
  1755. \$keys = ".$this->getPeerClassname()."::getFieldNames(\$keyType);
  1756. ";
  1757. foreach ($table->getColumns() as $num => $col) {
  1758. $cfc = $col->getPhpName();
  1759. $cptype = $col->getPhpType();
  1760. $script .= "
  1761. if (array_key_exists(\$keys[$num], \$arr)) \$this->set$cfc(\$arr[\$keys[$num]]);";
  1762. } /* foreach */
  1763. $script .= "
  1764. }
  1765. ";
  1766. } // addFromArray
  1767. /**
  1768. * Adds a delete() method to remove the object form the datastore.
  1769. * @param string &$script The script will be modified in this method.
  1770. */
  1771. protected function addDelete(&$script)
  1772. {
  1773. $this->addDeleteComment($script);
  1774. $this->addDeleteOpen($script);
  1775. $this->addDeleteBody($script);
  1776. $this->addDeleteClose($script);
  1777. }
  1778. /**
  1779. * Adds the comment for the delete function
  1780. * @param string &$script The script will be modified in this method.
  1781. * @see addDelete()
  1782. **/
  1783. protected function addDeleteComment(&$script) {
  1784. $script .= "
  1785. /**
  1786. * Removes this object from datastore and sets delete attribute.
  1787. *
  1788. * @param PropelPDO \$con
  1789. * @return void
  1790. * @throws PropelException
  1791. * @see BaseObject::setDeleted()
  1792. * @see BaseObject::isDeleted()
  1793. */";
  1794. }
  1795. /**
  1796. * Adds the function declaration for the delete function
  1797. * @param string &$script The script will be modified in this method.
  1798. * @see addDelete()
  1799. **/
  1800. protected function addDeleteOpen(&$script) {
  1801. $script .= "
  1802. public function delete(PropelPDO \$con = null)
  1803. {";
  1804. }
  1805. /**
  1806. * Adds the function body for the delete function
  1807. * @param string &$script The script will be modified in this method.
  1808. * @see addDelete()
  1809. **/
  1810. protected function addDeleteBody(&$script) {
  1811. $script .= "
  1812. if (\$this->isDeleted()) {
  1813. throw new PropelException(\"This object has already been deleted.\");
  1814. }
  1815. if (\$con === null) {
  1816. \$con = Propel::getConnection(".$this->getPeerClassname()."::DATABASE_NAME, Propel::CONNECTION_WRITE);
  1817. }
  1818. \$con->beginTransaction();
  1819. try {";
  1820. if($this->getGeneratorConfig()->getBuildProperty('addHooks')) {
  1821. $script .= "
  1822. \$ret = \$this->preDelete(\$con);";
  1823. // apply behaviors
  1824. $this->applyBehaviorModifier('preDelete', $script, " ");
  1825. $script .= "
  1826. if (\$ret) {
  1827. ".$this->getPeerClassname()."::doDelete(\$this, \$con);
  1828. \$this->postDelete(\$con);";
  1829. // apply behaviors
  1830. $this->applyBehaviorModifier('postDelete', $script, " ");
  1831. $script .= "
  1832. \$this->setDeleted(true);
  1833. \$con->commit();
  1834. } else {
  1835. \$con->commit();
  1836. }";
  1837. } else {
  1838. // apply behaviors
  1839. $this->applyBehaviorModifier('preDelete', $script, " ");
  1840. $script .= "
  1841. ".$this->getPeerClassname()."::doDelete(\$this, \$con);";
  1842. // apply behaviors
  1843. $this->applyBehaviorModifier('postDelete', $script, " ");
  1844. $script .= "
  1845. \$this->setDeleted(true);
  1846. \$con->commit();";
  1847. }
  1848. $script .= "
  1849. } catch (PropelException \$e) {
  1850. \$con->rollBack();
  1851. throw \$e;
  1852. }";
  1853. }
  1854. /**
  1855. * Adds the function close for the delete function
  1856. * @param string &$script The script will be modified in this method.
  1857. * @see addDelete()
  1858. **/
  1859. protected function addDeleteClose(&$script) {
  1860. $script .= "
  1861. }
  1862. ";
  1863. } // addDelete()
  1864. /**
  1865. * Adds a reload() method to re-fetch the data for this object from the database.
  1866. * @param string &$script The script will be modified in this method.
  1867. */
  1868. protected function addReload(&$script)
  1869. {
  1870. $table = $this->getTable();
  1871. $script .= "
  1872. /**
  1873. * Reloads this object from datastore based on primary key and (optionally) resets all associated objects.
  1874. *
  1875. * This will only work if the object has been saved and has a valid primary key set.
  1876. *
  1877. * @param boolean \$deep (optional) Whether to also de-associated any related objects.
  1878. * @param PropelPDO \$con (optional) The PropelPDO connection to use.
  1879. * @return void
  1880. * @throws PropelException - if this object is deleted, unsaved or doesn't have pk match in db
  1881. */
  1882. public function reload(\$deep = false, PropelPDO \$con = null)
  1883. {
  1884. if (\$this->isDeleted()) {
  1885. throw new PropelException(\"Cannot reload a deleted object.\");
  1886. }
  1887. if (\$this->isNew()) {
  1888. throw new PropelException(\"Cannot reload an unsaved object.\");
  1889. }
  1890. if (\$con === null) {
  1891. \$con = Propel::getConnection(".$this->getPeerClassname()."::DATABASE_NAME, Propel::CONNECTION_READ);
  1892. }
  1893. // We don't need to alter the object instance pool; we're just modifying this instance
  1894. // already in the pool.
  1895. \$stmt = ".$this->getPeerClassname()."::doSelectStmt(\$this->buildPkeyCriteria(), \$con);
  1896. \$row = \$stmt->fetch(PDO::FETCH_NUM);
  1897. \$stmt->closeCursor();
  1898. if (!\$row) {
  1899. throw new PropelException('Cannot find matching row in the database to reload object values.');
  1900. }
  1901. \$this->hydrate(\$row, 0, true); // rehydrate
  1902. ";
  1903. // support for lazy load columns
  1904. foreach ($table->getColumns() as $col) {
  1905. if ($col->isLazyLoad()) {
  1906. $clo = strtolower($col->getName());
  1907. $script .= "
  1908. // Reset the $clo lazy-load column
  1909. \$this->" . $clo . " = null;
  1910. \$this->".$clo."_isLoaded = false;
  1911. ";
  1912. }
  1913. }
  1914. $script .= "
  1915. if (\$deep) { // also de-associate any related objects?
  1916. ";
  1917. foreach ($table->getForeignKeys() as $fk) {
  1918. $varName = $this->getFKVarName($fk);
  1919. $script .= "
  1920. \$this->".$varName." = null;";
  1921. }
  1922. foreach ($table->getReferrers() as $refFK) {
  1923. if ($refFK->isLocalPrimaryKey()) {
  1924. $script .= "
  1925. \$this->".$this->getPKRefFKVarName($refFK)." = null;
  1926. ";
  1927. } else {
  1928. $script .= "
  1929. \$this->".$this->getRefFKCollVarName($refFK)." = null;
  1930. \$this->".$this->getRefFKLastCriteriaVarName($refFK)." = null;
  1931. ";
  1932. }
  1933. }
  1934. $script .= "
  1935. } // if (deep)
  1936. }
  1937. ";
  1938. } // addReload()
  1939. /**
  1940. * Adds the methods related to refreshing, saving and deleting the object.
  1941. * @param string &$script The script will be modified in this method.
  1942. */
  1943. protected function addManipulationMethods(&$script)
  1944. {
  1945. $this->addReload($script);
  1946. $this->addDelete($script);
  1947. $this->addSave($script);
  1948. $this->addDoSave($script);
  1949. }
  1950. /**
  1951. * Adds the methods related to validationg the object.
  1952. * @param string &$script The script will be modified in this method.
  1953. */
  1954. protected function addValidationMethods(&$script)
  1955. {
  1956. $this->addValidationFailuresAttribute($script);
  1957. $this->addGetValidationFailures($script);
  1958. $this->addValidate($script);
  1959. $this->addDoValidate($script);
  1960. }
  1961. /**
  1962. * Adds the $validationFailures attribute to store ValidationFailed objects.
  1963. * @param string &$script The script will be modified in this method.
  1964. */
  1965. protected function addValidationFailuresAttribute(&$script)
  1966. {
  1967. $script .= "
  1968. /**
  1969. * Array of ValidationFailed objects.
  1970. * @var array ValidationFailed[]
  1971. */
  1972. protected \$validationFailures = array();
  1973. ";
  1974. }
  1975. /**
  1976. * Adds the getValidationFailures() method.
  1977. * @param string &$script The script will be modified in this method.
  1978. */
  1979. protected function addGetValidationFailures(&$script)
  1980. {
  1981. $script .= "
  1982. /**
  1983. * Gets any ValidationFailed objects that resulted from last call to validate().
  1984. *
  1985. *
  1986. * @return array ValidationFailed[]
  1987. * @see validate()
  1988. */
  1989. public function getValidationFailures()
  1990. {
  1991. return \$this->validationFailures;
  1992. }
  1993. ";
  1994. } // addGetValidationFailures()
  1995. /**
  1996. * Adds the correct getPrimaryKey() method for this object.
  1997. * @param string &$script The script will be modified in this method.
  1998. */
  1999. protected function addGetPrimaryKey(&$script)
  2000. {
  2001. $pkeys = $this->getTable()->getPrimaryKey();
  2002. if (count($pkeys) == 1) {
  2003. $this->addGetPrimaryKey_SinglePK($script);
  2004. } elseif (count($pkeys) > 1) {
  2005. $this->addGetPrimaryKey_MultiPK($script);
  2006. } else {
  2007. // no primary key -- this is deprecated, since we don't *need* this method anymore
  2008. $this->addGetPrimaryKey_NoPK($script);
  2009. }
  2010. }
  2011. /**
  2012. * Adds the getPrimaryKey() method for tables that contain a single-column primary key.
  2013. * @param string &$script The script will be modified in this method.
  2014. */
  2015. protected function addGetPrimaryKey_SinglePK(&$script)
  2016. {
  2017. $table = $this->getTable();
  2018. $pkeys = $table->getPrimaryKey();
  2019. $cptype = $pkeys[0]->getPhpType();
  2020. $script .= "
  2021. /**
  2022. * Returns the primary key for this object (row).
  2023. * @return $cptype
  2024. */
  2025. public function getPrimaryKey()
  2026. {
  2027. return \$this->get".$pkeys[0]->getPhpName()."();
  2028. }
  2029. ";
  2030. } // addetPrimaryKey_SingleFK
  2031. /**
  2032. * Adds the setPrimaryKey() method for tables that contain a multi-column primary key.
  2033. * @param string &$script The script will be modified in this method.
  2034. */
  2035. protected function addGetPrimaryKey_MultiPK(&$script)
  2036. {
  2037. $script .= "
  2038. /**
  2039. * Returns the composite primary key for this object.
  2040. * The array elements will be in same order as specified in XML.
  2041. * @return array
  2042. */
  2043. public function getPrimaryKey()
  2044. {
  2045. \$pks = array();
  2046. ";
  2047. $i = 0;
  2048. foreach ($this->getTable()->getPrimaryKey() as $pk) {
  2049. $script .= "
  2050. \$pks[$i] = \$this->get".$pk->getPhpName()."();
  2051. ";
  2052. $i++;
  2053. } /* foreach */
  2054. $script .= "
  2055. return \$pks;
  2056. }
  2057. ";
  2058. } // addGetPrimaryKey_MultiFK()
  2059. /**
  2060. * Adds the getPrimaryKey() method for objects that have no primary key.
  2061. * This "feature" is dreprecated, since the getPrimaryKey() method is not required
  2062. * by the Persistent interface (or used by the templates). Hence, this method is also
  2063. * deprecated.
  2064. * @param string &$script The script will be modified in this method.
  2065. * @deprecated
  2066. */
  2067. protected function addGetPrimaryKey_NoPK(&$script)
  2068. {
  2069. $script .= "
  2070. /**
  2071. * Returns NULL since this table doesn't have a primary key.
  2072. * This method exists only for BC and is deprecated!
  2073. * @return null
  2074. */
  2075. public function getPrimaryKey()
  2076. {
  2077. return null;
  2078. }
  2079. ";
  2080. }
  2081. /**
  2082. * Adds the correct setPrimaryKey() method for this object.
  2083. * @param string &$script The script will be modified in this method.
  2084. */
  2085. protected function addSetPrimaryKey(&$script)
  2086. {
  2087. $pkeys = $this->getTable()->getPrimaryKey();
  2088. if (count($pkeys) == 1) {
  2089. $this->addSetPrimaryKey_SinglePK($script);
  2090. } elseif (count($pkeys) > 1) {
  2091. $this->addSetPrimaryKey_MultiPK($script);
  2092. } else {
  2093. // no primary key -- this is deprecated, since we don't *need* this method anymore
  2094. $this->addSetPrimaryKey_NoPK($script);
  2095. }
  2096. }
  2097. /**
  2098. * Adds the setPrimaryKey() method for tables that contain a single-column primary key.
  2099. * @param string &$script The script will be modified in this method.
  2100. */
  2101. protected function addSetPrimaryKey_SinglePK(&$script)
  2102. {
  2103. $pkeys = $this->getTable()->getPrimaryKey();
  2104. $col = $pkeys[0];
  2105. $clo=strtolower($col->getName());
  2106. $ctype = $col->getPhpType();
  2107. $script .= "
  2108. /**
  2109. * Generic method to set the primary key ($clo column).
  2110. *
  2111. * @param $ctype \$key Primary key.
  2112. * @return void
  2113. */
  2114. public function setPrimaryKey(\$key)
  2115. {
  2116. \$this->set".$col->getPhpName()."(\$key);
  2117. }
  2118. ";
  2119. } // addSetPrimaryKey_SinglePK
  2120. /**
  2121. * Adds the setPrimaryKey() method for tables that contain a multi-columnprimary key.
  2122. * @param string &$script The script will be modified in this method.
  2123. */
  2124. protected function addSetPrimaryKey_MultiPK(&$script)
  2125. {
  2126. $script .="
  2127. /**
  2128. * Set the [composite] primary key.
  2129. *
  2130. * @param array \$keys The elements of the composite key (order must match the order in XML file).
  2131. * @return void
  2132. */
  2133. public function setPrimaryKey(\$keys)
  2134. {
  2135. ";
  2136. $i = 0;
  2137. foreach ($this->getTable()->getPrimaryKey() as $pk) {
  2138. $pktype = $pk->getPhpType();
  2139. $script .= "
  2140. \$this->set".$pk->getPhpName()."(\$keys[$i]);
  2141. ";
  2142. $i++;
  2143. } /* foreach ($table->getPrimaryKey() */
  2144. $script .= "
  2145. }
  2146. ";
  2147. } // addSetPrimaryKey_MultiPK
  2148. /**
  2149. * Adds the setPrimaryKey() method for objects that have no primary key.
  2150. * This "feature" is dreprecated, since the setPrimaryKey() method is not required
  2151. * by the Persistent interface (or used by the templates). Hence, this method is also
  2152. * deprecated.
  2153. * @param string &$script The script will be modified in this method.
  2154. * @deprecated
  2155. */
  2156. protected function addSetPrimaryKey_NoPK(&$script)
  2157. {
  2158. $script .="
  2159. /**
  2160. * Dummy primary key setter.
  2161. *
  2162. * This function only exists to preserve backwards compatibility. It is no longer
  2163. * needed or required by the Persistent interface. It will be removed in next BC-breaking
  2164. * release of Propel.
  2165. *
  2166. * @deprecated
  2167. */
  2168. public function setPrimaryKey(\$pk)
  2169. {
  2170. // do nothing, because this object doesn't have any primary keys
  2171. }
  2172. ";
  2173. }
  2174. // --------------------------------------------------------------------
  2175. // Complex OM Methods
  2176. // --------------------------------------------------------------------
  2177. /**
  2178. * Constructs variable name for fkey-related objects.
  2179. * @param ForeignKey $fk
  2180. * @return string
  2181. */
  2182. protected function getFKVarName(ForeignKey $fk)
  2183. {
  2184. return 'a' . $this->getFKPhpNameAffix($fk, $plural = false);
  2185. }
  2186. /**
  2187. * Constructs variable name for objects which referencing current table by specified foreign key.
  2188. * @param ForeignKey $fk
  2189. * @return string
  2190. */
  2191. protected function getRefFKCollVarName(ForeignKey $fk)
  2192. {
  2193. return 'coll' . $this->getRefFKPhpNameAffix($fk, $plural = true);
  2194. }
  2195. /**
  2196. * Constructs variable name for single object which references current table by specified foreign key
  2197. * which is ALSO a primary key (hence one-to-one relationship).
  2198. * @param ForeignKey $fk
  2199. * @return string
  2200. */
  2201. protected function getPKRefFKVarName(ForeignKey $fk)
  2202. {
  2203. return 'single' . $this->getRefFKPhpNameAffix($fk, $plural = false);
  2204. }
  2205. /**
  2206. * Gets variable name for the Criteria which was used to fetch the objects which
  2207. * referencing current table by specified foreign key.
  2208. * @param ForeignKey $fk
  2209. * @return string
  2210. */
  2211. protected function getRefFKLastCriteriaVarName(ForeignKey $fk)
  2212. {
  2213. return 'last' . $this->getRefFKPhpNameAffix($fk, $plural = false) . 'Criteria';
  2214. }
  2215. // ----------------------------------------------------------------
  2216. //
  2217. // F K M E T H O D S
  2218. //
  2219. // ----------------------------------------------------------------
  2220. /**
  2221. * Adds the methods that get & set objects related by foreign key to the current object.
  2222. * @param string &$script The script will be modified in this method.
  2223. */
  2224. protected function addFKMethods(&$script)
  2225. {
  2226. foreach ($this->getTable()->getForeignKeys() as $fk) {
  2227. $this->addFKMutator($script, $fk);
  2228. $this->addFKAccessor($script, $fk);
  2229. } // foreach fk
  2230. }
  2231. /**
  2232. * Adds the class attributes that are needed to store fkey related objects.
  2233. * @param string &$script The script will be modified in this method.
  2234. */
  2235. protected function addFKAttributes(&$script, ForeignKey $fk)
  2236. {
  2237. $className = $this->getForeignTable($fk)->getPhpName();
  2238. $varName = $this->getFKVarName($fk);
  2239. $script .= "
  2240. /**
  2241. * @var $className
  2242. */
  2243. protected $".$varName.";
  2244. ";
  2245. }
  2246. /**
  2247. * Adds the mutator (setter) method for setting an fkey related object.
  2248. * @param string &$script The script will be modified in this method.
  2249. */
  2250. protected function addFKMutator(&$script, ForeignKey $fk)
  2251. {
  2252. $table = $this->getTable();
  2253. $tblFK = $this->getForeignTable($fk);
  2254. $joinTableObjectBuilder = $this->getNewObjectBuilder($tblFK);
  2255. $className = $joinTableObjectBuilder->getObjectClassname();
  2256. $varName = $this->getFKVarName($fk);
  2257. $script .= "
  2258. /**
  2259. * Declares an association between this object and a $className object.
  2260. *
  2261. * @param $className \$v
  2262. * @return ".$this->getObjectClassname()." The current object (for fluent API support)
  2263. * @throws PropelException
  2264. */
  2265. public function set".$this->getFKPhpNameAffix($fk, $plural = false)."($className \$v = null)
  2266. {";
  2267. foreach ($fk->getLocalColumns() as $columnName) {
  2268. $column = $table->getColumn($columnName);
  2269. $lfmap = $fk->getLocalForeignMapping();
  2270. $colFKName = $lfmap[$columnName];
  2271. $colFK = $tblFK->getColumn($colFKName);
  2272. $script .= "
  2273. if (\$v === null) {
  2274. \$this->set".$column->getPhpName()."(".$this->getDefaultValueString($column).");
  2275. } else {
  2276. \$this->set".$column->getPhpName()."(\$v->get".$colFK->getPhpName()."());
  2277. }
  2278. ";
  2279. } /* foreach local col */
  2280. $script .= "
  2281. \$this->$varName = \$v;
  2282. ";
  2283. // Now add bi-directional relationship binding, taking into account whether this is
  2284. // a one-to-one relationship.
  2285. if ($fk->isLocalPrimaryKey()) {
  2286. $script .= "
  2287. // Add binding for other direction of this 1:1 relationship.
  2288. if (\$v !== null) {
  2289. \$v->set".$this->getRefFKPhpNameAffix($fk, $plural = false)."(\$this);
  2290. }
  2291. ";
  2292. } else {
  2293. $script .= "
  2294. // Add binding for other direction of this n:n relationship.
  2295. // If this object has already been added to the $className object, it will not be re-added.
  2296. if (\$v !== null) {
  2297. \$v->add".$this->getRefFKPhpNameAffix($fk, $plural = false)."(\$this);
  2298. }
  2299. ";
  2300. }
  2301. $script .= "
  2302. return \$this;
  2303. }
  2304. ";
  2305. }
  2306. /**
  2307. * Adds the accessor (getter) method for getting an fkey related object.
  2308. * @param string &$script The script will be modified in this method.
  2309. */
  2310. protected function addFKAccessor(&$script, ForeignKey $fk)
  2311. {
  2312. $table = $this->getTable();
  2313. $varName = $this->getFKVarName($fk);
  2314. $pCollName = $this->getFKPhpNameAffix($fk, $plural = true);
  2315. $fkPeerBuilder = $this->getNewPeerBuilder($this->getForeignTable($fk));
  2316. $fkObjectBuilder = $this->getNewObjectBuilder($this->getForeignTable($fk))->getStubObjectBuilder();
  2317. $className = $fkObjectBuilder->getClassname(); // get the Classname that has maybe a prefix
  2318. $and = "";
  2319. $comma = "";
  2320. $conditional = "";
  2321. $argmap = array(); // foreign -> local mapping
  2322. $argsize = 0;
  2323. foreach ($fk->getLocalColumns() as $columnName) {
  2324. $lfmap = $fk->getLocalForeignMapping();
  2325. $localColumn = $table->getColumn($columnName);
  2326. $foreignColumn = $fk->getForeignTable()->getColumn($lfmap[$columnName]);
  2327. $column = $table->getColumn($columnName);
  2328. $cptype = $column->getPhpType();
  2329. $clo = strtolower($column->getName());
  2330. if ($cptype == "integer" || $cptype == "float" || $cptype == "double") {
  2331. $conditional .= $and . "\$this->". $clo ." != 0";
  2332. } elseif ($cptype == "string") {
  2333. $conditional .= $and . "(\$this->" . $clo ." !== \"\" && \$this->".$clo." !== null)";
  2334. } else {
  2335. $conditional .= $and . "\$this->" . $clo ." !== null";
  2336. }
  2337. $argmap[] = array('foreign' => $foreignColumn, 'local' => $localColumn);
  2338. $and = " && ";
  2339. $comma = ", ";
  2340. $argsize = $argsize + 1;
  2341. }
  2342. // If the related column is a primary kay and if it's a simple association,
  2343. // The use retrieveByPk() instead of doSelect() to take advantage of instance pooling
  2344. $useRetrieveByPk = count($argmap) == 1 && $argmap[0]['foreign']->isPrimaryKey();
  2345. $script .= "
  2346. /**
  2347. * Get the associated $className object
  2348. *
  2349. * @param PropelPDO Optional Connection object.
  2350. * @return $className The associated $className object.
  2351. * @throws PropelException
  2352. */
  2353. public function get".$this->getFKPhpNameAffix($fk, $plural = false)."(PropelPDO \$con = null)
  2354. {";
  2355. $script .= "
  2356. if (\$this->$varName === null && ($conditional)) {";
  2357. if ($useRetrieveByPk) {
  2358. $script .= "
  2359. \$this->$varName = ".$fkPeerBuilder->getPeerClassname()."::retrieveByPk(\$this->$clo);";
  2360. } else {
  2361. $script .= "
  2362. \$c = new Criteria(".$fkPeerBuilder->getPeerClassname()."::DATABASE_NAME);";
  2363. foreach ($argmap as $el) {
  2364. $fcol = $el['foreign'];
  2365. $lcol = $el['local'];
  2366. $clo = strtolower($lcol->getName());
  2367. $script .= "
  2368. \$c->add(".$fkPeerBuilder->getColumnConstant($fcol).", \$this->".$clo.");";
  2369. }
  2370. $script .= "
  2371. \$this->$varName = ".$fkPeerBuilder->getPeerClassname()."::doSelectOne(\$c, \$con);";
  2372. }
  2373. if ($fk->isLocalPrimaryKey()) {
  2374. $script .= "
  2375. // Because this foreign key represents a one-to-one relationship, we will create a bi-directional association.
  2376. \$this->{$varName}->set".$this->getRefFKPhpNameAffix($fk, $plural = false)."(\$this);";
  2377. } else {
  2378. $script .= "
  2379. /* The following can be used additionally to
  2380. guarantee the related object contains a reference
  2381. to this object. This level of coupling may, however, be
  2382. undesirable since it could result in an only partially populated collection
  2383. in the referenced object.
  2384. \$this->{$varName}->add".$this->getRefFKPhpNameAffix($fk, $plural = true)."(\$this);
  2385. */";
  2386. }
  2387. $script .= "
  2388. }
  2389. return \$this->$varName;
  2390. }
  2391. ";
  2392. } // addFKAccessor
  2393. /**
  2394. * Adds a convenience method for setting a related object by specifying the primary key.
  2395. * This can be used in conjunction with the getPrimaryKey() for systems where nothing is known
  2396. * about the actual objects being related.
  2397. * @param string &$script The script will be modified in this method.
  2398. */
  2399. protected function addFKByKeyMutator(&$script, ForeignKey $fk)
  2400. {
  2401. $table = $this->getTable();
  2402. #$className = $this->getForeignTable($fk)->getPhpName();
  2403. $methodAffix = $this->getFKPhpNameAffix($fk);
  2404. #$varName = $this->getFKVarName($fk);
  2405. $script .= "
  2406. /**
  2407. * Provides convenient way to set a relationship based on a
  2408. * key. e.g.
  2409. * <code>\$bar->setFooKey(\$foo->getPrimaryKey())</code>
  2410. *";
  2411. if (count($fk->getLocalColumns()) > 1) {
  2412. $script .= "
  2413. * Note: It is important that the xml schema used to create this class
  2414. * maintains consistency in the order of related columns between
  2415. * ".$table->getName()." and ". $tblFK->getName().".
  2416. * If for some reason this is impossible, this method should be
  2417. * overridden in <code>".$table->getPhpName()."</code>.";
  2418. }
  2419. $script .= "
  2420. * @return ".$this->getObjectClassname()." The current object (for fluent API support)
  2421. * @throws PropelException
  2422. */
  2423. public function set".$methodAffix."Key(\$key)
  2424. {
  2425. ";
  2426. if (count($fk->getLocalColumns()) > 1) {
  2427. $i = 0;
  2428. foreach ($fk->getLocalColumns() as $colName) {
  2429. $col = $table->getColumn($colName);
  2430. $fktype = $col->getPhpType();
  2431. $script .= "
  2432. \$this->set".$col->getPhpName()."( ($fktype) \$key[$i] );
  2433. ";
  2434. $i++;
  2435. } /* foreach */
  2436. } else {
  2437. $lcols = $fk->getLocalColumns();
  2438. $colName = $lcols[0];
  2439. $col = $table->getColumn($colName);
  2440. $fktype = $col->getPhpType();
  2441. $script .= "
  2442. \$this->set".$col->getPhpName()."( ($fktype) \$key);
  2443. ";
  2444. }
  2445. $script .= "
  2446. return \$this;
  2447. }
  2448. ";
  2449. } // addFKByKeyMutator()
  2450. /**
  2451. * Adds the method that fetches fkey-related (referencing) objects but also joins in data from another table.
  2452. * @param string &$script The script will be modified in this method.
  2453. */
  2454. protected function addRefFKGetJoinMethods(&$script, ForeignKey $refFK)
  2455. {
  2456. $table = $this->getTable();
  2457. $tblFK = $refFK->getTable();
  2458. $join_behavior = $this->getGeneratorConfig()->getBuildProperty('useLeftJoinsInDoJoinMethods') ? 'Criteria::LEFT_JOIN' : 'Criteria::INNER_JOIN';
  2459. $peerClassname = $this->getStubPeerBuilder()->getClassname();
  2460. $relCol = $this->getRefFKPhpNameAffix($refFK, $plural=true);
  2461. $collName = $this->getRefFKCollVarName($refFK);
  2462. $lastCriteriaName = $this->getRefFKLastCriteriaVarName($refFK);
  2463. $fkPeerBuilder = $this->getNewPeerBuilder($tblFK);
  2464. $lastTable = "";
  2465. foreach ($tblFK->getForeignKeys() as $fk2) {
  2466. $tblFK2 = $this->getForeignTable($fk2);
  2467. $doJoinGet = !$tblFK2->isForReferenceOnly();
  2468. // it doesn't make sense to join in rows from the curent table, since we are fetching
  2469. // objects related to *this* table (i.e. the joined rows will all be the same row as current object)
  2470. if ($this->getTable()->getPhpName() == $tblFK2->getPhpName()) {
  2471. $doJoinGet = false;
  2472. }
  2473. $relCol2 = $this->getFKPhpNameAffix($fk2, $plural = false);
  2474. if ( $this->getRelatedBySuffix($refFK) != "" &&
  2475. ($this->getRelatedBySuffix($refFK) == $this->getRelatedBySuffix($fk2))) {
  2476. $doJoinGet = false;
  2477. }
  2478. if ($doJoinGet) {
  2479. $script .= "
  2480. /**
  2481. * If this collection has already been initialized with
  2482. * an identical criteria, it returns the collection.
  2483. * Otherwise if this ".$table->getPhpName()." is new, it will return
  2484. * an empty collection; or if this ".$table->getPhpName()." has previously
  2485. * been saved, it will retrieve related $relCol from storage.
  2486. *
  2487. * This method is protected by default in order to keep the public
  2488. * api reasonable. You can provide public methods for those you
  2489. * actually need in ".$table->getPhpName().".
  2490. */
  2491. public function get".$relCol."Join".$relCol2."(\$criteria = null, \$con = null, \$join_behavior = $join_behavior)
  2492. {";
  2493. $script .= "
  2494. if (\$criteria === null) {
  2495. \$criteria = new Criteria($peerClassname::DATABASE_NAME);
  2496. }
  2497. elseif (\$criteria instanceof Criteria)
  2498. {
  2499. \$criteria = clone \$criteria;
  2500. }
  2501. if (\$this->$collName === null) {
  2502. if (\$this->isNew()) {
  2503. \$this->$collName = array();
  2504. } else {
  2505. ";
  2506. foreach ($refFK->getForeignColumns() as $columnName) {
  2507. $column = $table->getColumn($columnName);
  2508. $flMap = $refFK->getForeignLocalMapping();
  2509. $colFKName = $flMap[$columnName];
  2510. $colFK = $tblFK->getColumn($colFKName);
  2511. if ($colFK === null) {
  2512. throw new EngineException("Column $colFKName not found in " . $tblFK->getName());
  2513. }
  2514. $clo = strtolower($column->getName());
  2515. $script .= "
  2516. \$criteria->add(".$fkPeerBuilder->getColumnConstant($colFK).", \$this->$clo);
  2517. ";
  2518. } // end foreach ($fk->getForeignColumns()
  2519. $script .= "
  2520. \$this->$collName = ".$fkPeerBuilder->getPeerClassname()."::doSelectJoin$relCol2(\$criteria, \$con, \$join_behavior);
  2521. }
  2522. } else {
  2523. // the following code is to determine if a new query is
  2524. // called for. If the criteria is the same as the last
  2525. // one, just return the collection.
  2526. ";
  2527. foreach ($refFK->getForeignColumns() as $columnName) {
  2528. $column = $table->getColumn($columnName);
  2529. $flMap = $refFK->getForeignLocalMapping();
  2530. $colFKName = $flMap[$columnName];
  2531. $colFK = $tblFK->getColumn($colFKName);
  2532. $clo = strtolower($column->getName());
  2533. $script .= "
  2534. \$criteria->add(".$fkPeerBuilder->getColumnConstant($colFK).", \$this->$clo);
  2535. ";
  2536. } /* end foreach ($fk->getForeignColumns() */
  2537. $script .= "
  2538. if (!isset(\$this->$lastCriteriaName) || !\$this->".$lastCriteriaName."->equals(\$criteria)) {
  2539. \$this->$collName = ".$fkPeerBuilder->getPeerClassname()."::doSelectJoin$relCol2(\$criteria, \$con, \$join_behavior);
  2540. }
  2541. }
  2542. \$this->$lastCriteriaName = \$criteria;
  2543. return \$this->$collName;
  2544. }
  2545. ";
  2546. } /* end if ($doJoinGet) */
  2547. } /* end foreach ($tblFK->getForeignKeys() as $fk2) { */
  2548. } // function
  2549. // ----------------------------------------------------------------
  2550. //
  2551. // R E F E R R E R F K M E T H O D S
  2552. //
  2553. // ----------------------------------------------------------------
  2554. /**
  2555. * Adds the attributes used to store objects that have referrer fkey relationships to this object.
  2556. * <code>protected collVarName;</code>
  2557. * <code>private lastVarNameCriteria = null;</code>
  2558. * @param string &$script The script will be modified in this method.
  2559. */
  2560. protected function addRefFKAttributes(&$script, ForeignKey $refFK)
  2561. {
  2562. $joinedTableObjectBuilder = $this->getNewObjectBuilder($refFK->getTable());
  2563. $className = $joinedTableObjectBuilder->getObjectClassname();
  2564. if ($refFK->isLocalPrimaryKey()) {
  2565. $script .= "
  2566. /**
  2567. * @var $className one-to-one related $className object
  2568. */
  2569. protected $".$this->getPKRefFKVarName($refFK).";
  2570. ";
  2571. } else {
  2572. $script .= "
  2573. /**
  2574. * @var array {$className}[] Collection to store aggregation of $className objects.
  2575. */
  2576. protected $".$this->getRefFKCollVarName($refFK).";
  2577. /**
  2578. * @var Criteria The criteria used to select the current contents of ".$this->getRefFKCollVarName($refFK).".
  2579. */
  2580. private $".$this->getRefFKLastCriteriaVarName($refFK)." = null;
  2581. ";
  2582. }
  2583. }
  2584. /**
  2585. * Adds the methods for retrieving, initializing, adding objects that are related to this one by foreign keys.
  2586. * @param string &$script The script will be modified in this method.
  2587. */
  2588. protected function addRefFKMethods(&$script)
  2589. {
  2590. foreach ($this->getTable()->getReferrers() as $refFK) {
  2591. if ($refFK->isLocalPrimaryKey()) {
  2592. $this->addPKRefFKGet($script, $refFK);
  2593. $this->addPKRefFKSet($script, $refFK);
  2594. } else {
  2595. $this->addRefFKClear($script, $refFK);
  2596. $this->addRefFKInit($script, $refFK);
  2597. $this->addRefFKGet($script, $refFK);
  2598. $this->addRefFKCount($script, $refFK);
  2599. $this->addRefFKAdd($script, $refFK);
  2600. $this->addRefFKGetJoinMethods($script, $refFK);
  2601. }
  2602. }
  2603. }
  2604. /**
  2605. * Adds the method that clears the referrer fkey collection.
  2606. * @param string &$script The script will be modified in this method.
  2607. */
  2608. protected function addRefFKClear(&$script, ForeignKey $refFK) {
  2609. $relCol = $this->getRefFKPhpNameAffix($refFK, $plural = true);
  2610. $collName = $this->getRefFKCollVarName($refFK);
  2611. $script .= "
  2612. /**
  2613. * Clears out the $collName collection (array).
  2614. *
  2615. * This does not modify the database; however, it will remove any associated objects, causing
  2616. * them to be refetched by subsequent calls to accessor method.
  2617. *
  2618. * @return void
  2619. * @see add$relCol()
  2620. */
  2621. public function clear$relCol()
  2622. {
  2623. \$this->$collName = null; // important to set this to NULL since that means it is uninitialized
  2624. }
  2625. ";
  2626. } // addRefererClear()
  2627. /**
  2628. * Adds the method that initializes the referrer fkey collection.
  2629. * @param string &$script The script will be modified in this method.
  2630. */
  2631. protected function addRefFKInit(&$script, ForeignKey $refFK) {
  2632. $relCol = $this->getRefFKPhpNameAffix($refFK, $plural = true);
  2633. $collName = $this->getRefFKCollVarName($refFK);
  2634. $script .= "
  2635. /**
  2636. * Initializes the $collName collection (array).
  2637. *
  2638. * By default this just sets the $collName collection to an empty array (like clear$collName());
  2639. * however, you may wish to override this method in your stub class to provide setting appropriate
  2640. * to your application -- for example, setting the initial array to the values stored in database.
  2641. *
  2642. * @return void
  2643. */
  2644. public function init$relCol()
  2645. {
  2646. \$this->$collName = array();
  2647. }
  2648. ";
  2649. } // addRefererInit()
  2650. /**
  2651. * Adds the method that adds an object into the referrer fkey collection.
  2652. * @param string &$script The script will be modified in this method.
  2653. */
  2654. protected function addRefFKAdd(&$script, ForeignKey $refFK)
  2655. {
  2656. $tblFK = $refFK->getTable();
  2657. $joinedTableObjectBuilder = $this->getNewObjectBuilder($refFK->getTable());
  2658. $className = $joinedTableObjectBuilder->getObjectClassname();
  2659. $collName = $this->getRefFKCollVarName($refFK);
  2660. $script .= "
  2661. /**
  2662. * Method called to associate a $className object to this object
  2663. * through the $className foreign key attribute.
  2664. *
  2665. * @param $className \$l $className
  2666. * @return void
  2667. * @throws PropelException
  2668. */
  2669. public function add".$this->getRefFKPhpNameAffix($refFK, $plural = false)."($className \$l)
  2670. {
  2671. if (\$this->$collName === null) {
  2672. \$this->init".$this->getRefFKPhpNameAffix($refFK, $plural = true)."();
  2673. }
  2674. if (!in_array(\$l, \$this->$collName, true)) { // only add it if the **same** object is not already associated
  2675. array_push(\$this->$collName, \$l);
  2676. \$l->set".$this->getFKPhpNameAffix($refFK, $plural = false)."(\$this);
  2677. }
  2678. }
  2679. ";
  2680. } // addRefererAdd
  2681. /**
  2682. * Adds the method that returns the size of the referrer fkey collection.
  2683. * @param string &$script The script will be modified in this method.
  2684. */
  2685. protected function addRefFKCount(&$script, ForeignKey $refFK)
  2686. {
  2687. $table = $this->getTable();
  2688. $tblFK = $refFK->getTable();
  2689. $peerClassname = $this->getStubPeerBuilder()->getClassname();
  2690. $fkPeerBuilder = $this->getNewPeerBuilder($refFK->getTable());
  2691. $relCol = $this->getRefFKPhpNameAffix($refFK, $plural = true);
  2692. $collName = $this->getRefFKCollVarName($refFK);
  2693. $lastCriteriaName = $this->getRefFKLastCriteriaVarName($refFK);
  2694. $className = $fkPeerBuilder->getObjectClassname();
  2695. $script .= "
  2696. /**
  2697. * Returns the number of related $className objects.
  2698. *
  2699. * @param Criteria \$criteria
  2700. * @param boolean \$distinct
  2701. * @param PropelPDO \$con
  2702. * @return int Count of related $className objects.
  2703. * @throws PropelException
  2704. */
  2705. public function count$relCol(Criteria \$criteria = null, \$distinct = false, PropelPDO \$con = null)
  2706. {";
  2707. $script .= "
  2708. if (\$criteria === null) {
  2709. \$criteria = new Criteria($peerClassname::DATABASE_NAME);
  2710. } else {
  2711. \$criteria = clone \$criteria;
  2712. }
  2713. if (\$distinct) {
  2714. \$criteria->setDistinct();
  2715. }
  2716. \$count = null;
  2717. if (\$this->$collName === null) {
  2718. if (\$this->isNew()) {
  2719. \$count = 0;
  2720. } else {
  2721. ";
  2722. foreach ($refFK->getLocalColumns() as $colFKName) {
  2723. // $colFKName is local to the referring table (i.e. foreign to this table)
  2724. $lfmap = $refFK->getLocalForeignMapping();
  2725. $localColumn = $this->getTable()->getColumn($lfmap[$colFKName]);
  2726. $colFK = $refFK->getTable()->getColumn($colFKName);
  2727. $clo = strtolower($localColumn->getName());
  2728. $script .= "
  2729. \$criteria->add(".$fkPeerBuilder->getColumnConstant($colFK).", \$this->$clo);
  2730. ";
  2731. } // end foreach ($fk->getForeignColumns()
  2732. $script .= "
  2733. \$count = ".$fkPeerBuilder->getPeerClassname()."::doCount(\$criteria, false, \$con);
  2734. }
  2735. } else {
  2736. // criteria has no effect for a new object
  2737. if (!\$this->isNew()) {
  2738. // the following code is to determine if a new query is
  2739. // called for. If the criteria is the same as the last
  2740. // one, just return count of the collection.
  2741. ";
  2742. foreach ($refFK->getLocalColumns() as $colFKName) {
  2743. // $colFKName is local to the referring table (i.e. foreign to this table)
  2744. $lfmap = $refFK->getLocalForeignMapping();
  2745. $localColumn = $this->getTable()->getColumn($lfmap[$colFKName]);
  2746. $colFK = $refFK->getTable()->getColumn($colFKName);
  2747. $clo = strtolower($localColumn->getName());
  2748. $script .= "
  2749. \$criteria->add(".$fkPeerBuilder->getColumnConstant($colFK).", \$this->$clo);
  2750. ";
  2751. } // foreach ($fk->getForeignColumns()
  2752. $script .= "
  2753. if (!isset(\$this->$lastCriteriaName) || !\$this->".$lastCriteriaName."->equals(\$criteria)) {
  2754. \$count = ".$fkPeerBuilder->getPeerClassname()."::doCount(\$criteria, false, \$con);
  2755. } else {
  2756. \$count = count(\$this->$collName);
  2757. }
  2758. } else {
  2759. \$count = count(\$this->$collName);
  2760. }
  2761. }
  2762. return \$count;
  2763. }
  2764. ";
  2765. } // addRefererCount
  2766. /**
  2767. * Adds the method that returns the referrer fkey collection.
  2768. * @param string &$script The script will be modified in this method.
  2769. */
  2770. protected function addRefFKGet(&$script, ForeignKey $refFK)
  2771. {
  2772. $table = $this->getTable();
  2773. $tblFK = $refFK->getTable();
  2774. $peerClassname = $this->getStubPeerBuilder()->getClassname();
  2775. $fkPeerBuilder = $this->getNewPeerBuilder($refFK->getTable());
  2776. $relCol = $this->getRefFKPhpNameAffix($refFK, $plural = true);
  2777. $collName = $this->getRefFKCollVarName($refFK);
  2778. $lastCriteriaName = $this->getRefFKLastCriteriaVarName($refFK);
  2779. $className = $fkPeerBuilder->getObjectClassname();
  2780. $script .= "
  2781. /**
  2782. * Gets an array of $className objects which contain a foreign key that references this object.
  2783. *
  2784. * If this collection has already been initialized with an identical Criteria, it returns the collection.
  2785. * Otherwise if this ".$this->getObjectClassname()." has previously been saved, it will retrieve
  2786. * related $relCol from storage. If this ".$this->getObjectClassname()." is new, it will return
  2787. * an empty collection or the current collection, the criteria is ignored on a new object.
  2788. *
  2789. * @param PropelPDO \$con
  2790. * @param Criteria \$criteria
  2791. * @return array {$className}[]
  2792. * @throws PropelException
  2793. */
  2794. public function get$relCol(\$criteria = null, PropelPDO \$con = null)
  2795. {";
  2796. $script .= "
  2797. if (\$criteria === null) {
  2798. \$criteria = new Criteria($peerClassname::DATABASE_NAME);
  2799. }
  2800. elseif (\$criteria instanceof Criteria)
  2801. {
  2802. \$criteria = clone \$criteria;
  2803. }
  2804. if (\$this->$collName === null) {
  2805. if (\$this->isNew()) {
  2806. \$this->$collName = array();
  2807. } else {
  2808. ";
  2809. foreach ($refFK->getLocalColumns() as $colFKName) {
  2810. // $colFKName is local to the referring table (i.e. foreign to this table)
  2811. $lfmap = $refFK->getLocalForeignMapping();
  2812. $localColumn = $this->getTable()->getColumn($lfmap[$colFKName]);
  2813. $colFK = $refFK->getTable()->getColumn($colFKName);
  2814. $clo = strtolower($localColumn->getName());
  2815. $script .= "
  2816. \$criteria->add(".$fkPeerBuilder->getColumnConstant($colFK).", \$this->$clo);
  2817. ";
  2818. } // end foreach ($fk->getForeignColumns()
  2819. $script .= "
  2820. ".$fkPeerBuilder->getPeerClassname()."::addSelectColumns(\$criteria);
  2821. \$this->$collName = ".$fkPeerBuilder->getPeerClassname()."::doSelect(\$criteria, \$con);
  2822. }
  2823. } else {
  2824. // criteria has no effect for a new object
  2825. if (!\$this->isNew()) {
  2826. // the following code is to determine if a new query is
  2827. // called for. If the criteria is the same as the last
  2828. // one, just return the collection.
  2829. ";
  2830. foreach ($refFK->getLocalColumns() as $colFKName) {
  2831. // $colFKName is local to the referring table (i.e. foreign to this table)
  2832. $lfmap = $refFK->getLocalForeignMapping();
  2833. $localColumn = $this->getTable()->getColumn($lfmap[$colFKName]);
  2834. $colFK = $refFK->getTable()->getColumn($colFKName);
  2835. $clo = strtolower($localColumn->getName());
  2836. $script .= "
  2837. \$criteria->add(".$fkPeerBuilder->getColumnConstant($colFK).", \$this->$clo);
  2838. ";
  2839. } // foreach ($fk->getForeignColumns()
  2840. $script .= "
  2841. ".$fkPeerBuilder->getPeerClassname()."::addSelectColumns(\$criteria);
  2842. if (!isset(\$this->$lastCriteriaName) || !\$this->".$lastCriteriaName."->equals(\$criteria)) {
  2843. \$this->$collName = ".$fkPeerBuilder->getPeerClassname()."::doSelect(\$criteria, \$con);
  2844. }
  2845. }
  2846. }
  2847. \$this->$lastCriteriaName = \$criteria;
  2848. return \$this->$collName;
  2849. }
  2850. ";
  2851. } // addRefererGet()
  2852. /**
  2853. * Adds the method that gets a one-to-one related referrer fkey.
  2854. * This is for one-to-one relationship special case.
  2855. * @param string &$script The script will be modified in this method.
  2856. */
  2857. protected function addPKRefFKGet(&$script, ForeignKey $refFK)
  2858. {
  2859. $table = $this->getTable();
  2860. $tblFK = $refFK->getTable();
  2861. $joinedTableObjectBuilder = $this->getNewObjectBuilder($refFK->getTable());
  2862. $joinedTablePeerBuilder = $this->getNewObjectBuilder($refFK->getTable());
  2863. $className = $joinedTableObjectBuilder->getObjectClassname();
  2864. $varName = $this->getPKRefFKVarName($refFK);
  2865. $script .= "
  2866. /**
  2867. * Gets a single $className object, which is related to this object by a one-to-one relationship.
  2868. *
  2869. * @param PropelPDO \$con
  2870. * @return $className
  2871. * @throws PropelException
  2872. */
  2873. public function get".$this->getRefFKPhpNameAffix($refFK, $plural = false)."(PropelPDO \$con = null)
  2874. {
  2875. ";
  2876. $script .= "
  2877. if (\$this->$varName === null && !\$this->isNew()) {";
  2878. $lfmap = $refFK->getLocalForeignMapping();
  2879. // remember: this object represents the foreign table,
  2880. // so we need foreign columns of the reffk to know the local columns
  2881. // that we need to set :)
  2882. $localcols = $refFK->getForeignColumns();
  2883. // we know that at least every column in the primary key of the foreign table
  2884. // is represented in this foreign key
  2885. $params = array();
  2886. foreach ($tblFK->getPrimaryKey() as $col) {
  2887. $localColumn = $table->getColumn($lfmap[$col->getName()]);
  2888. $clo = strtolower($localColumn->getName());
  2889. $params[] = "\$this->$clo";
  2890. }
  2891. $script .= "
  2892. \$this->$varName = ".$joinedTableObjectBuilder->getPeerClassname()."::retrieveByPK(".implode(", ", $params).", \$con);
  2893. }
  2894. return \$this->$varName;
  2895. }
  2896. ";
  2897. } // addPKRefFKGet()
  2898. /**
  2899. * Adds the method that sets a one-to-one related referrer fkey.
  2900. * This is for one-to-one relationships special case.
  2901. * @param string &$script The script will be modified in this method.
  2902. * @param ForeignKey $refFK The referencing foreign key.
  2903. */
  2904. protected function addPKRefFKSet(&$script, ForeignKey $refFK)
  2905. {
  2906. $tblFK = $refFK->getTable();
  2907. $joinedTableObjectBuilder = $this->getNewObjectBuilder($refFK->getTable());
  2908. $className = $joinedTableObjectBuilder->getObjectClassname();
  2909. $varName = $this->getPKRefFKVarName($refFK);
  2910. $script .= "
  2911. /**
  2912. * Sets a single $className object as related to this object by a one-to-one relationship.
  2913. *
  2914. * @param $className \$l $className
  2915. * @return ".$this->getObjectClassname()." The current object (for fluent API support)
  2916. * @throws PropelException
  2917. */
  2918. public function set".$this->getRefFKPhpNameAffix($refFK, $plural = false)."($className \$v)
  2919. {
  2920. \$this->$varName = \$v;
  2921. // Make sure that that the passed-in $className isn't already associated with this object
  2922. if (\$v->get".$this->getFKPhpNameAffix($refFK, $plural = false)."() === null) {
  2923. \$v->set".$this->getFKPhpNameAffix($refFK, $plural = false)."(\$this);
  2924. }
  2925. return \$this;
  2926. }
  2927. ";
  2928. } // addPKRefFKSet
  2929. // ----------------------------------------------------------------
  2930. //
  2931. // M A N I P U L A T I O N M E T H O D S
  2932. //
  2933. // ----------------------------------------------------------------
  2934. /**
  2935. * Adds the workhourse doSave() method.
  2936. * @param string &$script The script will be modified in this method.
  2937. */
  2938. protected function addDoSave(&$script)
  2939. {
  2940. $table = $this->getTable();
  2941. $reloadOnUpdate = $table->isReloadOnUpdate();
  2942. $reloadOnInsert = $table->isReloadOnInsert();
  2943. $script .= "
  2944. /**
  2945. * Performs the work of inserting or updating the row in the database.
  2946. *
  2947. * If the object is new, it inserts it; otherwise an update is performed.
  2948. * All related objects are also updated in this method.
  2949. *
  2950. * @param PropelPDO \$con";
  2951. if ($reloadOnUpdate || $reloadOnInsert) {
  2952. $script .= "
  2953. * @param boolean \$skipReload Whether to skip the reload for this object from database.";
  2954. }
  2955. $script .= "
  2956. * @return int The number of rows affected by this insert/update and any referring fk objects' save() operations.
  2957. * @throws PropelException
  2958. * @see save()
  2959. */
  2960. protected function doSave(PropelPDO \$con".($reloadOnUpdate || $reloadOnInsert ? ", \$skipReload = false" : "").")
  2961. {
  2962. \$affectedRows = 0; // initialize var to track total num of affected rows
  2963. if (!\$this->alreadyInSave) {
  2964. \$this->alreadyInSave = true;
  2965. ";
  2966. if ($reloadOnInsert || $reloadOnUpdate) {
  2967. $script .= "
  2968. \$reloadObject = false;
  2969. ";
  2970. }
  2971. if (count($table->getForeignKeys())) {
  2972. $script .= "
  2973. // We call the save method on the following object(s) if they
  2974. // were passed to this object by their coresponding set
  2975. // method. This object relates to these object(s) by a
  2976. // foreign key reference.
  2977. ";
  2978. foreach ($table->getForeignKeys() as $fk)
  2979. {
  2980. $aVarName = $this->getFKVarName($fk);
  2981. $script .= "
  2982. if (\$this->$aVarName !== null) {
  2983. if (\$this->".$aVarName."->isModified() || \$this->".$aVarName."->isNew()) {
  2984. \$affectedRows += \$this->".$aVarName."->save(\$con);
  2985. }
  2986. \$this->set".$this->getFKPhpNameAffix($fk, $plural = false)."(\$this->$aVarName);
  2987. }
  2988. ";
  2989. } // foreach foreign k
  2990. } // if (count(foreign keys))
  2991. if ($table->hasAutoIncrementPrimaryKey() ) {
  2992. $script .= "
  2993. if (\$this->isNew() ) {
  2994. \$this->modifiedColumns[] = " . $this->getColumnConstant($table->getAutoIncrementPrimaryKey() ) . ";
  2995. }";
  2996. }
  2997. $script .= "
  2998. // If this object has been modified, then save it to the database.
  2999. if (\$this->isModified()";
  3000. $script .= ") {
  3001. if (\$this->isNew()) {
  3002. \$pk = ".$this->getPeerClassname()."::doInsert(\$this, \$con);";
  3003. if ($reloadOnInsert) {
  3004. $script .= "
  3005. if (!\$skipReload) {
  3006. \$reloadObject = true;
  3007. }";
  3008. }
  3009. $script .= "
  3010. \$affectedRows += 1; // we are assuming that there is only 1 row per doInsert() which
  3011. // should always be true here (even though technically
  3012. // BasePeer::doInsert() can insert multiple rows).
  3013. ";
  3014. if ($table->getIdMethod() != IDMethod::NO_ID_METHOD) {
  3015. if (count($pks = $table->getPrimaryKey())) {
  3016. foreach ($pks as $pk) {
  3017. if ($pk->isAutoIncrement()) {
  3018. $script .= "
  3019. \$this->set".$pk->getPhpName()."(\$pk); //[IMV] update autoincrement primary key
  3020. ";
  3021. }
  3022. }
  3023. }
  3024. } // if (id method != "none")
  3025. $script .= "
  3026. \$this->setNew(false);
  3027. } else {";
  3028. if ($reloadOnUpdate) {
  3029. $script .= "
  3030. if (!\$skipReload) {
  3031. \$reloadObject = true;
  3032. }";
  3033. }
  3034. $script .= "
  3035. \$affectedRows += ".$this->getPeerClassname()."::doUpdate(\$this, \$con);
  3036. }
  3037. ";
  3038. // We need to rewind any LOB columns
  3039. foreach ($table->getColumns() as $col) {
  3040. $clo = strtolower($col->getName());
  3041. if ($col->isLobType()) {
  3042. $script .= "
  3043. // Rewind the $clo LOB column, since PDO does not rewind after inserting value.
  3044. if (\$this->$clo !== null && is_resource(\$this->$clo)) {
  3045. rewind(\$this->$clo);
  3046. }
  3047. ";
  3048. }
  3049. }
  3050. $script .= "
  3051. \$this->resetModified(); // [HL] After being saved an object is no longer 'modified'
  3052. }
  3053. ";
  3054. foreach ($table->getReferrers() as $refFK) {
  3055. if ($refFK->isLocalPrimaryKey()) {
  3056. $varName = $this->getPKRefFKVarName($refFK);
  3057. $script .= "
  3058. if (\$this->$varName !== null) {
  3059. if (!\$this->{$varName}->isDeleted()) {
  3060. \$affectedRows += \$this->{$varName}->save(\$con);
  3061. }
  3062. }
  3063. ";
  3064. } else {
  3065. $collName = $this->getRefFKCollVarName($refFK);
  3066. $script .= "
  3067. if (\$this->$collName !== null) {
  3068. foreach (\$this->$collName as \$referrerFK) {
  3069. if (!\$referrerFK->isDeleted()) {
  3070. \$affectedRows += \$referrerFK->save(\$con);
  3071. }
  3072. }
  3073. }
  3074. ";
  3075. } // if refFK->isLocalPrimaryKey()
  3076. } /* foreach getReferrers() */
  3077. $script .= "
  3078. \$this->alreadyInSave = false;
  3079. ";
  3080. if ($reloadOnInsert || $reloadOnUpdate) {
  3081. $script .= "
  3082. if (\$reloadObject) {
  3083. \$this->reload(\$con);
  3084. }
  3085. ";
  3086. }
  3087. $script .= "
  3088. }
  3089. return \$affectedRows;
  3090. } // doSave()
  3091. ";
  3092. }
  3093. /**
  3094. * Adds the $alreadyInSave attribute, which prevents attempting to re-save the same object.
  3095. * @param string &$script The script will be modified in this method.
  3096. */
  3097. protected function addAlreadyInSaveAttribute(&$script)
  3098. {
  3099. $script .= "
  3100. /**
  3101. * Flag to prevent endless save loop, if this object is referenced
  3102. * by another object which falls in this transaction.
  3103. * @var boolean
  3104. */
  3105. protected \$alreadyInSave = false;
  3106. ";
  3107. }
  3108. /**
  3109. * Adds the save() method.
  3110. * @param string &$script The script will be modified in this method.
  3111. */
  3112. protected function addSave(&$script)
  3113. {
  3114. $this->addSaveComment($script);
  3115. $this->addSaveOpen($script);
  3116. $this->addSaveBody($script);
  3117. $this->addSaveClose($script);
  3118. }
  3119. /**
  3120. * Adds the comment for the save method
  3121. * @param string &$script The script will be modified in this method.
  3122. * @see addSave()
  3123. **/
  3124. protected function addSaveComment(&$script) {
  3125. $table = $this->getTable();
  3126. $reloadOnUpdate = $table->isReloadOnUpdate();
  3127. $reloadOnInsert = $table->isReloadOnInsert();
  3128. $script .= "
  3129. /**
  3130. * Persists this object to the database.
  3131. *
  3132. * If the object is new, it inserts it; otherwise an update is performed.
  3133. * All modified related objects will also be persisted in the doSave()
  3134. * method. This method wraps all precipitate database operations in a
  3135. * single transaction.";
  3136. if ($reloadOnUpdate) {
  3137. $script .= "
  3138. *
  3139. * Since this table was configured to reload rows on update, the object will
  3140. * be reloaded from the database if an UPDATE operation is performed (unless
  3141. * the \$skipReload parameter is TRUE).";
  3142. }
  3143. if ($reloadOnInsert) {
  3144. $script .= "
  3145. *
  3146. * Since this table was configured to reload rows on insert, the object will
  3147. * be reloaded from the database if an INSERT operation is performed (unless
  3148. * the \$skipReload parameter is TRUE).";
  3149. }
  3150. $script .= "
  3151. *
  3152. * @param PropelPDO \$con";
  3153. if ($reloadOnUpdate || $reloadOnInsert) {
  3154. $script .= "
  3155. * @param boolean \$skipReload Whether to skip the reload for this object from database.";
  3156. }
  3157. $script .= "
  3158. * @return int The number of rows affected by this insert/update and any referring fk objects' save() operations.
  3159. * @throws PropelException
  3160. * @see doSave()
  3161. */";
  3162. }
  3163. /**
  3164. * Adds the function declaration for the save method
  3165. * @param string &$script The script will be modified in this method.
  3166. * @see addSave()
  3167. **/
  3168. protected function addSaveOpen(&$script) {
  3169. $table = $this->getTable();
  3170. $reloadOnUpdate = $table->isReloadOnUpdate();
  3171. $reloadOnInsert = $table->isReloadOnInsert();
  3172. $script .= "
  3173. public function save(PropelPDO \$con = null".($reloadOnUpdate || $reloadOnInsert ? ", \$skipReload = false" : "").")
  3174. {";
  3175. }
  3176. /**
  3177. * Adds the function body for the save method
  3178. * @param string &$script The script will be modified in this method.
  3179. * @see addSave()
  3180. **/
  3181. protected function addSaveBody(&$script) {
  3182. $table = $this->getTable();
  3183. $reloadOnUpdate = $table->isReloadOnUpdate();
  3184. $reloadOnInsert = $table->isReloadOnInsert();
  3185. $script .= "
  3186. if (\$this->isDeleted()) {
  3187. throw new PropelException(\"You cannot save an object that has been deleted.\");
  3188. }
  3189. if (\$con === null) {
  3190. \$con = Propel::getConnection(".$this->getPeerClassname()."::DATABASE_NAME, Propel::CONNECTION_WRITE);
  3191. }
  3192. \$con->beginTransaction();
  3193. \$isInsert = \$this->isNew();
  3194. try {";
  3195. if($this->getGeneratorConfig()->getBuildProperty('addHooks')) {
  3196. // save with runtime hools
  3197. $script .= "
  3198. \$ret = \$this->preSave(\$con);";
  3199. $this->applyBehaviorModifier('preSave', $script, " ");
  3200. $script .= "
  3201. if (\$isInsert) {
  3202. \$ret = \$ret && \$this->preInsert(\$con);";
  3203. $this->applyBehaviorModifier('preInsert', $script, " ");
  3204. $script .= "
  3205. } else {
  3206. \$ret = \$ret && \$this->preUpdate(\$con);";
  3207. $this->applyBehaviorModifier('preUpdate', $script, " ");
  3208. $script .= "
  3209. }
  3210. if (\$ret) {
  3211. \$affectedRows = \$this->doSave(\$con".($reloadOnUpdate || $reloadOnInsert ? ", \$skipReload" : "").");
  3212. if (\$isInsert) {
  3213. \$this->postInsert(\$con);";
  3214. $this->applyBehaviorModifier('postInsert', $script, " ");
  3215. $script .= "
  3216. } else {
  3217. \$this->postUpdate(\$con);";
  3218. $this->applyBehaviorModifier('postUpdate', $script, " ");
  3219. $script .= "
  3220. }
  3221. \$this->postSave(\$con);";
  3222. $this->applyBehaviorModifier('postSave', $script, " ");
  3223. $script .= "
  3224. ".$this->getPeerClassname()."::addInstanceToPool(\$this);
  3225. } else {
  3226. \$affectedRows = 0;
  3227. }
  3228. \$con->commit();
  3229. return \$affectedRows;";
  3230. } else {
  3231. // save without runtime hooks
  3232. $this->applyBehaviorModifier('preSave', $script, " ");
  3233. if ($this->hasBehaviorModifier('preUpdate'))
  3234. {
  3235. $script .= "
  3236. if(!\$isInsert) {";
  3237. $this->applyBehaviorModifier('preUpdate', $script, " ");
  3238. $script .= "
  3239. }";
  3240. }
  3241. if ($this->hasBehaviorModifier('preInsert'))
  3242. {
  3243. $script .= "
  3244. if(\$isInsert) {";
  3245. $this->applyBehaviorModifier('preInsert', $script, " ");
  3246. $script .= "
  3247. }";
  3248. }
  3249. $script .= "
  3250. \$affectedRows = \$this->doSave(\$con".($reloadOnUpdate || $reloadOnInsert ? ", \$skipReload" : "").");";
  3251. $this->applyBehaviorModifier('postSave', $script, " ");
  3252. if ($this->hasBehaviorModifier('postUpdate'))
  3253. {
  3254. $script .= "
  3255. if(!\$isInsert) {";
  3256. $this->applyBehaviorModifier('postUpdate', $script, " ");
  3257. $script .= "
  3258. }";
  3259. }
  3260. if ($this->hasBehaviorModifier('postInsert'))
  3261. {
  3262. $script .= "
  3263. if(\$isInsert) {";
  3264. $this->applyBehaviorModifier('postInsert', $script, " ");
  3265. $script .= "
  3266. }";
  3267. }
  3268. $script .= "
  3269. \$con->commit();
  3270. ".$this->getPeerClassname()."::addInstanceToPool(\$this);
  3271. return \$affectedRows;";
  3272. }
  3273. $script .= "
  3274. } catch (PropelException \$e) {
  3275. \$con->rollBack();
  3276. throw \$e;
  3277. }";
  3278. }
  3279. /**
  3280. * Adds the function close for the save method
  3281. * @param string &$script The script will be modified in this method.
  3282. * @see addSave()
  3283. **/
  3284. protected function addSaveClose(&$script) {
  3285. $script .= "
  3286. }
  3287. ";
  3288. }
  3289. /**
  3290. * Adds the $alreadyInValidation attribute, which prevents attempting to re-validate the same object.
  3291. * @param string &$script The script will be modified in this method.
  3292. */
  3293. protected function addAlreadyInValidationAttribute(&$script)
  3294. {
  3295. $script .= "
  3296. /**
  3297. * Flag to prevent endless validation loop, if this object is referenced
  3298. * by another object which falls in this transaction.
  3299. * @var boolean
  3300. */
  3301. protected \$alreadyInValidation = false;
  3302. ";
  3303. }
  3304. /**
  3305. * Adds the validate() method.
  3306. * @param string &$script The script will be modified in this method.
  3307. */
  3308. protected function addValidate(&$script)
  3309. {
  3310. $script .= "
  3311. /**
  3312. * Validates the objects modified field values and all objects related to this table.
  3313. *
  3314. * If \$columns is either a column name or an array of column names
  3315. * only those columns are validated.
  3316. *
  3317. * @param mixed \$columns Column name or an array of column names.
  3318. * @return boolean Whether all columns pass validation.
  3319. * @see doValidate()
  3320. * @see getValidationFailures()
  3321. */
  3322. public function validate(\$columns = null)
  3323. {
  3324. \$res = \$this->doValidate(\$columns);
  3325. if (\$res === true) {
  3326. \$this->validationFailures = array();
  3327. return true;
  3328. } else {
  3329. \$this->validationFailures = \$res;
  3330. return false;
  3331. }
  3332. }
  3333. ";
  3334. } // addValidate()
  3335. /**
  3336. * Adds the workhourse doValidate() method.
  3337. * @param string &$script The script will be modified in this method.
  3338. */
  3339. protected function addDoValidate(&$script)
  3340. {
  3341. $table = $this->getTable();
  3342. $script .= "
  3343. /**
  3344. * This function performs the validation work for complex object models.
  3345. *
  3346. * In addition to checking the current object, all related objects will
  3347. * also be validated. If all pass then <code>true</code> is returned; otherwise
  3348. * an aggreagated array of ValidationFailed objects will be returned.
  3349. *
  3350. * @param array \$columns Array of column names to validate.
  3351. * @return mixed <code>true</code> if all validations pass; array of <code>ValidationFailed</code> objets otherwise.
  3352. */
  3353. protected function doValidate(\$columns = null)
  3354. {
  3355. if (!\$this->alreadyInValidation) {
  3356. \$this->alreadyInValidation = true;
  3357. \$retval = null;
  3358. \$failureMap = array();
  3359. ";
  3360. if (count($table->getForeignKeys()) != 0) {
  3361. $script .= "
  3362. // We call the validate method on the following object(s) if they
  3363. // were passed to this object by their coresponding set
  3364. // method. This object relates to these object(s) by a
  3365. // foreign key reference.
  3366. ";
  3367. foreach ($table->getForeignKeys() as $fk) {
  3368. $aVarName = $this->getFKVarName($fk);
  3369. $script .= "
  3370. if (\$this->".$aVarName." !== null) {
  3371. if (!\$this->".$aVarName."->validate(\$columns)) {
  3372. \$failureMap = array_merge(\$failureMap, \$this->".$aVarName."->getValidationFailures());
  3373. }
  3374. }
  3375. ";
  3376. } /* for () */
  3377. } /* if count(fkeys) */
  3378. $script .= "
  3379. if ((\$retval = ".$this->getPeerClassname()."::doValidate(\$this, \$columns)) !== true) {
  3380. \$failureMap = array_merge(\$failureMap, \$retval);
  3381. }
  3382. ";
  3383. foreach ($table->getReferrers() as $refFK) {
  3384. if ($refFK->isLocalPrimaryKey()) {
  3385. $varName = $this->getPKRefFKVarName($refFK);
  3386. $script .= "
  3387. if (\$this->$varName !== null) {
  3388. if (!\$this->".$varName."->validate(\$columns)) {
  3389. \$failureMap = array_merge(\$failureMap, \$this->".$varName."->getValidationFailures());
  3390. }
  3391. }
  3392. ";
  3393. } else {
  3394. $collName = $this->getRefFKCollVarName($refFK);
  3395. $script .= "
  3396. if (\$this->$collName !== null) {
  3397. foreach (\$this->$collName as \$referrerFK) {
  3398. if (!\$referrerFK->validate(\$columns)) {
  3399. \$failureMap = array_merge(\$failureMap, \$referrerFK->getValidationFailures());
  3400. }
  3401. }
  3402. }
  3403. ";
  3404. }
  3405. } /* foreach getReferrers() */
  3406. $script .= "
  3407. \$this->alreadyInValidation = false;
  3408. }
  3409. return (!empty(\$failureMap) ? \$failureMap : true);
  3410. }
  3411. ";
  3412. } // addDoValidate()
  3413. /**
  3414. * Adds the ensureConsistency() method to ensure that internal state is correct.
  3415. * @param string &$script The script will be modified in this method.
  3416. */
  3417. protected function addEnsureConsistency(&$script)
  3418. {
  3419. $table = $this->getTable();
  3420. $script .= "
  3421. /**
  3422. * Checks and repairs the internal consistency of the object.
  3423. *
  3424. * This method is executed after an already-instantiated object is re-hydrated
  3425. * from the database. It exists to check any foreign keys to make sure that
  3426. * the objects related to the current object are correct based on foreign key.
  3427. *
  3428. * You can override this method in the stub class, but you should always invoke
  3429. * the base method from the overridden method (i.e. parent::ensureConsistency()),
  3430. * in case your model changes.
  3431. *
  3432. * @throws PropelException
  3433. */
  3434. public function ensureConsistency()
  3435. {
  3436. ";
  3437. foreach ($table->getColumns() as $col) {
  3438. $clo=strtolower($col->getName());
  3439. if ($col->isForeignKey()) {
  3440. foreach ($col->getForeignKeys() as $fk) {
  3441. $tblFK = $table->getDatabase()->getTable($fk->getForeignTableName());
  3442. $colFK = $tblFK->getColumn($fk->getMappedForeignColumn($col->getName()));
  3443. $varName = $this->getFKVarName($fk);
  3444. $script .= "
  3445. if (\$this->".$varName." !== null && \$this->$clo !== \$this->".$varName."->get".$colFK->getPhpName()."()) {
  3446. \$this->$varName = null;
  3447. }";
  3448. } // foraech
  3449. } /* if col is foreign key */
  3450. } // foreach
  3451. $script .= "
  3452. } // ensureConsistency
  3453. ";
  3454. } // addCheckRelConsistency
  3455. /**
  3456. * Adds the copy() method, which (in complex OM) includes the $deepCopy param for making copies of related objects.
  3457. * @param string &$script The script will be modified in this method.
  3458. */
  3459. protected function addCopy(&$script)
  3460. {
  3461. $this->addCopyInto($script);
  3462. $table = $this->getTable();
  3463. $script .= "
  3464. /**
  3465. * Makes a copy of this object that will be inserted as a new row in table when saved.
  3466. * It creates a new object filling in the simple attributes, but skipping any primary
  3467. * keys that are defined for the table.
  3468. *
  3469. * If desired, this method can also make copies of all associated (fkey referrers)
  3470. * objects.
  3471. *
  3472. * @param boolean \$deepCopy Whether to also copy all rows that refer (by fkey) to the current row.
  3473. * @return ".$this->getObjectClassname()." Clone of current object.
  3474. * @throws PropelException
  3475. */
  3476. public function copy(\$deepCopy = false)
  3477. {
  3478. // we use get_class(), because this might be a subclass
  3479. \$clazz = get_class(\$this);
  3480. " . $this->buildObjectInstanceCreationCode('$copyObj', '$clazz') . "
  3481. \$this->copyInto(\$copyObj, \$deepCopy);
  3482. return \$copyObj;
  3483. }
  3484. ";
  3485. } // addCopy()
  3486. /**
  3487. * Adds the copyInto() method, which takes an object and sets contents to match current object.
  3488. * In complex OM this method includes the $deepCopy param for making copies of related objects.
  3489. * @param string &$script The script will be modified in this method.
  3490. */
  3491. protected function addCopyInto(&$script)
  3492. {
  3493. $table = $this->getTable();
  3494. $script .= "
  3495. /**
  3496. * Sets contents of passed object to values from current object.
  3497. *
  3498. * If desired, this method can also make copies of all associated (fkey referrers)
  3499. * objects.
  3500. *
  3501. * @param object \$copyObj An object of ".$this->getObjectClassname()." (or compatible) type.
  3502. * @param boolean \$deepCopy Whether to also copy all rows that refer (by fkey) to the current row.
  3503. * @throws PropelException
  3504. */
  3505. public function copyInto(\$copyObj, \$deepCopy = false)
  3506. {
  3507. ";
  3508. $autoIncCols = array();
  3509. foreach ($table->getColumns() as $col) {
  3510. /* @var $col Column */
  3511. if ($col->isAutoIncrement()) {
  3512. $autoIncCols[] = $col;
  3513. }
  3514. }
  3515. foreach ($table->getColumns() as $col) {
  3516. if (!in_array($col, $autoIncCols, true)) {
  3517. $script .= "
  3518. \$copyObj->set".$col->getPhpName()."(\$this->".strtolower($col->getName()).");
  3519. ";
  3520. }
  3521. } // foreach
  3522. // Avoid useless code by checking to see if there are any referrers
  3523. // to this table:
  3524. if (count($table->getReferrers()) > 0) {
  3525. $script .= "
  3526. if (\$deepCopy) {
  3527. // important: temporarily setNew(false) because this affects the behavior of
  3528. // the getter/setter methods for fkey referrer objects.
  3529. \$copyObj->setNew(false);
  3530. ";
  3531. foreach ($table->getReferrers() as $fk) {
  3532. //HL: commenting out self-referrential check below
  3533. // it seems to work as expected and is probably desireable to have those referrers from same table deep-copied.
  3534. //if ( $fk->getTable()->getName() != $table->getName() ) {
  3535. if ($fk->isLocalPrimaryKey()) {
  3536. $afx = $this->getRefFKPhpNameAffix($fk, $plural = false);
  3537. $script .= "
  3538. \$relObj = \$this->get$afx();
  3539. if (\$relObj) {
  3540. \$copyObj->set$afx(\$relObj->copy(\$deepCopy));
  3541. }
  3542. ";
  3543. } else {
  3544. $script .= "
  3545. foreach (\$this->get".$this->getRefFKPhpNameAffix($fk, true)."() as \$relObj) {
  3546. if (\$relObj !== \$this) { // ensure that we don't try to copy a reference to ourselves
  3547. \$copyObj->add".$this->getRefFKPhpNameAffix($fk)."(\$relObj->copy(\$deepCopy));
  3548. }
  3549. }
  3550. ";
  3551. }
  3552. // HL: commenting out close of self-referential check
  3553. // } /* if tblFK != table */
  3554. } /* foreach */
  3555. $script .= "
  3556. } // if (\$deepCopy)
  3557. ";
  3558. } /* if (count referrers > 0 ) */
  3559. $script .= "
  3560. \$copyObj->setNew(true);
  3561. ";
  3562. // Note: we're no longer resetting non-autoincrement primary keys to default values
  3563. // due to: http://propel.phpdb.org/trac/ticket/618
  3564. foreach ($autoIncCols as $col) {
  3565. $coldefval = $col->getPhpDefaultValue();
  3566. $coldefval = var_export($coldefval, true);
  3567. $script .= "
  3568. \$copyObj->set".$col->getPhpName() ."($coldefval); // this is a auto-increment column, so set to default value
  3569. ";
  3570. } // foreach
  3571. $script .= "
  3572. }
  3573. ";
  3574. } // addCopyInto()
  3575. /**
  3576. * Adds clearAllReferencers() method which resets all the collections of referencing
  3577. * fk objects.
  3578. * @param string &$script The script will be modified in this method.
  3579. */
  3580. protected function addClearAllReferences(&$script)
  3581. {
  3582. $table = $this->getTable();
  3583. $script .= "
  3584. /**
  3585. * Resets all collections of referencing foreign keys.
  3586. *
  3587. * This method is a user-space workaround for PHP's inability to garbage collect objects
  3588. * with circular references. This is currently necessary when using Propel in certain
  3589. * daemon or large-volumne/high-memory operations.
  3590. *
  3591. * @param boolean \$deep Whether to also clear the references on all associated objects.
  3592. */
  3593. public function clearAllReferences(\$deep = false)
  3594. {
  3595. if (\$deep) {";
  3596. $vars = array();
  3597. foreach ($this->getTable()->getReferrers() as $refFK) {
  3598. if ($refFK->isLocalPrimaryKey()) {
  3599. $varName = $this->getPKRefFKVarName($refFK);
  3600. $vars[] = $varName;
  3601. $script .= "
  3602. if (\$this->$varName) {
  3603. \$this->{$varName}->clearAllReferences(\$deep);
  3604. }";
  3605. } else {
  3606. $varName = $this->getRefFKCollVarName($refFK);
  3607. $vars[] = $varName;
  3608. $script .= "
  3609. if (\$this->$varName) {
  3610. foreach ((array) \$this->$varName as \$o) {
  3611. \$o->clearAllReferences(\$deep);
  3612. }
  3613. }";
  3614. }
  3615. }
  3616. $script .= "
  3617. } // if (\$deep)
  3618. ";
  3619. foreach ($vars as $varName) {
  3620. $script .= "
  3621. \$this->$varName = null;";
  3622. }
  3623. foreach ($table->getForeignKeys() as $fk) {
  3624. $className = $this->getForeignTable($fk)->getPhpName();
  3625. $varName = $this->getFKVarName($fk);
  3626. $script .= "
  3627. \$this->$varName = null;";
  3628. }
  3629. $script .= "
  3630. }
  3631. ";
  3632. }
  3633. /**
  3634. * Adds a magic __toString() method if a string column was defined as primary string
  3635. * @param string &$script The script will be modified in this method.
  3636. */
  3637. protected function addPrimaryString(&$script)
  3638. {
  3639. foreach ($this->getTable()->getColumns() as $column) {
  3640. if ($column->isPrimaryString()) {
  3641. $script .= "
  3642. /**
  3643. * Return the string representation of this object
  3644. *
  3645. * @return string The value of the '{$column->getName()}' column
  3646. */
  3647. public function __toString()
  3648. {
  3649. return (string) \$this->get{$column->getPhpName()}();
  3650. }
  3651. ";
  3652. break;
  3653. }
  3654. }
  3655. }
  3656. } // PHP5ObjectBuilder