PageRenderTime 90ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://github.com/mattleff/propel
PHP | 4724 lines | 2723 code | 448 blank | 1553 comment | 375 complexity | dc03adce0da45a20827f99540c211125 MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of the Propel package.
  4. * For the full copyright and license information, please view the LICENSE
  5. * file that was distributed with this source code.
  6. *
  7. * @license MIT License
  8. */
  9. require_once dirname(__FILE__) . '/ObjectBuilder.php';
  10. /**
  11. * Generates a PHP5 base Object class for user object model (OM).
  12. *
  13. * This class produces the base object class (e.g. BaseMyTable) which contains all
  14. * the custom-built accessor and setter methods.
  15. *
  16. * @author Hans Lellelid <hans@xmpl.org>
  17. * @package propel.generator.builder.om
  18. */
  19. class PHP5ObjectBuilder extends ObjectBuilder
  20. {
  21. /**
  22. * Gets the package for the [base] object classes.
  23. * @return string
  24. */
  25. public function getPackage()
  26. {
  27. return parent::getPackage() . ".om";
  28. }
  29. public function getNamespace()
  30. {
  31. if ($namespace = parent::getNamespace()) {
  32. if ($this->getGeneratorConfig() && $omns = $this->getGeneratorConfig()->getBuildProperty('namespaceOm')) {
  33. return $namespace . '\\' . $omns;
  34. } else {
  35. return $namespace;
  36. }
  37. }
  38. }
  39. /**
  40. * Returns the name of the current class being built.
  41. * @return string
  42. */
  43. public function getUnprefixedClassname()
  44. {
  45. return $this->getBuildProperty('basePrefix') . $this->getStubObjectBuilder()->getUnprefixedClassname();
  46. }
  47. /**
  48. * Validates the current table to make sure that it won't
  49. * result in generated code that will not parse.
  50. *
  51. * This method may emit warnings for code which may cause problems
  52. * and will throw exceptions for errors that will definitely cause
  53. * problems.
  54. */
  55. protected function validateModel()
  56. {
  57. parent::validateModel();
  58. $table = $this->getTable();
  59. // Check to see whether any generated foreign key names
  60. // will conflict with column names.
  61. $colPhpNames = array();
  62. $fkPhpNames = array();
  63. foreach ($table->getColumns() as $col) {
  64. $colPhpNames[] = $col->getPhpName();
  65. }
  66. foreach ($table->getForeignKeys() as $fk) {
  67. $fkPhpNames[] = $this->getFKPhpNameAffix($fk, $plural = false);
  68. }
  69. $intersect = array_intersect($colPhpNames, $fkPhpNames);
  70. if (!empty($intersect)) {
  71. throw new EngineException("One or more of your column names for [" . $table->getName() . "] table conflict with foreign key names (" . implode(", ", $intersect) . ")");
  72. }
  73. // Check foreign keys to see if there are any foreign keys that
  74. // are also matched with an inversed referencing foreign key
  75. // (this is currently unsupported behavior)
  76. // see: http://propel.phpdb.org/trac/ticket/549
  77. foreach ($table->getForeignKeys() as $fk) {
  78. if ($fk->isMatchedByInverseFK()) {
  79. throw new EngineException("The 1:1 relationship expressed by foreign key " . $fk->getName() . " is defined in both directions; Propel does not currently support this (if you must have both foreign key constraints, consider adding this constraint with a custom SQL file.)" );
  80. }
  81. }
  82. }
  83. /**
  84. * Returns the appropriate formatter (from platform) for a date/time column.
  85. * @param Column $col
  86. * @return string
  87. */
  88. protected function getTemporalFormatter(Column $col)
  89. {
  90. $fmt = null;
  91. if ($col->getType() === PropelTypes::DATE) {
  92. $fmt = $this->getPlatform()->getDateFormatter();
  93. } elseif ($col->getType() === PropelTypes::TIME) {
  94. $fmt = $this->getPlatform()->getTimeFormatter();
  95. } elseif ($col->getType() === PropelTypes::TIMESTAMP) {
  96. $fmt = $this->getPlatform()->getTimestampFormatter();
  97. }
  98. return $fmt;
  99. }
  100. /**
  101. * Returns the type-casted and stringified default value for the specified Column.
  102. * This only works for scalar default values currently.
  103. * @return string The default value or 'NULL' if there is none.
  104. */
  105. protected function getDefaultValueString(Column $col)
  106. {
  107. $val = $col->getPhpDefaultValue();
  108. if ($val === null) {
  109. return var_export(null, true);
  110. }
  111. if ($col->isTemporalType()) {
  112. $fmt = $this->getTemporalFormatter($col);
  113. try {
  114. if (!($this->getPlatform() instanceof MysqlPlatform &&
  115. ($val === '0000-00-00 00:00:00' || $val === '0000-00-00'))) {
  116. // while technically this is not a default value of NULL,
  117. // this seems to be closest in meaning.
  118. $defDt = new DateTime($val);
  119. $defaultValue = var_export($defDt->format($fmt), true);
  120. }
  121. } catch (Exception $x) {
  122. // prevent endless loop when timezone is undefined
  123. date_default_timezone_set('America/Los_Angeles');
  124. throw new EngineException(sprintf('Unable to parse default temporal value "%s" for column "%s"', $col->getDefaultValueString(), $col->getFullyQualifiedName()), $x);
  125. }
  126. } elseif ($col->isEnumType()) {
  127. $valueSet = $col->getValueSet();
  128. if (!in_array($val, $valueSet)) {
  129. throw new EngineException(sprintf('Default Value "%s" is not among the enumerated values', $val));
  130. }
  131. $defaultValue = array_search($val, $valueSet);
  132. } else if ($col->isPhpPrimitiveType()) {
  133. settype($val, $col->getPhpType());
  134. $defaultValue = var_export($val, true);
  135. } elseif ($col->isPhpObjectType()) {
  136. $defaultValue = 'new '.$col->getPhpType().'(' . var_export($val, true) . ')';
  137. } else {
  138. throw new EngineException("Cannot get default value string for " . $col->getFullyQualifiedName());
  139. }
  140. return $defaultValue;
  141. }
  142. /**
  143. * Adds the include() statements for files that this class depends on or utilizes.
  144. * @param string &$script The script will be modified in this method.
  145. */
  146. protected function addIncludes(&$script)
  147. {
  148. } // addIncludes()
  149. /**
  150. * Adds class phpdoc comment and openning of class.
  151. * @param string &$script The script will be modified in this method.
  152. */
  153. protected function addClassOpen(&$script)
  154. {
  155. $table = $this->getTable();
  156. $tableName = $table->getName();
  157. $tableDesc = $table->getDescription();
  158. $interface = $this->getInterface();
  159. $parentClass = $this->getBehaviorContent('parentClass');
  160. $parentClass = (null !== $parentClass) ? $parentClass : ClassTools::classname($this->getBaseClass());
  161. $script .= "
  162. /**
  163. * Base class that represents a row from the '$tableName' table.
  164. *
  165. * $tableDesc
  166. *";
  167. if ($this->getBuildProperty('addTimeStamp')) {
  168. $now = strftime('%c');
  169. $script .= "
  170. * This class was autogenerated by Propel " . $this->getBuildProperty('version') . " on:
  171. *
  172. * $now
  173. *";
  174. }
  175. $script .= "
  176. * @package propel.generator.".$this->getPackage()."
  177. */
  178. abstract class ".$this->getClassname()." extends ".$parentClass." ";
  179. $interface = ClassTools::getInterface($table);
  180. if ($interface) {
  181. $script .= " implements " . ClassTools::classname($interface);
  182. }
  183. if ($this->getTable()->getInterface()) {
  184. $this->declareClassFromBuilder($this->getInterfaceBuilder());
  185. }
  186. $script .= "
  187. {
  188. ";
  189. }
  190. /**
  191. * Specifies the methods that are added as part of the basic OM class.
  192. * This can be overridden by subclasses that wish to add more methods.
  193. * @see ObjectBuilder::addClassBody()
  194. */
  195. protected function addClassBody(&$script)
  196. {
  197. $this->declareClassFromBuilder($this->getStubPeerBuilder());
  198. $this->declareClassFromBuilder($this->getStubQueryBuilder());
  199. $this->declareClasses('Propel', 'PropelException', 'PDO', 'PropelPDO', 'Criteria', 'BaseObject', 'Persistent', 'BasePeer', 'PropelObjectCollection');
  200. $table = $this->getTable();
  201. if (!$table->isAlias()) {
  202. $this->addConstants($script);
  203. $this->addAttributes($script);
  204. }
  205. if ($this->hasDefaultValues()) {
  206. $this->addApplyDefaultValues($script);
  207. $this->addConstructor($script);
  208. }
  209. $this->addColumnAccessorMethods($script);
  210. $this->addColumnMutatorMethods($script);
  211. $this->addHasOnlyDefaultValues($script);
  212. $this->addHydrate($script);
  213. $this->addEnsureConsistency($script);
  214. if (!$table->isReadOnly()) {
  215. $this->addManipulationMethods($script);
  216. }
  217. if ($this->isAddValidateMethod()) {
  218. $this->addValidationMethods($script);
  219. }
  220. if ($this->isAddGenericAccessors()) {
  221. $this->addGetByName($script);
  222. $this->addGetByPosition($script);
  223. $this->addToArray($script);
  224. }
  225. if ($this->isAddGenericMutators()) {
  226. $this->addSetByName($script);
  227. $this->addSetByPosition($script);
  228. $this->addFromArray($script);
  229. }
  230. $this->addBuildCriteria($script);
  231. $this->addBuildPkeyCriteria($script);
  232. $this->addGetPrimaryKey($script);
  233. $this->addSetPrimaryKey($script);
  234. $this->addIsPrimaryKeyNull($script);
  235. $this->addCopy($script);
  236. if (!$table->isAlias()) {
  237. $this->addGetPeer($script);
  238. }
  239. $this->addFKMethods($script);
  240. $this->addRefFKMethods($script);
  241. $this->addCrossFKMethods($script);
  242. $this->addClear($script);
  243. $this->addClearAllReferences($script);
  244. $this->addPrimaryString($script);
  245. // apply behaviors
  246. $this->applyBehaviorModifier('objectMethods', $script, " ");
  247. $this->addMagicCall($script);
  248. }
  249. /**
  250. * Closes class.
  251. * @param string &$script The script will be modified in this method.
  252. */
  253. protected function addClassClose(&$script)
  254. {
  255. $script .= "
  256. } // " . $this->getClassname() . "
  257. ";
  258. $this->applyBehaviorModifier('objectFilter', $script, "");
  259. }
  260. /**
  261. * Adds any constants to the class.
  262. * @param string &$script The script will be modified in this method.
  263. */
  264. protected function addConstants(&$script)
  265. {
  266. $script .= "
  267. /**
  268. * Peer class name
  269. */
  270. const PEER = '" . addslashes($this->getStubPeerBuilder()->getFullyQualifiedClassname()) . "';
  271. ";
  272. }
  273. /**
  274. * Adds class attributes.
  275. * @param string &$script The script will be modified in this method.
  276. */
  277. protected function addAttributes(&$script)
  278. {
  279. $table = $this->getTable();
  280. $script .= "
  281. /**
  282. * The Peer class.
  283. * Instance provides a convenient way of calling static methods on a class
  284. * that calling code may not be able to identify.
  285. * @var ".$this->getPeerClassname()."
  286. */
  287. protected static \$peer;
  288. ";
  289. if (!$table->isAlias()) {
  290. $this->addColumnAttributes($script);
  291. }
  292. foreach ($table->getForeignKeys() as $fk) {
  293. $this->addFKAttributes($script, $fk);
  294. }
  295. foreach ($table->getReferrers() as $refFK) {
  296. $this->addRefFKAttributes($script, $refFK);
  297. }
  298. // many-to-many relationships
  299. foreach ($table->getCrossFks() as $fkList) {
  300. $crossFK = $fkList[1];
  301. $this->addCrossFKAttributes($script, $crossFK);
  302. }
  303. $this->addAlreadyInSaveAttribute($script);
  304. $this->addAlreadyInValidationAttribute($script);
  305. // apply behaviors
  306. $this->applyBehaviorModifier('objectAttributes', $script, " ");
  307. }
  308. /**
  309. * Adds variables that store column values.
  310. * @param string &$script The script will be modified in this method.
  311. * @see addColumnNameConstants()
  312. */
  313. protected function addColumnAttributes(&$script)
  314. {
  315. $table = $this->getTable();
  316. foreach ($table->getColumns() as $col) {
  317. $this->addColumnAttributeComment($script, $col);
  318. $this->addColumnAttributeDeclaration($script, $col);
  319. if ($col->isLazyLoad() ) {
  320. $this->addColumnAttributeLoaderComment($script, $col);
  321. $this->addColumnAttributeLoaderDeclaration($script, $col);
  322. }
  323. if ($col->getType() == PropelTypes::OBJECT || $col->getType() == PropelTypes::PHP_ARRAY) {
  324. $this->addColumnAttributeUnserializedComment($script, $col);
  325. $this->addColumnAttributeUnserializedDeclaration($script, $col);
  326. }
  327. }
  328. }
  329. /**
  330. * Add comment about the attribute (variable) that stores column values
  331. * @param string &$script The script will be modified in this method.
  332. * @param Column $col
  333. **/
  334. protected function addColumnAttributeComment(&$script, Column $col)
  335. {
  336. $cptype = $col->getPhpType();
  337. $clo = strtolower($col->getName());
  338. $script .= "
  339. /**
  340. * The value for the $clo field.";
  341. if ($col->getDefaultValue()) {
  342. if ($col->getDefaultValue()->isExpression()) {
  343. $script .= "
  344. * Note: this column has a database default value of: (expression) ".$col->getDefaultValue()->getValue();
  345. } else {
  346. $script .= "
  347. * Note: this column has a database default value of: ". $this->getDefaultValueString($col);
  348. }
  349. }
  350. $script .= "
  351. * @var $cptype
  352. */";
  353. }
  354. /**
  355. * Adds the declaration of a column value storage attribute
  356. * @param string &$script The script will be modified in this method.
  357. * @param Column $col
  358. **/
  359. protected function addColumnAttributeDeclaration(&$script, Column $col)
  360. {
  361. $clo = strtolower($col->getName());
  362. $script .= "
  363. protected \$" . $clo . ";
  364. ";
  365. }
  366. /**
  367. * Adds the comment about the attribute keeping track if an attribute value has been loaded
  368. * @param string &$script The script will be modified in this method.
  369. * @param Column $col
  370. **/
  371. protected function addColumnAttributeLoaderComment(&$script, Column $col)
  372. {
  373. $clo = strtolower($col->getName());
  374. $script .= "
  375. /**
  376. * Whether the lazy-loaded \$$clo value has been loaded from database.
  377. * This is necessary to avoid repeated lookups if \$$clo column is NULL in the db.
  378. * @var boolean
  379. */";
  380. }
  381. /**
  382. * Adds the declaration of the attribute keeping track of an attribute's loaded state
  383. * @param string &$script The script will be modified in this method.
  384. * @param Column $col
  385. **/
  386. protected function addColumnAttributeLoaderDeclaration(&$script, Column $col)
  387. {
  388. $clo = strtolower($col->getName());
  389. $script .= "
  390. protected \$".$clo."_isLoaded = false;
  391. ";
  392. }
  393. /**
  394. * Adds the comment about the serialized attribute
  395. * @param string &$script The script will be modified in this method.
  396. * @param Column $col
  397. **/
  398. protected function addColumnAttributeUnserializedComment(&$script, Column $col)
  399. {
  400. $clo = strtolower($col->getName());
  401. $script .= "
  402. /**
  403. * The unserialized \$$clo value - i.e. the persisted object.
  404. * This is necessary to avoid repeated calls to unserialize() at runtime.
  405. * @var object
  406. */";
  407. }
  408. /**
  409. * Adds the declaration of the serialized attribute
  410. * @param string &$script The script will be modified in this method.
  411. * @param Column $col
  412. **/
  413. protected function addColumnAttributeUnserializedDeclaration(&$script, Column $col)
  414. {
  415. $clo = strtolower($col->getName()) . "_unserialized";
  416. $script .= "
  417. protected \$" . $clo . ";
  418. ";
  419. }
  420. /**
  421. * Adds the getPeer() method.
  422. * This is a convenient, non introspective way of getting the Peer class for a particular object.
  423. * @param string &$script The script will be modified in this method.
  424. */
  425. protected function addGetPeer(&$script)
  426. {
  427. $this->addGetPeerComment($script);
  428. $this->addGetPeerFunctionOpen($script);
  429. $this->addGetPeerFunctionBody($script);
  430. $this->addGetPeerFunctionClose($script);
  431. }
  432. /**
  433. * Add the comment for the getPeer method
  434. * @param string &$script The script will be modified in this method.
  435. **/
  436. protected function addGetPeerComment(&$script) {
  437. $script .= "
  438. /**
  439. * Returns a peer instance associated with this om.
  440. *
  441. * Since Peer classes are not to have any instance attributes, this method returns the
  442. * same instance for all member of this class. The method could therefore
  443. * be static, but this would prevent one from overriding the behavior.
  444. *
  445. * @return ".$this->getPeerClassname()."
  446. */";
  447. }
  448. /**
  449. * Adds the function declaration (function opening) for the getPeer method
  450. * @param string &$script The script will be modified in this method.
  451. **/
  452. protected function addGetPeerFunctionOpen(&$script) {
  453. $script .= "
  454. public function getPeer()
  455. {";
  456. }
  457. /**
  458. * Adds the body of the getPeer method
  459. * @param string &$script The script will be modified in this method.
  460. **/
  461. protected function addGetPeerFunctionBody(&$script) {
  462. $script .= "
  463. if (self::\$peer === null) {
  464. " . $this->buildObjectInstanceCreationCode('self::$peer', $this->getPeerClassname()) . "
  465. }
  466. return self::\$peer;";
  467. }
  468. /**
  469. * Add the function close for the getPeer method
  470. * 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
  471. * @param string &$script The script will be modified in this method.
  472. **/
  473. protected function addGetPeerFunctionClose(&$script) {
  474. $script .= "
  475. }
  476. ";
  477. }
  478. /**
  479. * Adds the constructor for this object.
  480. * @param string &$script The script will be modified in this method.
  481. * @see addConstructor()
  482. */
  483. protected function addConstructor(&$script)
  484. {
  485. $this->addConstructorComment($script);
  486. $this->addConstructorOpen($script);
  487. $this->addConstructorBody($script);
  488. $this->addConstructorClose($script);
  489. }
  490. /**
  491. * Adds the comment for the constructor
  492. * @param string &$script The script will be modified in this method.
  493. **/
  494. protected function addConstructorComment(&$script) {
  495. $script .= "
  496. /**
  497. * Initializes internal state of ".$this->getClassname()." object.
  498. * @see applyDefaults()
  499. */";
  500. }
  501. /**
  502. * Adds the function declaration for the constructor
  503. * @param string &$script The script will be modified in this method.
  504. **/
  505. protected function addConstructorOpen(&$script) {
  506. $script .= "
  507. public function __construct()
  508. {";
  509. }
  510. /**
  511. * Adds the function body for the constructor
  512. * @param string &$script The script will be modified in this method.
  513. **/
  514. protected function addConstructorBody(&$script) {
  515. $script .= "
  516. parent::__construct();
  517. \$this->applyDefaultValues();";
  518. }
  519. /**
  520. * Adds the function close for the constructor
  521. * @param string &$script The script will be modified in this method.
  522. **/
  523. protected function addConstructorClose(&$script) {
  524. $script .= "
  525. }
  526. ";
  527. }
  528. /**
  529. * Adds the applyDefaults() method, which is called from the constructor.
  530. * @param string &$script The script will be modified in this method.
  531. * @see addConstructor()
  532. */
  533. protected function addApplyDefaultValues(&$script)
  534. {
  535. $this->addApplyDefaultValuesComment($script);
  536. $this->addApplyDefaultValuesOpen($script);
  537. $this->addApplyDefaultValuesBody($script);
  538. $this->addApplyDefaultValuesClose($script);
  539. }
  540. /**
  541. * Adds the comment for the applyDefaults method
  542. * @param string &$script The script will be modified in this method.
  543. * @see addApplyDefaultValues()
  544. **/
  545. protected function addApplyDefaultValuesComment(&$script) {
  546. $script .= "
  547. /**
  548. * Applies default values to this object.
  549. * This method should be called from the object's constructor (or
  550. * equivalent initialization method).
  551. * @see __construct()
  552. */";
  553. }
  554. /**
  555. * Adds the function declaration for the applyDefaults method
  556. * @param string &$script The script will be modified in this method.
  557. * @see addApplyDefaultValues()
  558. **/
  559. protected function addApplyDefaultValuesOpen(&$script) {
  560. $script .= "
  561. public function applyDefaultValues()
  562. {";
  563. }
  564. /**
  565. * Adds the function body of the applyDefault method
  566. * @param string &$script The script will be modified in this method.
  567. * @see addApplyDefaultValues()
  568. **/
  569. protected function addApplyDefaultValuesBody(&$script) {
  570. $table = $this->getTable();
  571. // FIXME - Apply support for PHP default expressions here
  572. // see: http://propel.phpdb.org/trac/ticket/378
  573. $colsWithDefaults = array();
  574. foreach ($table->getColumns() as $col) {
  575. $def = $col->getDefaultValue();
  576. if ($def !== null && !$def->isExpression()) {
  577. $colsWithDefaults[] = $col;
  578. }
  579. }
  580. $colconsts = array();
  581. foreach ($colsWithDefaults as $col) {
  582. $clo = strtolower($col->getName());
  583. $defaultValue = $this->getDefaultValueString($col);
  584. $script .= "
  585. \$this->".$clo." = $defaultValue;";
  586. }
  587. }
  588. /**
  589. * Adds the function close for the applyDefaults method
  590. * @param string &$script The script will be modified in this method.
  591. * @see addApplyDefaultValues()
  592. **/
  593. protected function addApplyDefaultValuesClose(&$script) {
  594. $script .= "
  595. }
  596. ";
  597. }
  598. // --------------------------------------------------------------
  599. //
  600. // A C C E S S O R M E T H O D S
  601. //
  602. // --------------------------------------------------------------
  603. /**
  604. * Adds a date/time/timestamp getter method.
  605. * @param string &$script The script will be modified in this method.
  606. * @param Column $col The current column.
  607. * @see parent::addColumnAccessors()
  608. */
  609. protected function addTemporalAccessor(&$script, Column $col)
  610. {
  611. $this->addTemporalAccessorComment($script, $col);
  612. $this->addTemporalAccessorOpen($script, $col);
  613. $this->addTemporalAccessorBody($script, $col);
  614. $this->addTemporalAccessorClose($script, $col);
  615. } // addTemporalAccessor
  616. /**
  617. * Adds the comment for a temporal accessor
  618. * @param string &$script The script will be modified in this method.
  619. * @param Column $col The current column.
  620. * @see addTemporalAccessor
  621. **/
  622. public function addTemporalAccessorComment(&$script, Column $col) {
  623. $clo = strtolower($col->getName());
  624. $useDateTime = $this->getBuildProperty('useDateTimeClass');
  625. $dateTimeClass = $this->getBuildProperty('dateTimeClass');
  626. if (!$dateTimeClass) {
  627. $dateTimeClass = 'DateTime';
  628. }
  629. $handleMysqlDate = false;
  630. if ($this->getPlatform() instanceof MysqlPlatform) {
  631. if ($col->getType() === PropelTypes::TIMESTAMP) {
  632. $handleMysqlDate = true;
  633. $mysqlInvalidDateString = '0000-00-00 00:00:00';
  634. } elseif ($col->getType() === PropelTypes::DATE) {
  635. $handleMysqlDate = true;
  636. $mysqlInvalidDateString = '0000-00-00';
  637. }
  638. // 00:00:00 is a valid time, so no need to check for that.
  639. }
  640. $script .= "
  641. /**
  642. * Get the [optionally formatted] temporal [$clo] column value.
  643. * ".$col->getDescription();
  644. if (!$useDateTime) {
  645. $script .= "
  646. * This accessor only only work with unix epoch dates. Consider enabling the propel.useDateTimeClass
  647. * option in order to avoid converstions to integers (which are limited in the dates they can express).";
  648. }
  649. $script .= "
  650. *
  651. * @param string \$format The date/time format string (either date()-style or strftime()-style).
  652. * If format is NULL, then the raw ".($useDateTime ? 'DateTime object' : 'unix timestamp integer')." will be returned.";
  653. if ($useDateTime) {
  654. $script .= "
  655. * @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 : '');
  656. } else {
  657. $script .= "
  658. * @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 : '');
  659. }
  660. $script .= "
  661. * @throws PropelException - if unable to parse/validate the date/time value.
  662. */";
  663. }
  664. /**
  665. * Adds the function declaration for a temporal accessor
  666. * @param string &$script The script will be modified in this method.
  667. * @param Column $col The current column.
  668. * @see addTemporalAccessor
  669. **/
  670. public function addTemporalAccessorOpen(&$script, Column $col) {
  671. $cfc = $col->getPhpName();
  672. $defaultfmt = null;
  673. $visibility = $col->getAccessorVisibility();
  674. // Default date/time formatter strings are specified in build.properties
  675. if ($col->getType() === PropelTypes::DATE) {
  676. $defaultfmt = $this->getBuildProperty('defaultDateFormat');
  677. } elseif ($col->getType() === PropelTypes::TIME) {
  678. $defaultfmt = $this->getBuildProperty('defaultTimeFormat');
  679. } elseif ($col->getType() === PropelTypes::TIMESTAMP) {
  680. $defaultfmt = $this->getBuildProperty('defaultTimeStampFormat');
  681. }
  682. if (empty($defaultfmt)) { $defaultfmt = null; }
  683. $script .= "
  684. ".$visibility." function get$cfc(\$format = ".var_export($defaultfmt, true)."";
  685. if ($col->isLazyLoad()) $script .= ", \$con = null";
  686. $script .= ")
  687. {";
  688. }
  689. protected function getAccessorLazyLoadSnippet(Column $col)
  690. {
  691. if ($col->isLazyLoad()) {
  692. $clo = strtolower($col->getName());
  693. $defaultValueString = 'null';
  694. $def = $col->getDefaultValue();
  695. if ($def !== null && !$def->isExpression()) {
  696. $defaultValueString = $this->getDefaultValueString($col);
  697. }
  698. return "
  699. if (!\$this->{$clo}_isLoaded && \$this->{$clo} === {$defaultValueString} && !\$this->isNew()) {
  700. \$this->load{$col->getPhpName()}(\$con);
  701. }
  702. ";
  703. }
  704. }
  705. /**
  706. * Adds the body of the temporal accessor
  707. * @param string &$script The script will be modified in this method.
  708. * @param Column $col The current column.
  709. * @see addTemporalAccessor
  710. **/
  711. protected function addTemporalAccessorBody(&$script, Column $col) {
  712. $cfc = $col->getPhpName();
  713. $clo = strtolower($col->getName());
  714. $useDateTime = $this->getBuildProperty('useDateTimeClass');
  715. $dateTimeClass = $this->getBuildProperty('dateTimeClass');
  716. if (!$dateTimeClass) {
  717. $dateTimeClass = 'DateTime';
  718. }
  719. $this->declareClasses($dateTimeClass);
  720. $defaultfmt = null;
  721. // Default date/time formatter strings are specified in build.properties
  722. if ($col->getType() === PropelTypes::DATE) {
  723. $defaultfmt = $this->getBuildProperty('defaultDateFormat');
  724. } elseif ($col->getType() === PropelTypes::TIME) {
  725. $defaultfmt = $this->getBuildProperty('defaultTimeFormat');
  726. } elseif ($col->getType() === PropelTypes::TIMESTAMP) {
  727. $defaultfmt = $this->getBuildProperty('defaultTimeStampFormat');
  728. }
  729. if (empty($defaultfmt)) { $defaultfmt = null; }
  730. $handleMysqlDate = false;
  731. if ($this->getPlatform() instanceof MysqlPlatform) {
  732. if ($col->getType() === PropelTypes::TIMESTAMP) {
  733. $handleMysqlDate = true;
  734. $mysqlInvalidDateString = '0000-00-00 00:00:00';
  735. } elseif ($col->getType() === PropelTypes::DATE) {
  736. $handleMysqlDate = true;
  737. $mysqlInvalidDateString = '0000-00-00';
  738. }
  739. // 00:00:00 is a valid time, so no need to check for that.
  740. }
  741. if ($col->isLazyLoad()) {
  742. $script .= $this->getAccessorLazyLoadSnippet($col);
  743. }
  744. $script .= "
  745. if (\$this->$clo === null) {
  746. return null;
  747. }
  748. ";
  749. if ($handleMysqlDate) {
  750. $script .= "
  751. if (\$this->$clo === '$mysqlInvalidDateString') {
  752. // while technically this is not a default value of NULL,
  753. // this seems to be closest in meaning.
  754. return null;
  755. } else {
  756. try {
  757. \$dt = new $dateTimeClass(\$this->$clo);
  758. } catch (Exception \$x) {
  759. throw new PropelException(\"Internally stored date/time/timestamp value could not be converted to $dateTimeClass: \" . var_export(\$this->$clo, true), \$x);
  760. }
  761. }
  762. ";
  763. } else {
  764. $script .= "
  765. try {
  766. \$dt = new $dateTimeClass(\$this->$clo);
  767. } catch (Exception \$x) {
  768. throw new PropelException(\"Internally stored date/time/timestamp value could not be converted to $dateTimeClass: \" . var_export(\$this->$clo, true), \$x);
  769. }
  770. ";
  771. } // if handleMyqlDate
  772. $script .= "
  773. if (\$format === null) {";
  774. if ($useDateTime) {
  775. $script .= "
  776. // Because propel.useDateTimeClass is TRUE, we return a $dateTimeClass object.
  777. return \$dt;";
  778. } else {
  779. $script .= "
  780. // We cast here to maintain BC in API; obviously we will lose data if we're dealing with pre-/post-epoch dates.
  781. return (int) \$dt->format('U');";
  782. }
  783. $script .= "
  784. } elseif (strpos(\$format, '%') !== false) {
  785. return strftime(\$format, \$dt->format('U'));
  786. } else {
  787. return \$dt->format(\$format);
  788. }";
  789. }
  790. /**
  791. * Adds the body of the temporal accessor
  792. * @param string &$script The script will be modified in this method.
  793. * @param Column $col The current column.
  794. * @see addTemporalAccessorClose
  795. **/
  796. protected function addTemporalAccessorClose(&$script, Column $col) {
  797. $script .= "
  798. }
  799. ";
  800. }
  801. /**
  802. * Adds an object getter method.
  803. * @param string &$script The script will be modified in this method.
  804. * @param Column $col The current column.
  805. * @see parent::addColumnAccessors()
  806. */
  807. protected function addObjectAccessor(&$script, Column $col)
  808. {
  809. $this->addDefaultAccessorComment($script, $col);
  810. $this->addDefaultAccessorOpen($script, $col);
  811. $this->addObjectAccessorBody($script, $col);
  812. $this->addDefaultAccessorClose($script, $col);
  813. }
  814. /**
  815. * Adds the function body for an object accessor method
  816. * @param string &$script The script will be modified in this method.
  817. * @param Column $col The current column.
  818. * @see addDefaultAccessor()
  819. **/
  820. protected function addObjectAccessorBody(&$script, Column $col)
  821. {
  822. $cfc = $col->getPhpName();
  823. $clo = strtolower($col->getName());
  824. $cloUnserialized = $clo.'_unserialized';
  825. if ($col->isLazyLoad()) {
  826. $script .= $this->getAccessorLazyLoadSnippet($col);
  827. }
  828. $script .= "
  829. if (null == \$this->$cloUnserialized && null !== \$this->$clo) {
  830. \$this->$cloUnserialized = unserialize(\$this->$clo);
  831. }
  832. return \$this->$cloUnserialized;";
  833. }
  834. /**
  835. * Adds an array getter method.
  836. * @param string &$script The script will be modified in this method.
  837. * @param Column $col The current column.
  838. * @see parent::addColumnAccessors()
  839. */
  840. protected function addArrayAccessor(&$script, Column $col)
  841. {
  842. $this->addDefaultAccessorComment($script, $col);
  843. $this->addDefaultAccessorOpen($script, $col);
  844. $this->addArrayAccessorBody($script, $col);
  845. $this->addDefaultAccessorClose($script, $col);
  846. }
  847. /**
  848. * Adds the function body for an array accessor method
  849. * @param string &$script The script will be modified in this method.
  850. * @param Column $col The current column.
  851. * @see addDefaultAccessor()
  852. **/
  853. protected function addArrayAccessorBody(&$script, Column $col)
  854. {
  855. $cfc = $col->getPhpName();
  856. $clo = strtolower($col->getName());
  857. $cloUnserialized = $clo.'_unserialized';
  858. if ($col->isLazyLoad()) {
  859. $script .= $this->getAccessorLazyLoadSnippet($col);
  860. }
  861. $script .= "
  862. if (null === \$this->$cloUnserialized) {
  863. \$this->$cloUnserialized = array();
  864. }
  865. if (!\$this->$cloUnserialized && null !== \$this->$clo) {
  866. \$$cloUnserialized = substr(\$this->$clo, 2, -2);
  867. \$this->$cloUnserialized = \$$cloUnserialized ? explode(' | ', \$$cloUnserialized) : array();
  868. }
  869. return \$this->$cloUnserialized;";
  870. }
  871. /**
  872. * Adds an enum getter method.
  873. * @param string &$script The script will be modified in this method.
  874. * @param Column $col The current column.
  875. * @see parent::addColumnAccessors()
  876. */
  877. protected function addEnumAccessor(&$script, Column $col)
  878. {
  879. $this->addDefaultAccessorComment($script, $col);
  880. $this->addDefaultAccessorOpen($script, $col);
  881. $this->addEnumAccessorBody($script, $col);
  882. $this->addDefaultAccessorClose($script, $col);
  883. }
  884. /**
  885. * Adds the function body for an enum accessor method
  886. * @param string &$script The script will be modified in this method.
  887. * @param Column $col The current column.
  888. * @see addDefaultAccessor()
  889. **/
  890. protected function addEnumAccessorBody(&$script, Column $col)
  891. {
  892. $cfc = $col->getPhpName();
  893. $clo = strtolower($col->getName());
  894. if ($col->isLazyLoad()) {
  895. $script .= $this->getAccessorLazyLoadSnippet($col);
  896. }
  897. $script .= "
  898. if (null === \$this->$clo) {
  899. return null;
  900. }
  901. \$valueSet = " . $this->getPeerClassname() . "::getValueSet(" . $this->getColumnConstant($col) . ");
  902. if (!isset(\$valueSet[\$this->$clo])) {
  903. throw new PropelException('Unknown stored enum key: ' . \$this->$clo);
  904. }
  905. return \$valueSet[\$this->$clo];";
  906. }
  907. /**
  908. * Adds a tester method for an array column.
  909. * @param string &$script The script will be modified in this method.
  910. * @param Column $col The current column.
  911. */
  912. protected function addHasArrayElement(&$script, Column $col)
  913. {
  914. $clo = strtolower($col->getName());
  915. $cfc = $col->getPhpName();
  916. $visibility = $col->getAccessorVisibility();
  917. $singularPhpName = rtrim($cfc, 's');
  918. $script .= "
  919. /**
  920. * Test the presence of a value in the [$clo] array column value.
  921. * @param mixed \$value
  922. * ".$col->getDescription();
  923. if ($col->isLazyLoad()) {
  924. $script .= "
  925. * @param PropelPDO An optional PropelPDO connection to use for fetching this lazy-loaded column.";
  926. }
  927. $script .= "
  928. * @return Boolean
  929. */
  930. $visibility function has$singularPhpName(\$value";
  931. if ($col->isLazyLoad()) $script .= ", PropelPDO \$con = null";
  932. $script .= ")
  933. {
  934. return in_array(\$value, \$this->get$cfc(";
  935. if ($col->isLazyLoad()) $script .= "\$con";
  936. $script .= "));
  937. } // has$singularPhpName()
  938. ";
  939. }
  940. /**
  941. * Adds a normal (non-temporal) getter method.
  942. * @param string &$script The script will be modified in this method.
  943. * @param Column $col The current column.
  944. * @see parent::addColumnAccessors()
  945. */
  946. protected function addDefaultAccessor(&$script, Column $col)
  947. {
  948. $this->addDefaultAccessorComment($script, $col);
  949. $this->addDefaultAccessorOpen($script, $col);
  950. $this->addDefaultAccessorBody($script, $col);
  951. $this->addDefaultAccessorClose($script, $col);
  952. }
  953. /**
  954. * Add the comment for a default accessor method (a getter)
  955. * @param string &$script The script will be modified in this method.
  956. * @param Column $col The current column.
  957. * @see addDefaultAccessor()
  958. **/
  959. public function addDefaultAccessorComment(&$script, Column $col) {
  960. $clo=strtolower($col->getName());
  961. $script .= "
  962. /**
  963. * Get the [$clo] column value.
  964. * ".$col->getDescription();
  965. if ($col->isLazyLoad()) {
  966. $script .= "
  967. * @param PropelPDO An optional PropelPDO connection to use for fetching this lazy-loaded column.";
  968. }
  969. $script .= "
  970. * @return ".$col->getPhpType()."
  971. */";
  972. }
  973. /**
  974. * Adds the function declaration for a default accessor
  975. * @param string &$script The script will be modified in this method.
  976. * @param Column $col The current column.
  977. * @see addDefaultAccessor()
  978. **/
  979. public function addDefaultAccessorOpen(&$script, Column $col) {
  980. $cfc = $col->getPhpName();
  981. $visibility = $col->getAccessorVisibility();
  982. $script .= "
  983. ".$visibility." function get$cfc(";
  984. if ($col->isLazyLoad()) $script .= "PropelPDO \$con = null";
  985. $script .= ")
  986. {";
  987. }
  988. /**
  989. * Adds the function body for a default accessor method
  990. * @param string &$script The script will be modified in this method.
  991. * @param Column $col The current column.
  992. * @see addDefaultAccessor()
  993. **/
  994. protected function addDefaultAccessorBody(&$script, Column $col) {
  995. $cfc = $col->getPhpName();
  996. $clo = strtolower($col->getName());
  997. if ($col->isLazyLoad()) {
  998. $script .= $this->getAccessorLazyLoadSnippet($col);
  999. }
  1000. $script .= "
  1001. return \$this->$clo;";
  1002. }
  1003. /**
  1004. * Adds the function close for a default accessor method
  1005. * @param string &$script The script will be modified in this method.
  1006. * @param Column $col The current column.
  1007. * @see addDefaultAccessor()
  1008. **/
  1009. protected function addDefaultAccessorClose(&$script, Column $col) {
  1010. $script .= "
  1011. }
  1012. ";
  1013. }
  1014. /**
  1015. * Adds the lazy loader method.
  1016. * @param string &$script The script will be modified in this method.
  1017. * @param Column $col The current column.
  1018. * @see parent::addColumnAccessors()
  1019. */
  1020. protected function addLazyLoader(&$script, Column $col)
  1021. {
  1022. $this->addLazyLoaderComment($script, $col);
  1023. $this->addLazyLoaderOpen($script, $col);
  1024. $this->addLazyLoaderBody($script, $col);
  1025. $this->addLazyLoaderClose($script, $col);
  1026. }
  1027. /**
  1028. * Adds the comment for the lazy loader method
  1029. * @param string &$script The script will be modified in this method.
  1030. * @param Column $col The current column.
  1031. * @see addLazyLoader()
  1032. **/
  1033. protected function addLazyLoaderComment(&$script, Column $col) {
  1034. $clo = strtolower($col->getName());
  1035. $script .= "
  1036. /**
  1037. * Load the value for the lazy-loaded [$clo] column.
  1038. *
  1039. * This method performs an additional query to return the value for
  1040. * the [$clo] column, since it is not populated by
  1041. * the hydrate() method.
  1042. *
  1043. * @param \$con PropelPDO (optional) The PropelPDO connection to use.
  1044. * @return void
  1045. * @throws PropelException - any underlying error will be wrapped and re-thrown.
  1046. */";
  1047. }
  1048. /**
  1049. * Adds the function declaration for the lazy loader method
  1050. * @param string &$script The script will be modified in this method.
  1051. * @param Column $col The current column.
  1052. * @see addLazyLoader()
  1053. **/
  1054. protected function addLazyLoaderOpen(&$script, Column $col) {
  1055. $cfc = $col->getPhpName();
  1056. $script .= "
  1057. protected function load$cfc(PropelPDO \$con = null)
  1058. {";
  1059. }
  1060. /**
  1061. * Adds the function body for the lazy loader method
  1062. * @param string &$script The script will be modified in this method.
  1063. * @param Column $col The current column.
  1064. * @see addLazyLoader()
  1065. **/
  1066. protected function addLazyLoaderBody(&$script, Column $col) {
  1067. $platform = $this->getPlatform();
  1068. $clo = strtolower($col->getName());
  1069. // pdo_sqlsrv driver requires the use of PDOStatement::bindColumn() or a hex string will be returned
  1070. if ($col->getType() === PropelTypes::BLOB && $platform instanceof SqlsrvPlatform) {
  1071. $script .= "
  1072. \$c = \$this->buildPkeyCriteria();
  1073. \$c->addSelectColumn(".$this->getColumnConstant($col).");
  1074. try {
  1075. \$row = array(0 => null);
  1076. \$stmt = ".$this->getPeerClassname()."::doSelectStmt(\$c, \$con);
  1077. \$stmt->bindColumn(1, \$row[0], PDO::PARAM_LOB, 0, PDO::SQLSRV_ENCODING_BINARY);
  1078. \$stmt->fetch(PDO::FETCH_BOUND);
  1079. \$stmt->closeCursor();";
  1080. } else {
  1081. $script .= "
  1082. \$c = \$this->buildPkeyCriteria();
  1083. \$c->addSelectColumn(".$this->getColumnConstant($col).");
  1084. try {
  1085. \$stmt = ".$this->getPeerClassname()."::doSelectStmt(\$c, \$con);
  1086. \$row = \$stmt->fetch(PDO::FETCH_NUM);
  1087. \$stmt->closeCursor();";
  1088. }
  1089. if ($col->getType() === PropelTypes::CLOB && $platform instanceof OraclePlatform) {
  1090. // PDO_OCI returns a stream for CLOB objects, while other PDO adapters return a string...
  1091. $script .= "
  1092. \$this->$clo = stream_get_contents(\$row[0]);";
  1093. } elseif ($col->isLobType() && !$platform->hasStreamBlobImpl()) {
  1094. $script .= "
  1095. if (\$row[0] !== null) {
  1096. \$this->$clo = fopen('php://memory', 'r+');
  1097. fwrite(\$this->$clo, \$row[0]);
  1098. rewind(\$this->$clo);
  1099. } else {
  1100. \$this->$clo = null;
  1101. }";
  1102. } elseif ($col->isPhpPrimitiveType()) {
  1103. $script .= "
  1104. \$this->$clo = (\$row[0] !== null) ? (".$col->getPhpType().") \$row[0] : null;";
  1105. } elseif ($col->isPhpObjectType()) {
  1106. $script .= "
  1107. \$this->$clo = (\$row[0] !== null) ? new ".$col->getPhpType()."(\$row[0]) : null;";
  1108. } else {
  1109. $script .= "
  1110. \$this->$clo = \$row[0];";
  1111. }
  1112. $script .= "
  1113. \$this->".$clo."_isLoaded = true;
  1114. } catch (Exception \$e) {
  1115. throw new PropelException(\"Error loading value for [$clo] column on demand.\", \$e);
  1116. }";
  1117. }
  1118. /**
  1119. * Adds the function close for the lazy loader
  1120. * @param string &$script The script will be modified in this method.
  1121. * @param Column $col The current column.
  1122. * @see addLazyLoader()
  1123. **/
  1124. protected function addLazyLoaderClose(&$script, Column $col) {
  1125. $script .= "
  1126. }";
  1127. } // addLazyLoader()
  1128. // --------------------------------------------------------------
  1129. //
  1130. // M U T A T O R M E T H O D S
  1131. //
  1132. // --------------------------------------------------------------
  1133. /**
  1134. * Adds the open of the mutator (setter) method for a column.
  1135. * @param string &$script The script will be modified in this method.
  1136. * @param Column $col The current column.
  1137. */
  1138. protected function addMutatorOpen(&$script, Column $col)
  1139. {
  1140. $this->addMutatorComment($script, $col);
  1141. $this->addMutatorOpenOpen($script, $col);
  1142. $this->addMutatorOpenBody($script, $col);
  1143. }
  1144. /**
  1145. * Adds the comment for a mutator
  1146. * @param string &$script The script will be modified in this method.
  1147. * @param Column $col The current column.
  1148. * @see addMutatorOpen()
  1149. **/
  1150. public function addMutatorComment(&$script, Column $col) {
  1151. $clo = strtolower($col->getName());
  1152. $script .= "
  1153. /**
  1154. * Set the value of [$clo] column.
  1155. * ".$col->getDescription()."
  1156. * @param ".$col->getPhpType()." \$v new value
  1157. * @return ".$this->getObjectClassname()." The current object (for fluent API support)
  1158. */";
  1159. }
  1160. /**
  1161. * Adds the mutator function declaration
  1162. * @param string &$script The script will be modified in this method.
  1163. * @param Column $col The current column.
  1164. * @see addMutatorOpen()
  1165. **/
  1166. public function addMutatorOpenOpen(&$script, Column $col) {
  1167. $cfc = $col->getPhpName();
  1168. $visibility = $col->getMutatorVisibility();
  1169. $script .= "
  1170. ".$visibility." function set$cfc(\$v)
  1171. {";
  1172. }
  1173. /**
  1174. * Adds the mutator open body part
  1175. * @param string &$script The script will be modified in this method.
  1176. * @param Column $col The current column.
  1177. * @see addMutatorOpen()
  1178. **/
  1179. protected function addMutatorOpenBody(&$script, Column $col)
  1180. {
  1181. $clo = strtolower($col->getName());
  1182. $cfc = $col->getPhpName();
  1183. if ($col->isLazyLoad()) {
  1184. $script .= "
  1185. // explicitly set the is-loaded flag to true for this lazy load col;
  1186. // it doesn't matter if the value is actually set or not (logic below) as
  1187. // any attempt to set the value means that no db lookup should be performed
  1188. // when the get$cfc() method is called.
  1189. \$this->".$clo."_isLoaded = true;
  1190. ";
  1191. }
  1192. }
  1193. /**
  1194. * Adds the close of the mutator (setter) method for a column.
  1195. *
  1196. * @param string &$script The script will be modified in this method.
  1197. * @param Column $col The current column.
  1198. */
  1199. protected function addMutatorClose(&$script, Column $col)
  1200. {
  1201. $this->addMutatorCloseBody($script, $col);
  1202. $this->addMutatorCloseClose($script, $col);
  1203. }
  1204. /**
  1205. * Adds the body of the close part of a mutator
  1206. * @param string &$script The script will be modified in this method.
  1207. * @param Column $col The current column.
  1208. * @see addMutatorClose()
  1209. **/
  1210. protected function addMutatorCloseBody(&$script, Column $col) {
  1211. $table = $this->getTable();
  1212. $cfc = $col->getPhpName();
  1213. $clo = strtolower($col->getName());
  1214. if ($col->isForeignKey()) {
  1215. foreach ($col->getForeignKeys() as $fk) {
  1216. $tblFK = $table->getDatabase()->getTable($fk->getForeignTableName());
  1217. $colFK = $tblFK->getColumn($fk->getMappedForeignColumn($col->getName()));
  1218. $varName = $this->getFKVarName($fk);
  1219. $script .= "
  1220. if (\$this->$varName !== null && \$this->".$varName."->get".$colFK->getPhpName()."() !== \$v) {
  1221. \$this->$varName = null;
  1222. }
  1223. ";
  1224. } // foreach fk
  1225. } /* if col is foreign key */
  1226. foreach ($col->getReferrers() as $refFK) {
  1227. $tblFK = $this->getDatabase()->getTable($refFK->getForeignTableName());
  1228. if ( $tblFK->getName() != $table->getName() ) {
  1229. foreach ($col->getForeignKeys() as $fk) {
  1230. $tblFK = $table->getDatabase()->getTable($fk->getForeignTableName());
  1231. $colFK = $tblFK->getColumn($fk->getMappedForeignColumn($col->getName()));
  1232. if ($refFK->isLocalPrimaryKey()) {
  1233. $varName = $this->getPKRefFKVarName($refFK);
  1234. $script .= "
  1235. // update associated ".$tblFK->getPhpName()."
  1236. if (\$this->$varName !== null) {
  1237. \$this->{$varName}->set".$colFK->getPhpName()."(\$v);
  1238. }
  1239. ";
  1240. } else {
  1241. $collName = $this->getRefFKCollVarName($refFK);
  1242. $script .= "
  1243. // update associated ".$tblFK->getPhpName()."
  1244. if (\$this->$collName !== null) {
  1245. foreach (\$this->$collName as \$referrerObject) {
  1246. \$referrerObject->set".$colFK->getPhpName()."(\$v);
  1247. }
  1248. }
  1249. ";
  1250. } // if (isLocalPrimaryKey
  1251. } // foreach col->getPrimaryKeys()
  1252. } // if tablFk != table
  1253. } // foreach
  1254. }
  1255. /**
  1256. * Adds the close for the mutator close
  1257. * @param string &$script The script will be modified in this method.
  1258. * @param Column $col The current column.
  1259. * @see addMutatorClose()
  1260. **/
  1261. protected function addMutatorCloseClose(&$script, Column $col) {
  1262. $cfc = $col->getPhpName();
  1263. $script .= "
  1264. return \$this;
  1265. } // set$cfc()
  1266. ";
  1267. }
  1268. /**
  1269. * Adds a setter for BLOB columns.
  1270. * @param string &$script The script will be modified in this method.
  1271. * @param Column $col The current column.
  1272. * @see parent::addColumnMutators()
  1273. */
  1274. protected function addLobMutator(&$script, Column $col)
  1275. {
  1276. $this->addMutatorOpen($script, $col);
  1277. $clo = strtolower($col->getName());
  1278. $script .= "
  1279. // Because BLOB columns are streams in PDO we have to assume that they are
  1280. // always modified when a new value is passed in. For example, the contents
  1281. // of the stream itself may have changed externally.
  1282. if (!is_resource(\$v) && \$v !== null) {
  1283. \$this->$clo = fopen('php://memory', 'r+');
  1284. fwrite(\$this->$clo, \$v);
  1285. rewind(\$this->$clo);
  1286. } else { // it's already a stream
  1287. \$this->$clo = \$v;
  1288. }
  1289. \$this->modifiedColumns[] = ".$this->getColumnConstant($col).";
  1290. ";
  1291. $this->addMutatorClose($script, $col);
  1292. } // addLobMutatorSnippet
  1293. /**
  1294. * Adds a setter method for date/time/timestamp columns.
  1295. * @param string &$script The script will be modified in this method.
  1296. * @param Column $col The current column.
  1297. * @see parent::addColumnMutators()
  1298. */
  1299. protected function addTemporalMutator(&$script, Column $col)
  1300. {
  1301. $cfc = $col->getPhpName();
  1302. $clo = strtolower($col->getName());
  1303. $visibility = $col->getMutatorVisibility();
  1304. $dateTimeClass = $this->getBuildProperty('dateTimeClass');
  1305. if (!$dateTimeClass) {
  1306. $dateTimeClass = 'DateTime';
  1307. }
  1308. $this->declareClasses($dateTimeClass, 'DateTimeZone', 'PropelDateTime');
  1309. $this->addTemporalMutatorComment($script, $col);
  1310. $this->addMutatorOpenOpen($script, $col);
  1311. $this->addMutatorOpenBody($script, $col);
  1312. $fmt = var_export($this->getTemporalFormatter($col), true);
  1313. $script .= "
  1314. \$dt = PropelDateTime::newInstance(\$v, null, '$dateTimeClass');
  1315. if (\$this->$clo !== null || \$dt !== null) {
  1316. \$currentDateAsString = (\$this->$clo !== null && \$tmpDt = new $dateTimeClass(\$this->$clo)) ? \$tmpDt->format($fmt) : null;
  1317. \$newDateAsString = \$dt ? \$dt->format($fmt) : null;";
  1318. if (($def = $col->getDefaultValue()) !== null && !$def->isExpression()) {
  1319. $defaultValue = $this->getDefaultValueString($col);
  1320. $script .= "
  1321. if ( (\$currentDateAsString !== \$newDateAsString) // normalized values don't match
  1322. || (\$dt->format($fmt) === $defaultValue) // or the entered value matches the default
  1323. ) {";
  1324. } else {
  1325. $script .= "
  1326. if (\$currentDateAsString !== \$newDateAsString) {";
  1327. }
  1328. $script .= "
  1329. \$this->$clo = \$newDateAsString;
  1330. \$this->modifiedColumns[] = ".$this->getColumnConstant($col).";
  1331. }
  1332. } // if either are not null
  1333. ";
  1334. $this->addMutatorClose($script, $col);
  1335. }
  1336. public function addTemporalMutatorComment(&$script, Column $col)
  1337. {
  1338. $cfc = $col->getPhpName();
  1339. $clo = strtolower($col->getName());
  1340. $script .= "
  1341. /**
  1342. * Sets the value of [$clo] column to a normalized version of the date/time value specified.
  1343. * ".$col->getDescription()."
  1344. * @param mixed \$v string, integer (timestamp), or DateTime value.
  1345. * Empty strings are treated as NULL.
  1346. * @return ".$this->getObjectClassname()." The current object (for fluent API support)
  1347. */";
  1348. }
  1349. /**
  1350. * Adds a setter for Object columns.
  1351. * @param string &$script The script will be modified in this method.
  1352. * @param Column $col The current column.
  1353. * @see parent::addColumnMutators()
  1354. */
  1355. protected function addObjectMutator(&$script, Column $col)
  1356. {
  1357. $clo = strtolower($col->getName());
  1358. $cloUnserialized = $clo.'_unserialized';
  1359. $this->addMutatorOpen($script, $col);
  1360. $script .= "
  1361. if (\$this->$cloUnserialized !== \$v";
  1362. if (($def = $col->getDefaultValue()) !== null && !$def->isExpression()) {
  1363. $script .= " || \$this->isNew()";
  1364. }
  1365. $script .= ") {
  1366. \$this->$cloUnserialized = \$v;
  1367. \$this->$clo = serialize(\$v);
  1368. \$this->modifiedColumns[] = ".$this->getColumnConstant($col).";
  1369. }
  1370. ";
  1371. $this->addMutatorClose($script, $col);
  1372. }
  1373. /**
  1374. * Adds a setter for Array columns.
  1375. * @param string &$script The script will be modified in this method.
  1376. * @param Column $col The current column.
  1377. * @see parent::addColumnMutators()
  1378. */
  1379. protected function addArrayMutator(&$script, Column $col)
  1380. {
  1381. $clo = strtolower($col->getName());
  1382. $cloUnserialized = $clo.'_unserialized';
  1383. $this->addMutatorOpen($script, $col);
  1384. $script .= "
  1385. if (\$this->$cloUnserialized !== \$v";
  1386. if (($def = $col->getDefaultValue()) !== null && !$def->isExpression()) {
  1387. $script .= " || \$this->isNew()";
  1388. }
  1389. $script .= ") {
  1390. \$this->$cloUnserialized = \$v;
  1391. \$this->$clo = '| ' . implode(' | ', \$v) . ' |';
  1392. \$this->modifiedColumns[] = ".$this->getColumnConstant($col).";
  1393. }
  1394. ";
  1395. $this->addMutatorClose($script, $col);
  1396. }
  1397. /**
  1398. * Adds a push method for an array column.
  1399. * @param string &$script The script will be modified in this method.
  1400. * @param Column $col The current column.
  1401. */
  1402. protected function addAddArrayElement(&$script, Column $col)
  1403. {
  1404. $clo = strtolower($col->getName());
  1405. $cfc = $col->getPhpName();
  1406. $visibility = $col->getAccessorVisibility();
  1407. $singularPhpName = rtrim($cfc, 's');
  1408. $script .= "
  1409. /**
  1410. * Adds a value to the [$clo] array column value.
  1411. * @param mixed \$value
  1412. * ".$col->getDescription();
  1413. if ($col->isLazyLoad()) {
  1414. $script .= "
  1415. * @param PropelPDO An optional PropelPDO connection to use for fetching this lazy-loaded column.";
  1416. }
  1417. $script .= "
  1418. * @return ".$this->getObjectClassname()." The current object (for fluent API support)
  1419. */
  1420. $visibility function add$singularPhpName(\$value";
  1421. if ($col->isLazyLoad()) $script .= ", PropelPDO \$con = null";
  1422. $script .= ")
  1423. {
  1424. \$currentArray = \$this->get$cfc(";
  1425. if ($col->isLazyLoad()) $script .= "\$con";
  1426. $script .= ");
  1427. \$currentArray []= \$value;
  1428. \$this->set$cfc(\$currentArray);
  1429. return \$this;
  1430. } // add$singularPhpName()
  1431. ";
  1432. }
  1433. /**
  1434. * Adds a remove method for an array column.
  1435. * @param string &$script The script will be modified in this method.
  1436. * @param Column $col The current column.
  1437. */
  1438. protected function addRemoveArrayElement(&$script, Column $col)
  1439. {
  1440. $clo = strtolower($col->getName());
  1441. $cfc = $col->getPhpName();
  1442. $visibility = $col->getAccessorVisibility();
  1443. $singularPhpName = rtrim($cfc, 's');
  1444. $script .= "
  1445. /**
  1446. * Removes a value from the [$clo] array column value.
  1447. * @param mixed \$value
  1448. * ".$col->getDescription();
  1449. if ($col->isLazyLoad()) {
  1450. $script .= "
  1451. * @param PropelPDO An optional PropelPDO connection to use for fetching this lazy-loaded column.";
  1452. }
  1453. $script .= "
  1454. * @return ".$this->getObjectClassname()." The current object (for fluent API support)
  1455. */
  1456. $visibility function remove$singularPhpName(\$value";
  1457. if ($col->isLazyLoad()) $script .= ", PropelPDO \$con = null";
  1458. // we want to reindex the array, so array_ functions are not the best choice
  1459. $script .= ")
  1460. {
  1461. \$targetArray = array();
  1462. foreach (\$this->get$cfc(";
  1463. if ($col->isLazyLoad()) $script .= "\$con";
  1464. $script .= ") as \$element) {
  1465. if (\$element != \$value) {
  1466. \$targetArray []= \$element;
  1467. }
  1468. }
  1469. \$this->set$cfc(\$targetArray);
  1470. return \$this;
  1471. } // remove$singularPhpName()
  1472. ";
  1473. }
  1474. /**
  1475. * Adds a setter for Enum columns.
  1476. * @param string &$script The script will be modified in this method.
  1477. * @param Column $col The current column.
  1478. * @see parent::addColumnMutators()
  1479. */
  1480. protected function addEnumMutator(&$script, Column $col)
  1481. {
  1482. $clo = strtolower($col->getName());
  1483. $this->addMutatorOpen($script, $col);
  1484. $script .= "
  1485. if (\$v !== null) {
  1486. \$valueSet = " . $this->getPeerClassname() . "::getValueSet(" . $this->getColumnConstant($col) . ");
  1487. if (!in_array(\$v, \$valueSet)) {
  1488. throw new PropelException(sprintf('Value \"%s\" is not accepted in this enumerated column', \$v));
  1489. }
  1490. \$v = array_search(\$v, \$valueSet);
  1491. }
  1492. if (\$this->$clo !== \$v";
  1493. if (($def = $col->getDefaultValue()) !== null && !$def->isExpression()) {
  1494. $script .= " || \$this->isNew()";
  1495. }
  1496. $script .= ") {
  1497. \$this->$clo = \$v;
  1498. \$this->modifiedColumns[] = ".$this->getColumnConstant($col).";
  1499. }
  1500. ";
  1501. $this->addMutatorClose($script, $col);
  1502. }
  1503. /**
  1504. * Adds setter method for boolean columns.
  1505. * @param string &$script The script will be modified in this method.
  1506. * @param Column $col The current column.
  1507. * @see parent::addColumnMutators()
  1508. */
  1509. protected function addBooleanMutator(&$script, Column $col)
  1510. {
  1511. $clo = strtolower($col->getName());
  1512. $this->addBooleanMutatorComment($script, $col);
  1513. $this->addMutatorOpenOpen($script, $col);
  1514. $this->addMutatorOpenBody($script, $col);
  1515. $script .= "
  1516. if (\$v !== null) {
  1517. if (is_string(\$v)) {
  1518. \$v = in_array(strtolower(\$v), array('false', 'off', '-', 'no', 'n', '0')) ? false : true;
  1519. } else {
  1520. \$v = (boolean) \$v;
  1521. }
  1522. }
  1523. ";
  1524. $script .= "
  1525. if (\$this->$clo !== \$v";
  1526. if (($def = $col->getDefaultValue()) !== null && !$def->isExpression()) {
  1527. $script .= " || \$this->isNew()";
  1528. }
  1529. $script .= ") {
  1530. \$this->$clo = \$v;
  1531. \$this->modifiedColumns[] = ".$this->getColumnConstant($col).";
  1532. }
  1533. ";
  1534. $this->addMutatorClose($script, $col);
  1535. }
  1536. public function addBooleanMutatorComment(&$script, Column $col)
  1537. {
  1538. $cfc = $col->getPhpName();
  1539. $clo = strtolower($col->getName());
  1540. $script .= "
  1541. /**
  1542. * Sets the value of the [$clo] column.
  1543. * Non-boolean arguments are converted using the following rules:
  1544. * * 1, '1', 'true', 'on', and 'yes' are converted to boolean true
  1545. * * 0, '0', 'false', 'off', and 'no' are converted to boolean false
  1546. * Check on string values is case insensitive (so 'FaLsE' is seen as 'false').
  1547. * ".$col->getDescription()."
  1548. * @param boolean|integer|string \$v The new value
  1549. * @return ".$this->getObjectClassname()." The current object (for fluent API support)
  1550. */";
  1551. }
  1552. /**
  1553. * Adds setter method for "normal" columns.
  1554. * @param string &$script The script will be modified in this method.
  1555. * @param Column $col The current column.
  1556. * @see parent::addColumnMutators()
  1557. */
  1558. protected function addDefaultMutator(&$script, Column $col)
  1559. {
  1560. $clo = strtolower($col->getName());
  1561. $this->addMutatorOpen($script, $col);
  1562. // Perform type-casting to ensure that we can use type-sensitive
  1563. // checking in mutators.
  1564. if ($col->isPhpPrimitiveType()) {
  1565. $script .= "
  1566. if (\$v !== null) {
  1567. \$v = (".$col->getPhpType().") \$v;
  1568. }
  1569. ";
  1570. }
  1571. $script .= "
  1572. if (\$this->$clo !== \$v";
  1573. if (($def = $col->getDefaultValue()) !== null && !$def->isExpression()) {
  1574. $script .= " || \$this->isNew()";
  1575. }
  1576. $script .= ") {
  1577. \$this->$clo = \$v;
  1578. \$this->modifiedColumns[] = ".$this->getColumnConstant($col).";
  1579. }
  1580. ";
  1581. $this->addMutatorClose($script, $col);
  1582. }
  1583. /**
  1584. * Adds the hasOnlyDefaultValues() method.
  1585. * @param string &$script The script will be modified in this method.
  1586. */
  1587. protected function addHasOnlyDefaultValues(&$script)
  1588. {
  1589. $this->addHasOnlyDefaultValuesComment($script);
  1590. $this->addHasOnlyDefaultValuesOpen($script);
  1591. $this->addHasOnlyDefaultValuesBody($script);
  1592. $this->addHasOnlyDefaultValuesClose($script);
  1593. }
  1594. /**
  1595. * Adds the comment for the hasOnlyDefaultValues method
  1596. * @param string &$script The script will be modified in this method.
  1597. * @see addHasOnlyDefaultValues
  1598. **/
  1599. protected function addHasOnlyDefaultValuesComment(&$script) {
  1600. $script .= "
  1601. /**
  1602. * Indicates whether the columns in this object are only set to default values.
  1603. *
  1604. * This method can be used in conjunction with isModified() to indicate whether an object is both
  1605. * modified _and_ has some values set which are non-default.
  1606. *
  1607. * @return boolean Whether the columns in this object are only been set with default values.
  1608. */";
  1609. }
  1610. /**
  1611. * Adds the function declaration for the hasOnlyDefaultValues method
  1612. * @param string &$script The script will be modified in this method.
  1613. * @see addHasOnlyDefaultValues
  1614. **/
  1615. protected function addHasOnlyDefaultValuesOpen(&$script) {
  1616. $script .= "
  1617. public function hasOnlyDefaultValues()
  1618. {";
  1619. }
  1620. /**
  1621. * Adds the function body for the hasOnlyDefaultValues method
  1622. * @param string &$script The script will be modified in this method.
  1623. * @see addHasOnlyDefaultValues
  1624. **/
  1625. protected function addHasOnlyDefaultValuesBody(&$script) {
  1626. $table = $this->getTable();
  1627. $colsWithDefaults = array();
  1628. foreach ($table->getColumns() as $col) {
  1629. $def = $col->getDefaultValue();
  1630. if ($def !== null && !$def->isExpression()) {
  1631. $colsWithDefaults[] = $col;
  1632. }
  1633. }
  1634. foreach ($colsWithDefaults as $col) {
  1635. $clo = strtolower($col->getName());
  1636. $def = $col->getDefaultValue();
  1637. $script .= "
  1638. if (\$this->$clo !== " . $this->getDefaultValueString($col).") {
  1639. return false;
  1640. }
  1641. ";
  1642. }
  1643. }
  1644. /**
  1645. * Adds the function close for the hasOnlyDefaultValues method
  1646. * @param string &$script The script will be modified in this method.
  1647. * @see addHasOnlyDefaultValues
  1648. **/
  1649. protected function addHasOnlyDefaultValuesClose(&$script) {
  1650. $script .= "
  1651. // otherwise, everything was equal, so return TRUE
  1652. return true;";
  1653. $script .= "
  1654. } // hasOnlyDefaultValues()
  1655. ";
  1656. }
  1657. /**
  1658. * Adds the hydrate() method, which sets attributes of the object based on a ResultSet.
  1659. * @param string &$script The script will be modified in this method.
  1660. */
  1661. protected function addHydrate(&$script)
  1662. {
  1663. $this->addHydrateComment($script);
  1664. $this->addHydrateOpen($script);
  1665. $this->addHydrateBody($script);
  1666. $this->addHydrateClose($script);
  1667. }
  1668. /**
  1669. * Adds the comment for the hydrate method
  1670. * @param string &$script The script will be modified in this method.
  1671. * @see addHydrate()
  1672. */
  1673. protected function addHydrateComment(&$script) {
  1674. $script .= "
  1675. /**
  1676. * Hydrates (populates) the object variables with values from the database resultset.
  1677. *
  1678. * An offset (0-based \"start column\") is specified so that objects can be hydrated
  1679. * with a subset of the columns in the resultset rows. This is needed, for example,
  1680. * for results of JOIN queries where the resultset row includes columns from two or
  1681. * more tables.
  1682. *
  1683. * @param array \$row The row returned by PDOStatement->fetch(PDO::FETCH_NUM)
  1684. * @param int \$startcol 0-based offset column which indicates which restultset column to start with.
  1685. * @param boolean \$rehydrate Whether this object is being re-hydrated from the database.
  1686. * @return int next starting column
  1687. * @throws PropelException - Any caught Exception will be rewrapped as a PropelException.
  1688. */";
  1689. }
  1690. /**
  1691. * Adds the function declaration for the hydrate method
  1692. * @param string &$script The script will be modified in this method.
  1693. * @see addHydrate()
  1694. */
  1695. protected function addHydrateOpen(&$script) {
  1696. $script .= "
  1697. public function hydrate(\$row, \$startcol = 0, \$rehydrate = false)
  1698. {";
  1699. }
  1700. /**
  1701. * Adds the function body for the hydrate method
  1702. * @param string &$script The script will be modified in this method.
  1703. * @see addHydrate()
  1704. */
  1705. protected function addHydrateBody(&$script) {
  1706. $table = $this->getTable();
  1707. $platform = $this->getPlatform();
  1708. $script .= "
  1709. try {
  1710. ";
  1711. $n = 0;
  1712. foreach ($table->getColumns() as $col) {
  1713. if (!$col->isLazyLoad()) {
  1714. $clo = strtolower($col->getName());
  1715. if ($col->getType() === PropelTypes::CLOB_EMU && $this->getPlatform() instanceof OraclePlatform) {
  1716. // PDO_OCI returns a stream for CLOB objects, while other PDO adapters return a string...
  1717. $script .= "
  1718. \$this->$clo = stream_get_contents(\$row[\$startcol + $n]);";
  1719. } elseif ($col->isLobType() && !$platform->hasStreamBlobImpl()) {
  1720. $script .= "
  1721. if (\$row[\$startcol + $n] !== null) {
  1722. \$this->$clo = fopen('php://memory', 'r+');
  1723. fwrite(\$this->$clo, \$row[\$startcol + $n]);
  1724. rewind(\$this->$clo);
  1725. } else {
  1726. \$this->$clo = null;
  1727. }";
  1728. } elseif ($col->isPhpPrimitiveType()) {
  1729. $script .= "
  1730. \$this->$clo = (\$row[\$startcol + $n] !== null) ? (".$col->getPhpType().") \$row[\$startcol + $n] : null;";
  1731. } elseif ($col->getType() == PropelTypes::OBJECT) {
  1732. $script .= "
  1733. \$this->$clo = \$row[\$startcol + $n];";
  1734. } elseif ($col->isPhpObjectType()) {
  1735. $script .= "
  1736. \$this->$clo = (\$row[\$startcol + $n] !== null) ? new ".$col->getPhpType()."(\$row[\$startcol + $n]) : null;";
  1737. } else {
  1738. $script .= "
  1739. \$this->$clo = \$row[\$startcol + $n];";
  1740. }
  1741. $n++;
  1742. } // if col->isLazyLoad()
  1743. } /* foreach */
  1744. if ($this->getBuildProperty("addSaveMethod")) {
  1745. $script .= "
  1746. \$this->resetModified();
  1747. ";
  1748. }
  1749. $script .= "
  1750. \$this->setNew(false);
  1751. if (\$rehydrate) {
  1752. \$this->ensureConsistency();
  1753. }
  1754. return \$startcol + $n; // $n = ".$this->getPeerClassname()."::NUM_HYDRATE_COLUMNS.
  1755. } catch (Exception \$e) {
  1756. throw new PropelException(\"Error populating ".$this->getStubObjectBuilder()->getClassname()." object\", \$e);
  1757. }";
  1758. }
  1759. /**
  1760. * Adds the function close for the hydrate method
  1761. * @param string &$script The script will be modified in this method.
  1762. * @see addHydrate()
  1763. */
  1764. protected function addHydrateClose(&$script) {
  1765. $script .= "
  1766. }
  1767. ";
  1768. }
  1769. /**
  1770. * Adds the buildPkeyCriteria method
  1771. * @param string &$script The script will be modified in this method.
  1772. **/
  1773. protected function addBuildPkeyCriteria(&$script) {
  1774. $this->addBuildPkeyCriteriaComment($script);
  1775. $this->addBuildPkeyCriteriaOpen($script);
  1776. $this->addBuildPkeyCriteriaBody($script);
  1777. $this->addBuildPkeyCriteriaClose($script);
  1778. }
  1779. /**
  1780. * Adds the comment for the buildPkeyCriteria method
  1781. * @param string &$script The script will be modified in this method.
  1782. * @see addBuildPkeyCriteria()
  1783. **/
  1784. protected function addBuildPkeyCriteriaComment(&$script) {
  1785. $script .= "
  1786. /**
  1787. * Builds a Criteria object containing the primary key for this object.
  1788. *
  1789. * Unlike buildCriteria() this method includes the primary key values regardless
  1790. * of whether or not they have been modified.
  1791. *
  1792. * @return Criteria The Criteria object containing value(s) for primary key(s).
  1793. */";
  1794. }
  1795. /**
  1796. * Adds the function declaration for the buildPkeyCriteria method
  1797. * @param string &$script The script will be modified in this method.
  1798. * @see addBuildPkeyCriteria()
  1799. **/
  1800. protected function addBuildPkeyCriteriaOpen(&$script) {
  1801. $script .= "
  1802. public function buildPkeyCriteria()
  1803. {";
  1804. }
  1805. /**
  1806. * Adds the function body for the buildPkeyCriteria method
  1807. * @param string &$script The script will be modified in this method.
  1808. * @see addBuildPkeyCriteria()
  1809. **/
  1810. protected function addBuildPkeyCriteriaBody(&$script) {
  1811. $script .= "
  1812. \$criteria = new Criteria(".$this->getPeerClassname()."::DATABASE_NAME);";
  1813. foreach ($this->getTable()->getPrimaryKey() as $col) {
  1814. $clo = strtolower($col->getName());
  1815. $script .= "
  1816. \$criteria->add(".$this->getColumnConstant($col).", \$this->$clo);";
  1817. }
  1818. }
  1819. /**
  1820. * Adds the function close for the buildPkeyCriteria method
  1821. * @param string &$script The script will be modified in this method.
  1822. * @see addBuildPkeyCriteria()
  1823. **/
  1824. protected function addBuildPkeyCriteriaClose(&$script) {
  1825. $script .= "
  1826. return \$criteria;
  1827. }
  1828. ";
  1829. }
  1830. /**
  1831. * Adds the buildCriteria method
  1832. * @param string &$script The script will be modified in this method.
  1833. **/
  1834. protected function addBuildCriteria(&$script)
  1835. {
  1836. $this->addBuildCriteriaComment($script);
  1837. $this->addBuildCriteriaOpen($script);
  1838. $this->addBuildCriteriaBody($script);
  1839. $this->addBuildCriteriaClose($script);
  1840. }
  1841. /**
  1842. * Adds comment for the buildCriteria method
  1843. * @param string &$script The script will be modified in this method.
  1844. * @see addBuildCriteria()
  1845. **/
  1846. protected function addBuildCriteriaComment(&$script) {
  1847. $script .= "
  1848. /**
  1849. * Build a Criteria object containing the values of all modified columns in this object.
  1850. *
  1851. * @return Criteria The Criteria object containing all modified values.
  1852. */";
  1853. }
  1854. /**
  1855. * Adds the function declaration of the buildCriteria method
  1856. * @param string &$script The script will be modified in this method.
  1857. * @see addBuildCriteria()
  1858. **/
  1859. protected function addBuildCriteriaOpen(&$script) {
  1860. $script .= "
  1861. public function buildCriteria()
  1862. {";
  1863. }
  1864. /**
  1865. * Adds the function body of the buildCriteria method
  1866. * @param string &$script The script will be modified in this method.
  1867. * @see addBuildCriteria()
  1868. **/
  1869. protected function addBuildCriteriaBody(&$script) {
  1870. $script .= "
  1871. \$criteria = new Criteria(".$this->getPeerClassname()."::DATABASE_NAME);
  1872. ";
  1873. foreach ($this->getTable()->getColumns() as $col) {
  1874. $clo = strtolower($col->getName());
  1875. $script .= "
  1876. if (\$this->isColumnModified(".$this->getColumnConstant($col).")) \$criteria->add(".$this->getColumnConstant($col).", \$this->$clo);";
  1877. }
  1878. }
  1879. /**
  1880. * Adds the function close of the buildCriteria method
  1881. * @param string &$script The script will be modified in this method.
  1882. * @see addBuildCriteria()
  1883. **/
  1884. protected function addBuildCriteriaClose(&$script) {
  1885. $script .= "
  1886. return \$criteria;
  1887. }
  1888. ";
  1889. }
  1890. /**
  1891. * Adds the toArray method
  1892. * @param string &$script The script will be modified in this method.
  1893. **/
  1894. protected function addToArray(&$script)
  1895. {
  1896. $fks = $this->getTable()->getForeignKeys();
  1897. $referrers = $this->getTable()->getReferrers();
  1898. $hasFks = count($fks) > 0 || count($referrers) > 0;
  1899. $objectClassName = $this->getObjectClassname();
  1900. $pkGetter = $this->getTable()->hasCompositePrimaryKey() ? 'serialize($this->getPrimaryKey())' : '$this->getPrimaryKey()';
  1901. $script .= "
  1902. /**
  1903. * Exports the object as an array.
  1904. *
  1905. * You can specify the key type of the array by passing one of the class
  1906. * type constants.
  1907. *
  1908. * @param string \$keyType (optional) One of the class type constants BasePeer::TYPE_PHPNAME, BasePeer::TYPE_STUDLYPHPNAME,
  1909. * BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM.
  1910. * Defaults to BasePeer::TYPE_PHPNAME.
  1911. * @param boolean \$includeLazyLoadColumns (optional) Whether to include lazy loaded columns. Defaults to TRUE.
  1912. * @param array \$alreadyDumpedObjects List of objects to skip to avoid recursion";
  1913. if ($hasFks) {
  1914. $script .= "
  1915. * @param boolean \$includeForeignObjects (optional) Whether to include hydrated related objects. Default to FALSE.";
  1916. }
  1917. $script .= "
  1918. *
  1919. * @return array an associative array containing the field names (as keys) and field values
  1920. */
  1921. public function toArray(\$keyType = BasePeer::TYPE_PHPNAME, \$includeLazyLoadColumns = true, \$alreadyDumpedObjects = array()" . ($hasFks ? ", \$includeForeignObjects = false" : '') . ")
  1922. {
  1923. if (isset(\$alreadyDumpedObjects['$objectClassName'][$pkGetter])) {
  1924. return '*RECURSION*';
  1925. }
  1926. \$alreadyDumpedObjects['$objectClassName'][$pkGetter] = true;
  1927. \$keys = ".$this->getPeerClassname()."::getFieldNames(\$keyType);
  1928. \$result = array(";
  1929. foreach ($this->getTable()->getColumns() as $num => $col) {
  1930. if ($col->isLazyLoad()) {
  1931. $script .= "
  1932. \$keys[$num] => (\$includeLazyLoadColumns) ? \$this->get".$col->getPhpName()."() : null,";
  1933. } else {
  1934. $script .= "
  1935. \$keys[$num] => \$this->get".$col->getPhpName()."(),";
  1936. }
  1937. }
  1938. $script .= "
  1939. );";
  1940. if ($hasFks) {
  1941. $script .= "
  1942. if (\$includeForeignObjects) {";
  1943. foreach ($fks as $fk) {
  1944. $script .= "
  1945. if (null !== \$this->" . $this->getFKVarName($fk) . ") {
  1946. \$result['" . $this->getFKPhpNameAffix($fk, $plural = false) . "'] = \$this->" . $this->getFKVarName($fk) . "->toArray(\$keyType, \$includeLazyLoadColumns, \$alreadyDumpedObjects, true);
  1947. }";
  1948. }
  1949. foreach ($referrers as $fk) {
  1950. if ($fk->isLocalPrimaryKey()) {
  1951. $script .= "
  1952. if (null !== \$this->" . $this->getPKRefFKVarName($fk) . ") {
  1953. \$result['" . $this->getRefFKPhpNameAffix($fk, $plural = false) . "'] = \$this->" . $this->getPKRefFKVarName($fk) . "->toArray(\$keyType, \$includeLazyLoadColumns, \$alreadyDumpedObjects, true);
  1954. }";
  1955. } else {
  1956. $script .= "
  1957. if (null !== \$this->" . $this->getRefFKCollVarName($fk) . ") {
  1958. \$result['" . $this->getRefFKPhpNameAffix($fk, $plural = true) . "'] = \$this->" . $this->getRefFKCollVarName($fk) . "->toArray(null, true, \$keyType, \$includeLazyLoadColumns, \$alreadyDumpedObjects);
  1959. }";
  1960. }
  1961. }
  1962. $script .= "
  1963. }";
  1964. }
  1965. $script .= "
  1966. return \$result;
  1967. }
  1968. ";
  1969. } // addToArray()
  1970. /**
  1971. * Adds the getByName method
  1972. * @param string &$script The script will be modified in this method.
  1973. **/
  1974. protected function addGetByName(&$script)
  1975. {
  1976. $this->addGetByNameComment($script);
  1977. $this->addGetByNameOpen($script);
  1978. $this->addGetByNameBody($script);
  1979. $this->addGetByNameClose($script);
  1980. }
  1981. /**
  1982. * Adds the comment for the getByName method
  1983. * @param string &$script The script will be modified in this method.
  1984. * @see addGetByName
  1985. **/
  1986. protected function addGetByNameComment(&$script) {
  1987. $script .= "
  1988. /**
  1989. * Retrieves a field from the object by name passed in as a string.
  1990. *
  1991. * @param string \$name name
  1992. * @param string \$type The type of fieldname the \$name is of:
  1993. * one of the class type constants BasePeer::TYPE_PHPNAME, BasePeer::TYPE_STUDLYPHPNAME
  1994. * BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM
  1995. * @return mixed Value of field.
  1996. */";
  1997. }
  1998. /**
  1999. * Adds the function declaration for the getByName method
  2000. * @param string &$script The script will be modified in this method.
  2001. * @see addGetByName
  2002. **/
  2003. protected function addGetByNameOpen(&$script) {
  2004. $script .= "
  2005. public function getByName(\$name, \$type = BasePeer::TYPE_PHPNAME)
  2006. {";
  2007. }
  2008. /**
  2009. * Adds the function body for the getByName method
  2010. * @param string &$script The script will be modified in this method.
  2011. * @see addGetByName
  2012. **/
  2013. protected function addGetByNameBody(&$script) {
  2014. $script .= "
  2015. \$pos = ".$this->getPeerClassname()."::translateFieldName(\$name, \$type, BasePeer::TYPE_NUM);
  2016. \$field = \$this->getByPosition(\$pos);";
  2017. }
  2018. /**
  2019. * Adds the function close for the getByName method
  2020. * @param string &$script The script will be modified in this method.
  2021. * @see addGetByName
  2022. **/
  2023. protected function addGetByNameClose(&$script) {
  2024. $script .= "
  2025. return \$field;
  2026. }
  2027. ";
  2028. }
  2029. /**
  2030. * Adds the getByPosition method
  2031. * @param string &$script The script will be modified in this method.
  2032. **/
  2033. protected function addGetByPosition(&$script)
  2034. {
  2035. $this->addGetByPositionComment($script);
  2036. $this->addGetByPositionOpen($script);
  2037. $this->addGetByPositionBody($script);
  2038. $this->addGetByPositionClose($script);
  2039. }
  2040. /**
  2041. * Adds comment for the getByPosition method
  2042. * @param string &$script The script will be modified in this method.
  2043. * @see addGetByPosition
  2044. **/
  2045. protected function addGetByPositionComment(&$script) {
  2046. $script .= "
  2047. /**
  2048. * Retrieves a field from the object by Position as specified in the xml schema.
  2049. * Zero-based.
  2050. *
  2051. * @param int \$pos position in xml schema
  2052. * @return mixed Value of field at \$pos
  2053. */";
  2054. }
  2055. /**
  2056. * Adds the function declaration for the getByPosition method
  2057. * @param string &$script The script will be modified in this method.
  2058. * @see addGetByPosition
  2059. **/
  2060. protected function addGetByPositionOpen(&$script) {
  2061. $script .= "
  2062. public function getByPosition(\$pos)
  2063. {";
  2064. }
  2065. /**
  2066. * Adds the function body for the getByPosition method
  2067. * @param string &$script The script will be modified in this method.
  2068. * @see addGetByPosition
  2069. **/
  2070. protected function addGetByPositionBody(&$script) {
  2071. $table = $this->getTable();
  2072. $script .= "
  2073. switch(\$pos) {";
  2074. $i = 0;
  2075. foreach ($table->getColumns() as $col) {
  2076. $cfc = $col->getPhpName();
  2077. $script .= "
  2078. case $i:
  2079. return \$this->get$cfc();
  2080. break;";
  2081. $i++;
  2082. } /* foreach */
  2083. $script .= "
  2084. default:
  2085. return null;
  2086. break;
  2087. } // switch()";
  2088. }
  2089. /**
  2090. * Adds the function close for the getByPosition method
  2091. * @param string &$script The script will be modified in this method.
  2092. * @see addGetByPosition
  2093. **/
  2094. protected function addGetByPositionClose(&$script) {
  2095. $script .= "
  2096. }
  2097. ";
  2098. }
  2099. protected function addSetByName(&$script)
  2100. {
  2101. $table = $this->getTable();
  2102. $script .= "
  2103. /**
  2104. * Sets a field from the object by name passed in as a string.
  2105. *
  2106. * @param string \$name peer name
  2107. * @param mixed \$value field value
  2108. * @param string \$type The type of fieldname the \$name is of:
  2109. * one of the class type constants BasePeer::TYPE_PHPNAME, BasePeer::TYPE_STUDLYPHPNAME
  2110. * BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM
  2111. * @return void
  2112. */
  2113. public function setByName(\$name, \$value, \$type = BasePeer::TYPE_PHPNAME)
  2114. {
  2115. \$pos = ".$this->getPeerClassname()."::translateFieldName(\$name, \$type, BasePeer::TYPE_NUM);
  2116. return \$this->setByPosition(\$pos, \$value);
  2117. }
  2118. ";
  2119. }
  2120. protected function addSetByPosition(&$script)
  2121. {
  2122. $table = $this->getTable();
  2123. $script .= "
  2124. /**
  2125. * Sets a field from the object by Position as specified in the xml schema.
  2126. * Zero-based.
  2127. *
  2128. * @param int \$pos position in xml schema
  2129. * @param mixed \$value field value
  2130. * @return void
  2131. */
  2132. public function setByPosition(\$pos, \$value)
  2133. {
  2134. switch(\$pos) {";
  2135. $i = 0;
  2136. foreach ($table->getColumns() as $col) {
  2137. $cfc = $col->getPhpName();
  2138. $cptype = $col->getPhpType();
  2139. $script .= "
  2140. case $i:
  2141. \$this->set$cfc(\$value);
  2142. break;";
  2143. $i++;
  2144. } /* foreach */
  2145. $script .= "
  2146. } // switch()
  2147. }
  2148. ";
  2149. } // addSetByPosition()
  2150. protected function addFromArray(&$script)
  2151. {
  2152. $table = $this->getTable();
  2153. $script .= "
  2154. /**
  2155. * Populates the object using an array.
  2156. *
  2157. * This is particularly useful when populating an object from one of the
  2158. * request arrays (e.g. \$_POST). This method goes through the column
  2159. * names, checking to see whether a matching key exists in populated
  2160. * array. If so the setByName() method is called for that column.
  2161. *
  2162. * You can specify the key type of the array by additionally passing one
  2163. * of the class type constants BasePeer::TYPE_PHPNAME, BasePeer::TYPE_STUDLYPHPNAME,
  2164. * BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM.
  2165. * The default key type is the column's phpname (e.g. 'AuthorId')
  2166. *
  2167. * @param array \$arr An array to populate the object from.
  2168. * @param string \$keyType The type of keys the array uses.
  2169. * @return void
  2170. */
  2171. public function fromArray(\$arr, \$keyType = BasePeer::TYPE_PHPNAME)
  2172. {
  2173. \$keys = ".$this->getPeerClassname()."::getFieldNames(\$keyType);
  2174. ";
  2175. foreach ($table->getColumns() as $num => $col) {
  2176. $cfc = $col->getPhpName();
  2177. $cptype = $col->getPhpType();
  2178. $script .= "
  2179. if (array_key_exists(\$keys[$num], \$arr)) \$this->set$cfc(\$arr[\$keys[$num]]);";
  2180. } /* foreach */
  2181. $script .= "
  2182. }
  2183. ";
  2184. } // addFromArray
  2185. /**
  2186. * Adds a delete() method to remove the object form the datastore.
  2187. * @param string &$script The script will be modified in this method.
  2188. */
  2189. protected function addDelete(&$script)
  2190. {
  2191. $this->addDeleteComment($script);
  2192. $this->addDeleteOpen($script);
  2193. $this->addDeleteBody($script);
  2194. $this->addDeleteClose($script);
  2195. }
  2196. /**
  2197. * Adds the comment for the delete function
  2198. * @param string &$script The script will be modified in this method.
  2199. * @see addDelete()
  2200. **/
  2201. protected function addDeleteComment(&$script) {
  2202. $script .= "
  2203. /**
  2204. * Removes this object from datastore and sets delete attribute.
  2205. *
  2206. * @param PropelPDO \$con
  2207. * @return void
  2208. * @throws PropelException
  2209. * @see BaseObject::setDeleted()
  2210. * @see BaseObject::isDeleted()
  2211. */";
  2212. }
  2213. /**
  2214. * Adds the function declaration for the delete function
  2215. * @param string &$script The script will be modified in this method.
  2216. * @see addDelete()
  2217. **/
  2218. protected function addDeleteOpen(&$script) {
  2219. $script .= "
  2220. public function delete(PropelPDO \$con = null)
  2221. {";
  2222. }
  2223. /**
  2224. * Adds the function body for the delete function
  2225. * @param string &$script The script will be modified in this method.
  2226. * @see addDelete()
  2227. **/
  2228. protected function addDeleteBody(&$script) {
  2229. $script .= "
  2230. if (\$this->isDeleted()) {
  2231. throw new PropelException(\"This object has already been deleted.\");
  2232. }
  2233. if (\$con === null) {
  2234. \$con = Propel::getConnection(".$this->getPeerClassname()."::DATABASE_NAME, Propel::CONNECTION_WRITE);
  2235. }
  2236. \$con->beginTransaction();
  2237. try {";
  2238. if($this->getGeneratorConfig()->getBuildProperty('addHooks')) {
  2239. $script .= "
  2240. \$ret = \$this->preDelete(\$con);";
  2241. // apply behaviors
  2242. $this->applyBehaviorModifier('preDelete', $script, " ");
  2243. $script .= "
  2244. if (\$ret) {
  2245. ".$this->getQueryClassname()."::create()
  2246. ->filterByPrimaryKey(\$this->getPrimaryKey())
  2247. ->delete(\$con);
  2248. \$this->postDelete(\$con);";
  2249. // apply behaviors
  2250. $this->applyBehaviorModifier('postDelete', $script, " ");
  2251. $script .= "
  2252. \$con->commit();
  2253. \$this->setDeleted(true);
  2254. } else {
  2255. \$con->commit();
  2256. }";
  2257. } else {
  2258. // apply behaviors
  2259. $this->applyBehaviorModifier('preDelete', $script, " ");
  2260. $script .= "
  2261. ".$this->getPeerClassname()."::doDelete(\$this, \$con);";
  2262. // apply behaviors
  2263. $this->applyBehaviorModifier('postDelete', $script, " ");
  2264. $script .= "
  2265. \$con->commit();
  2266. \$this->setDeleted(true);";
  2267. }
  2268. $script .= "
  2269. } catch (PropelException \$e) {
  2270. \$con->rollBack();
  2271. throw \$e;
  2272. }";
  2273. }
  2274. /**
  2275. * Adds the function close for the delete function
  2276. * @param string &$script The script will be modified in this method.
  2277. * @see addDelete()
  2278. **/
  2279. protected function addDeleteClose(&$script) {
  2280. $script .= "
  2281. }
  2282. ";
  2283. } // addDelete()
  2284. /**
  2285. * Adds a reload() method to re-fetch the data for this object from the database.
  2286. * @param string &$script The script will be modified in this method.
  2287. */
  2288. protected function addReload(&$script)
  2289. {
  2290. $table = $this->getTable();
  2291. $script .= "
  2292. /**
  2293. * Reloads this object from datastore based on primary key and (optionally) resets all associated objects.
  2294. *
  2295. * This will only work if the object has been saved and has a valid primary key set.
  2296. *
  2297. * @param boolean \$deep (optional) Whether to also de-associated any related objects.
  2298. * @param PropelPDO \$con (optional) The PropelPDO connection to use.
  2299. * @return void
  2300. * @throws PropelException - if this object is deleted, unsaved or doesn't have pk match in db
  2301. */
  2302. public function reload(\$deep = false, PropelPDO \$con = null)
  2303. {
  2304. if (\$this->isDeleted()) {
  2305. throw new PropelException(\"Cannot reload a deleted object.\");
  2306. }
  2307. if (\$this->isNew()) {
  2308. throw new PropelException(\"Cannot reload an unsaved object.\");
  2309. }
  2310. if (\$con === null) {
  2311. \$con = Propel::getConnection(".$this->getPeerClassname()."::DATABASE_NAME, Propel::CONNECTION_READ);
  2312. }
  2313. // We don't need to alter the object instance pool; we're just modifying this instance
  2314. // already in the pool.
  2315. \$stmt = ".$this->getPeerClassname()."::doSelectStmt(\$this->buildPkeyCriteria(), \$con);
  2316. \$row = \$stmt->fetch(PDO::FETCH_NUM);
  2317. \$stmt->closeCursor();
  2318. if (!\$row) {
  2319. throw new PropelException('Cannot find matching row in the database to reload object values.');
  2320. }
  2321. \$this->hydrate(\$row, 0, true); // rehydrate
  2322. ";
  2323. // support for lazy load columns
  2324. foreach ($table->getColumns() as $col) {
  2325. if ($col->isLazyLoad()) {
  2326. $clo = strtolower($col->getName());
  2327. $script .= "
  2328. // Reset the $clo lazy-load column
  2329. \$this->" . $clo . " = null;
  2330. \$this->".$clo."_isLoaded = false;
  2331. ";
  2332. }
  2333. }
  2334. $script .= "
  2335. if (\$deep) { // also de-associate any related objects?
  2336. ";
  2337. foreach ($table->getForeignKeys() as $fk) {
  2338. $varName = $this->getFKVarName($fk);
  2339. $script .= "
  2340. \$this->".$varName." = null;";
  2341. }
  2342. foreach ($table->getReferrers() as $refFK) {
  2343. if ($refFK->isLocalPrimaryKey()) {
  2344. $script .= "
  2345. \$this->".$this->getPKRefFKVarName($refFK)." = null;
  2346. ";
  2347. } else {
  2348. $script .= "
  2349. \$this->".$this->getRefFKCollVarName($refFK)." = null;
  2350. ";
  2351. }
  2352. }
  2353. foreach ($table->getCrossFks() as $fkList) {
  2354. list($refFK, $crossFK) = $fkList;
  2355. $script .= "
  2356. \$this->" . $this->getCrossFKVarName($crossFK). " = null;";
  2357. }
  2358. $script .= "
  2359. } // if (deep)
  2360. }
  2361. ";
  2362. } // addReload()
  2363. /**
  2364. * Adds the methods related to refreshing, saving and deleting the object.
  2365. * @param string &$script The script will be modified in this method.
  2366. */
  2367. protected function addManipulationMethods(&$script)
  2368. {
  2369. $this->addReload($script);
  2370. $this->addDelete($script);
  2371. $this->addSave($script);
  2372. $this->addDoSave($script);
  2373. }
  2374. /**
  2375. * Adds the methods related to validationg the object.
  2376. * @param string &$script The script will be modified in this method.
  2377. */
  2378. protected function addValidationMethods(&$script)
  2379. {
  2380. $this->addValidationFailuresAttribute($script);
  2381. $this->addGetValidationFailures($script);
  2382. $this->addValidate($script);
  2383. $this->addDoValidate($script);
  2384. }
  2385. /**
  2386. * Adds the $validationFailures attribute to store ValidationFailed objects.
  2387. * @param string &$script The script will be modified in this method.
  2388. */
  2389. protected function addValidationFailuresAttribute(&$script)
  2390. {
  2391. $script .= "
  2392. /**
  2393. * Array of ValidationFailed objects.
  2394. * @var array ValidationFailed[]
  2395. */
  2396. protected \$validationFailures = array();
  2397. ";
  2398. }
  2399. /**
  2400. * Adds the getValidationFailures() method.
  2401. * @param string &$script The script will be modified in this method.
  2402. */
  2403. protected function addGetValidationFailures(&$script)
  2404. {
  2405. $script .= "
  2406. /**
  2407. * Gets any ValidationFailed objects that resulted from last call to validate().
  2408. *
  2409. *
  2410. * @return array ValidationFailed[]
  2411. * @see validate()
  2412. */
  2413. public function getValidationFailures()
  2414. {
  2415. return \$this->validationFailures;
  2416. }
  2417. ";
  2418. } // addGetValidationFailures()
  2419. /**
  2420. * Adds the correct getPrimaryKey() method for this object.
  2421. * @param string &$script The script will be modified in this method.
  2422. */
  2423. protected function addGetPrimaryKey(&$script)
  2424. {
  2425. $pkeys = $this->getTable()->getPrimaryKey();
  2426. if (count($pkeys) == 1) {
  2427. $this->addGetPrimaryKey_SinglePK($script);
  2428. } elseif (count($pkeys) > 1) {
  2429. $this->addGetPrimaryKey_MultiPK($script);
  2430. } else {
  2431. // no primary key -- this is deprecated, since we don't *need* this method anymore
  2432. $this->addGetPrimaryKey_NoPK($script);
  2433. }
  2434. }
  2435. /**
  2436. * Adds the getPrimaryKey() method for tables that contain a single-column primary key.
  2437. * @param string &$script The script will be modified in this method.
  2438. */
  2439. protected function addGetPrimaryKey_SinglePK(&$script)
  2440. {
  2441. $table = $this->getTable();
  2442. $pkeys = $table->getPrimaryKey();
  2443. $cptype = $pkeys[0]->getPhpType();
  2444. $script .= "
  2445. /**
  2446. * Returns the primary key for this object (row).
  2447. * @return $cptype
  2448. */
  2449. public function getPrimaryKey()
  2450. {
  2451. return \$this->get".$pkeys[0]->getPhpName()."();
  2452. }
  2453. ";
  2454. } // addetPrimaryKey_SingleFK
  2455. /**
  2456. * Adds the setPrimaryKey() method for tables that contain a multi-column primary key.
  2457. * @param string &$script The script will be modified in this method.
  2458. */
  2459. protected function addGetPrimaryKey_MultiPK(&$script)
  2460. {
  2461. $script .= "
  2462. /**
  2463. * Returns the composite primary key for this object.
  2464. * The array elements will be in same order as specified in XML.
  2465. * @return array
  2466. */
  2467. public function getPrimaryKey()
  2468. {
  2469. \$pks = array();";
  2470. $i = 0;
  2471. foreach ($this->getTable()->getPrimaryKey() as $pk) {
  2472. $script .= "
  2473. \$pks[$i] = \$this->get".$pk->getPhpName()."();";
  2474. $i++;
  2475. } /* foreach */
  2476. $script .= "
  2477. return \$pks;
  2478. }
  2479. ";
  2480. } // addGetPrimaryKey_MultiFK()
  2481. /**
  2482. * Adds the getPrimaryKey() method for objects that have no primary key.
  2483. * This "feature" is dreprecated, since the getPrimaryKey() method is not required
  2484. * by the Persistent interface (or used by the templates). Hence, this method is also
  2485. * deprecated.
  2486. * @param string &$script The script will be modified in this method.
  2487. * @deprecated
  2488. */
  2489. protected function addGetPrimaryKey_NoPK(&$script)
  2490. {
  2491. $script .= "
  2492. /**
  2493. * Returns NULL since this table doesn't have a primary key.
  2494. * This method exists only for BC and is deprecated!
  2495. * @return null
  2496. */
  2497. public function getPrimaryKey()
  2498. {
  2499. return null;
  2500. }
  2501. ";
  2502. }
  2503. /**
  2504. * Adds the correct setPrimaryKey() method for this object.
  2505. * @param string &$script The script will be modified in this method.
  2506. */
  2507. protected function addSetPrimaryKey(&$script)
  2508. {
  2509. $pkeys = $this->getTable()->getPrimaryKey();
  2510. if (count($pkeys) == 1) {
  2511. $this->addSetPrimaryKey_SinglePK($script);
  2512. } elseif (count($pkeys) > 1) {
  2513. $this->addSetPrimaryKey_MultiPK($script);
  2514. } else {
  2515. // no primary key -- this is deprecated, since we don't *need* this method anymore
  2516. $this->addSetPrimaryKey_NoPK($script);
  2517. }
  2518. }
  2519. /**
  2520. * Adds the setPrimaryKey() method for tables that contain a single-column primary key.
  2521. * @param string &$script The script will be modified in this method.
  2522. */
  2523. protected function addSetPrimaryKey_SinglePK(&$script)
  2524. {
  2525. $pkeys = $this->getTable()->getPrimaryKey();
  2526. $col = $pkeys[0];
  2527. $clo=strtolower($col->getName());
  2528. $ctype = $col->getPhpType();
  2529. $script .= "
  2530. /**
  2531. * Generic method to set the primary key ($clo column).
  2532. *
  2533. * @param $ctype \$key Primary key.
  2534. * @return void
  2535. */
  2536. public function setPrimaryKey(\$key)
  2537. {
  2538. \$this->set".$col->getPhpName()."(\$key);
  2539. }
  2540. ";
  2541. } // addSetPrimaryKey_SinglePK
  2542. /**
  2543. * Adds the setPrimaryKey() method for tables that contain a multi-columnprimary key.
  2544. * @param string &$script The script will be modified in this method.
  2545. */
  2546. protected function addSetPrimaryKey_MultiPK(&$script)
  2547. {
  2548. $script .="
  2549. /**
  2550. * Set the [composite] primary key.
  2551. *
  2552. * @param array \$keys The elements of the composite key (order must match the order in XML file).
  2553. * @return void
  2554. */
  2555. public function setPrimaryKey(\$keys)
  2556. {";
  2557. $i = 0;
  2558. foreach ($this->getTable()->getPrimaryKey() as $pk) {
  2559. $pktype = $pk->getPhpType();
  2560. $script .= "
  2561. \$this->set".$pk->getPhpName()."(\$keys[$i]);";
  2562. $i++;
  2563. } /* foreach ($table->getPrimaryKey() */
  2564. $script .= "
  2565. }
  2566. ";
  2567. } // addSetPrimaryKey_MultiPK
  2568. /**
  2569. * Adds the setPrimaryKey() method for objects that have no primary key.
  2570. * This "feature" is dreprecated, since the setPrimaryKey() method is not required
  2571. * by the Persistent interface (or used by the templates). Hence, this method is also
  2572. * deprecated.
  2573. * @param string &$script The script will be modified in this method.
  2574. * @deprecated
  2575. */
  2576. protected function addSetPrimaryKey_NoPK(&$script)
  2577. {
  2578. $script .="
  2579. /**
  2580. * Dummy primary key setter.
  2581. *
  2582. * This function only exists to preserve backwards compatibility. It is no longer
  2583. * needed or required by the Persistent interface. It will be removed in next BC-breaking
  2584. * release of Propel.
  2585. *
  2586. * @deprecated
  2587. */
  2588. public function setPrimaryKey(\$pk)
  2589. {
  2590. // do nothing, because this object doesn't have any primary keys
  2591. }
  2592. ";
  2593. }
  2594. /**
  2595. * Adds the isPrimaryKeyNull() method
  2596. * @param string &$script The script will be modified in this method.
  2597. */
  2598. protected function addIsPrimaryKeyNull(&$script)
  2599. {
  2600. $table = $this->getTable();
  2601. $pkeys = $table->getPrimaryKey();
  2602. $script .= "
  2603. /**
  2604. * Returns true if the primary key for this object is null.
  2605. * @return boolean
  2606. */
  2607. public function isPrimaryKeyNull()
  2608. {";
  2609. if (count($pkeys) == 1) {
  2610. $script .= "
  2611. return null === \$this->get" . $pkeys[0]->getPhpName() . "();";
  2612. } else {
  2613. $tests = array();
  2614. foreach ($pkeys as $pkey) {
  2615. $tests[]= "(null === \$this->get" . $pkey->getPhpName() . "())";
  2616. }
  2617. $script .= "
  2618. return " . join(' && ', $tests) . ";";
  2619. }
  2620. $script .= "
  2621. }
  2622. ";
  2623. } // addetPrimaryKey_SingleFK
  2624. // --------------------------------------------------------------------
  2625. // Complex OM Methods
  2626. // --------------------------------------------------------------------
  2627. /**
  2628. * Constructs variable name for fkey-related objects.
  2629. * @param ForeignKey $fk
  2630. * @return string
  2631. */
  2632. public function getFKVarName(ForeignKey $fk)
  2633. {
  2634. return 'a' . $this->getFKPhpNameAffix($fk, $plural = false);
  2635. }
  2636. /**
  2637. * Constructs variable name for objects which referencing current table by specified foreign key.
  2638. * @param ForeignKey $fk
  2639. * @return string
  2640. */
  2641. public function getRefFKCollVarName(ForeignKey $fk)
  2642. {
  2643. return 'coll' . $this->getRefFKPhpNameAffix($fk, $plural = true);
  2644. }
  2645. /**
  2646. * Constructs variable name for single object which references current table by specified foreign key
  2647. * which is ALSO a primary key (hence one-to-one relationship).
  2648. * @param ForeignKey $fk
  2649. * @return string
  2650. */
  2651. public function getPKRefFKVarName(ForeignKey $fk)
  2652. {
  2653. return 'single' . $this->getRefFKPhpNameAffix($fk, $plural = false);
  2654. }
  2655. // ----------------------------------------------------------------
  2656. //
  2657. // F K M E T H O D S
  2658. //
  2659. // ----------------------------------------------------------------
  2660. /**
  2661. * Adds the methods that get & set objects related by foreign key to the current object.
  2662. * @param string &$script The script will be modified in this method.
  2663. */
  2664. protected function addFKMethods(&$script)
  2665. {
  2666. foreach ($this->getTable()->getForeignKeys() as $fk) {
  2667. $this->declareClassFromBuilder($this->getNewStubObjectBuilder($fk->getForeignTable()));
  2668. $this->declareClassFromBuilder($this->getNewStubQueryBuilder($fk->getForeignTable()));
  2669. $this->addFKMutator($script, $fk);
  2670. $this->addFKAccessor($script, $fk);
  2671. } // foreach fk
  2672. }
  2673. /**
  2674. * Adds the class attributes that are needed to store fkey related objects.
  2675. * @param string &$script The script will be modified in this method.
  2676. */
  2677. protected function addFKAttributes(&$script, ForeignKey $fk)
  2678. {
  2679. $className = $this->getForeignTable($fk)->getPhpName();
  2680. $varName = $this->getFKVarName($fk);
  2681. $script .= "
  2682. /**
  2683. * @var $className
  2684. */
  2685. protected $".$varName.";
  2686. ";
  2687. }
  2688. /**
  2689. * Adds the mutator (setter) method for setting an fkey related object.
  2690. * @param string &$script The script will be modified in this method.
  2691. */
  2692. protected function addFKMutator(&$script, ForeignKey $fk)
  2693. {
  2694. $table = $this->getTable();
  2695. $tblFK = $this->getForeignTable($fk);
  2696. $joinTableObjectBuilder = $this->getNewObjectBuilder($tblFK);
  2697. $className = $joinTableObjectBuilder->getObjectClassname();
  2698. $varName = $this->getFKVarName($fk);
  2699. $script .= "
  2700. /**
  2701. * Declares an association between this object and a $className object.
  2702. *
  2703. * @param $className \$v
  2704. * @return ".$this->getObjectClassname()." The current object (for fluent API support)
  2705. * @throws PropelException
  2706. */
  2707. public function set".$this->getFKPhpNameAffix($fk, $plural = false)."($className \$v = null)
  2708. {";
  2709. foreach ($fk->getLocalColumns() as $columnName) {
  2710. $column = $table->getColumn($columnName);
  2711. $lfmap = $fk->getLocalForeignMapping();
  2712. $colFKName = $lfmap[$columnName];
  2713. $colFK = $tblFK->getColumn($colFKName);
  2714. $script .= "
  2715. if (\$v === null) {
  2716. \$this->set".$column->getPhpName()."(".$this->getDefaultValueString($column).");
  2717. } else {
  2718. \$this->set".$column->getPhpName()."(\$v->get".$colFK->getPhpName()."());
  2719. }
  2720. ";
  2721. } /* foreach local col */
  2722. $script .= "
  2723. \$this->$varName = \$v;
  2724. ";
  2725. // Now add bi-directional relationship binding, taking into account whether this is
  2726. // a one-to-one relationship.
  2727. if ($fk->isLocalPrimaryKey()) {
  2728. $script .= "
  2729. // Add binding for other direction of this 1:1 relationship.
  2730. if (\$v !== null) {
  2731. \$v->set".$this->getRefFKPhpNameAffix($fk, $plural = false)."(\$this);
  2732. }
  2733. ";
  2734. } else {
  2735. $script .= "
  2736. // Add binding for other direction of this n:n relationship.
  2737. // If this object has already been added to the $className object, it will not be re-added.
  2738. if (\$v !== null) {
  2739. \$v->add".$this->getRefFKPhpNameAffix($fk, $plural = false)."(\$this);
  2740. }
  2741. ";
  2742. }
  2743. $script .= "
  2744. return \$this;
  2745. }
  2746. ";
  2747. }
  2748. /**
  2749. * Adds the accessor (getter) method for getting an fkey related object.
  2750. * @param string &$script The script will be modified in this method.
  2751. */
  2752. protected function addFKAccessor(&$script, ForeignKey $fk)
  2753. {
  2754. $table = $this->getTable();
  2755. $varName = $this->getFKVarName($fk);
  2756. $fkQueryBuilder = $this->getNewStubQueryBuilder($this->getForeignTable($fk));
  2757. $fkObjectBuilder = $this->getNewObjectBuilder($this->getForeignTable($fk))->getStubObjectBuilder();
  2758. $className = $fkObjectBuilder->getClassname(); // get the Classname that has maybe a prefix
  2759. $and = "";
  2760. $conditional = "";
  2761. $localColumns = array(); // foreign key local attributes names
  2762. // If the related columns are a primary key on the foreign table
  2763. // then use retrieveByPk() instead of doSelect() to take advantage
  2764. // of instance pooling
  2765. $useRetrieveByPk = $fk->isForeignPrimaryKey();
  2766. foreach ($fk->getLocalColumns() as $columnName) {
  2767. $lfmap = $fk->getLocalForeignMapping();
  2768. $localColumn = $table->getColumn($columnName);
  2769. $foreignColumn = $fk->getForeignTable()->getColumn($lfmap[$columnName]);
  2770. $column = $table->getColumn($columnName);
  2771. $cptype = $column->getPhpType();
  2772. $clo = strtolower($column->getName());
  2773. $localColumns[$foreignColumn->getPosition()] = '$this->'.$clo;
  2774. if ($cptype == "integer" || $cptype == "float" || $cptype == "double") {
  2775. $conditional .= $and . "\$this->". $clo ." != 0";
  2776. } elseif ($cptype == "string") {
  2777. $conditional .= $and . "(\$this->" . $clo ." !== \"\" && \$this->".$clo." !== null)";
  2778. } else {
  2779. $conditional .= $and . "\$this->" . $clo ." !== null";
  2780. }
  2781. $and = " && ";
  2782. }
  2783. ksort($localColumns); // restoring the order of the foreign PK
  2784. $localColumns = count($localColumns) > 1 ?
  2785. ('array('.implode(', ', $localColumns).')') : reset($localColumns);
  2786. $script .= "
  2787. /**
  2788. * Get the associated $className object
  2789. *
  2790. * @param PropelPDO Optional Connection object.
  2791. * @return $className The associated $className object.
  2792. * @throws PropelException
  2793. */
  2794. public function get".$this->getFKPhpNameAffix($fk, $plural = false)."(PropelPDO \$con = null)
  2795. {";
  2796. $script .= "
  2797. if (\$this->$varName === null && ($conditional)) {";
  2798. if ($useRetrieveByPk) {
  2799. $script .= "
  2800. \$this->$varName = ".$fkQueryBuilder->getClassname()."::create()->findPk($localColumns, \$con);";
  2801. } else {
  2802. $script .= "
  2803. \$this->$varName = ".$fkQueryBuilder->getClassname()."::create()
  2804. ->filterBy" . $this->getRefFKPhpNameAffix($fk, $plural = false) . "(\$this) // here
  2805. ->findOne(\$con);";
  2806. }
  2807. if ($fk->isLocalPrimaryKey()) {
  2808. $script .= "
  2809. // Because this foreign key represents a one-to-one relationship, we will create a bi-directional association.
  2810. \$this->{$varName}->set".$this->getRefFKPhpNameAffix($fk, $plural = false)."(\$this);";
  2811. } else {
  2812. $script .= "
  2813. /* The following can be used additionally to
  2814. guarantee the related object contains a reference
  2815. to this object. This level of coupling may, however, be
  2816. undesirable since it could result in an only partially populated collection
  2817. in the referenced object.
  2818. \$this->{$varName}->add".$this->getRefFKPhpNameAffix($fk, $plural = true)."(\$this);
  2819. */";
  2820. }
  2821. $script .= "
  2822. }
  2823. return \$this->$varName;
  2824. }
  2825. ";
  2826. } // addFKAccessor
  2827. /**
  2828. * Adds a convenience method for setting a related object by specifying the primary key.
  2829. * This can be used in conjunction with the getPrimaryKey() for systems where nothing is known
  2830. * about the actual objects being related.
  2831. * @param string &$script The script will be modified in this method.
  2832. */
  2833. protected function addFKByKeyMutator(&$script, ForeignKey $fk)
  2834. {
  2835. $table = $this->getTable();
  2836. #$className = $this->getForeignTable($fk)->getPhpName();
  2837. $methodAffix = $this->getFKPhpNameAffix($fk);
  2838. #$varName = $this->getFKVarName($fk);
  2839. $script .= "
  2840. /**
  2841. * Provides convenient way to set a relationship based on a
  2842. * key. e.g.
  2843. * <code>\$bar->setFooKey(\$foo->getPrimaryKey())</code>
  2844. *";
  2845. if (count($fk->getLocalColumns()) > 1) {
  2846. $script .= "
  2847. * Note: It is important that the xml schema used to create this class
  2848. * maintains consistency in the order of related columns between
  2849. * ".$table->getName()." and ". $tblFK->getName().".
  2850. * If for some reason this is impossible, this method should be
  2851. * overridden in <code>".$table->getPhpName()."</code>.";
  2852. }
  2853. $script .= "
  2854. * @return ".$this->getObjectClassname()." The current object (for fluent API support)
  2855. * @throws PropelException
  2856. */
  2857. public function set".$methodAffix."Key(\$key)
  2858. {
  2859. ";
  2860. if (count($fk->getLocalColumns()) > 1) {
  2861. $i = 0;
  2862. foreach ($fk->getLocalColumns() as $colName) {
  2863. $col = $table->getColumn($colName);
  2864. $fktype = $col->getPhpType();
  2865. $script .= "
  2866. \$this->set".$col->getPhpName()."( ($fktype) \$key[$i] );
  2867. ";
  2868. $i++;
  2869. } /* foreach */
  2870. } else {
  2871. $lcols = $fk->getLocalColumns();
  2872. $colName = $lcols[0];
  2873. $col = $table->getColumn($colName);
  2874. $fktype = $col->getPhpType();
  2875. $script .= "
  2876. \$this->set".$col->getPhpName()."( ($fktype) \$key);
  2877. ";
  2878. }
  2879. $script .= "
  2880. return \$this;
  2881. }
  2882. ";
  2883. } // addFKByKeyMutator()
  2884. /**
  2885. * Adds the method that fetches fkey-related (referencing) objects but also joins in data from another table.
  2886. * @param string &$script The script will be modified in this method.
  2887. */
  2888. protected function addRefFKGetJoinMethods(&$script, ForeignKey $refFK)
  2889. {
  2890. $table = $this->getTable();
  2891. $tblFK = $refFK->getTable();
  2892. $join_behavior = $this->getGeneratorConfig()->getBuildProperty('useLeftJoinsInDoJoinMethods') ? 'Criteria::LEFT_JOIN' : 'Criteria::INNER_JOIN';
  2893. $peerClassname = $this->getStubPeerBuilder()->getClassname();
  2894. $fkQueryClassname = $this->getNewStubQueryBuilder($refFK->getTable())->getClassname();
  2895. $relCol = $this->getRefFKPhpNameAffix($refFK, $plural=true);
  2896. $collName = $this->getRefFKCollVarName($refFK);
  2897. $fkPeerBuilder = $this->getNewPeerBuilder($tblFK);
  2898. $className = $fkPeerBuilder->getObjectClassname();
  2899. $lastTable = "";
  2900. foreach ($tblFK->getForeignKeys() as $fk2) {
  2901. $tblFK2 = $this->getForeignTable($fk2);
  2902. $doJoinGet = !$tblFK2->isForReferenceOnly();
  2903. // it doesn't make sense to join in rows from the curent table, since we are fetching
  2904. // objects related to *this* table (i.e. the joined rows will all be the same row as current object)
  2905. if ($this->getTable()->getPhpName() == $tblFK2->getPhpName()) {
  2906. $doJoinGet = false;
  2907. }
  2908. $relCol2 = $this->getFKPhpNameAffix($fk2, $plural = false);
  2909. if ( $this->getRelatedBySuffix($refFK) != "" &&
  2910. ($this->getRelatedBySuffix($refFK) == $this->getRelatedBySuffix($fk2))) {
  2911. $doJoinGet = false;
  2912. }
  2913. if ($doJoinGet) {
  2914. $script .= "
  2915. /**
  2916. * If this collection has already been initialized with
  2917. * an identical criteria, it returns the collection.
  2918. * Otherwise if this ".$table->getPhpName()." is new, it will return
  2919. * an empty collection; or if this ".$table->getPhpName()." has previously
  2920. * been saved, it will retrieve related $relCol from storage.
  2921. *
  2922. * This method is protected by default in order to keep the public
  2923. * api reasonable. You can provide public methods for those you
  2924. * actually need in ".$table->getPhpName().".
  2925. *
  2926. * @param Criteria \$criteria optional Criteria object to narrow the query
  2927. * @param PropelPDO \$con optional connection object
  2928. * @param string \$join_behavior optional join type to use (defaults to $join_behavior)
  2929. * @return PropelCollection|array {$className}[] List of $className objects
  2930. */
  2931. public function get".$relCol."Join".$relCol2."(\$criteria = null, \$con = null, \$join_behavior = $join_behavior)
  2932. {";
  2933. $script .= "
  2934. \$query = $fkQueryClassname::create(null, \$criteria);
  2935. \$query->joinWith('" . $this->getFKPhpNameAffix($fk2, $plural=false) . "', \$join_behavior);
  2936. return \$this->get". $relCol . "(\$query, \$con);
  2937. }
  2938. ";
  2939. } /* end if ($doJoinGet) */
  2940. } /* end foreach ($tblFK->getForeignKeys() as $fk2) { */
  2941. } // function
  2942. // ----------------------------------------------------------------
  2943. //
  2944. // R E F E R R E R F K M E T H O D S
  2945. //
  2946. // ----------------------------------------------------------------
  2947. /**
  2948. * Adds the attributes used to store objects that have referrer fkey relationships to this object.
  2949. * <code>protected collVarName;</code>
  2950. * <code>private lastVarNameCriteria = null;</code>
  2951. * @param string &$script The script will be modified in this method.
  2952. */
  2953. protected function addRefFKAttributes(&$script, ForeignKey $refFK)
  2954. {
  2955. $joinedTableObjectBuilder = $this->getNewObjectBuilder($refFK->getTable());
  2956. $className = $joinedTableObjectBuilder->getObjectClassname();
  2957. if ($refFK->isLocalPrimaryKey()) {
  2958. $script .= "
  2959. /**
  2960. * @var $className one-to-one related $className object
  2961. */
  2962. protected $".$this->getPKRefFKVarName($refFK).";
  2963. ";
  2964. } else {
  2965. $script .= "
  2966. /**
  2967. * @var array {$className}[] Collection to store aggregation of $className objects.
  2968. */
  2969. protected $".$this->getRefFKCollVarName($refFK).";
  2970. ";
  2971. }
  2972. }
  2973. /**
  2974. * Adds the methods for retrieving, initializing, adding objects that are related to this one by foreign keys.
  2975. * @param string &$script The script will be modified in this method.
  2976. */
  2977. protected function addRefFKMethods(&$script)
  2978. {
  2979. foreach ($this->getTable()->getReferrers() as $refFK) {
  2980. $this->declareClassFromBuilder($this->getNewStubObjectBuilder($refFK->getTable()));
  2981. $this->declareClassFromBuilder($this->getNewStubQueryBuilder($refFK->getTable()));
  2982. if ($refFK->isLocalPrimaryKey()) {
  2983. $this->addPKRefFKGet($script, $refFK);
  2984. $this->addPKRefFKSet($script, $refFK);
  2985. } else {
  2986. $this->addRefFKClear($script, $refFK);
  2987. $this->addRefFKInit($script, $refFK);
  2988. $this->addRefFKGet($script, $refFK);
  2989. $this->addRefFKCount($script, $refFK);
  2990. $this->addRefFKAdd($script, $refFK);
  2991. $this->addRefFKGetJoinMethods($script, $refFK);
  2992. }
  2993. }
  2994. }
  2995. /**
  2996. * Adds the method that clears the referrer fkey collection.
  2997. * @param string &$script The script will be modified in this method.
  2998. */
  2999. protected function addRefFKClear(&$script, ForeignKey $refFK) {
  3000. $relCol = $this->getRefFKPhpNameAffix($refFK, $plural = true);
  3001. $collName = $this->getRefFKCollVarName($refFK);
  3002. $script .= "
  3003. /**
  3004. * Clears out the $collName collection
  3005. *
  3006. * This does not modify the database; however, it will remove any associated objects, causing
  3007. * them to be refetched by subsequent calls to accessor method.
  3008. *
  3009. * @return void
  3010. * @see add$relCol()
  3011. */
  3012. public function clear$relCol()
  3013. {
  3014. \$this->$collName = null; // important to set this to NULL since that means it is uninitialized
  3015. }
  3016. ";
  3017. } // addRefererClear()
  3018. /**
  3019. * Adds the method that initializes the referrer fkey collection.
  3020. * @param string &$script The script will be modified in this method.
  3021. */
  3022. protected function addRefFKInit(&$script, ForeignKey $refFK) {
  3023. $relCol = $this->getRefFKPhpNameAffix($refFK, $plural = true);
  3024. $collName = $this->getRefFKCollVarName($refFK);
  3025. $script .= "
  3026. /**
  3027. * Initializes the $collName collection.
  3028. *
  3029. * By default this just sets the $collName collection to an empty array (like clear$collName());
  3030. * however, you may wish to override this method in your stub class to provide setting appropriate
  3031. * to your application -- for example, setting the initial array to the values stored in database.
  3032. *
  3033. * @param boolean \$overrideExisting If set to true, the method call initializes
  3034. * the collection even if it is not empty
  3035. *
  3036. * @return void
  3037. */
  3038. public function init$relCol(\$overrideExisting = true)
  3039. {
  3040. if (null !== \$this->$collName && !\$overrideExisting) {
  3041. return;
  3042. }
  3043. \$this->$collName = new PropelObjectCollection();
  3044. \$this->{$collName}->setModel('" . $this->getNewStubObjectBuilder($refFK->getTable())->getClassname() . "');
  3045. }
  3046. ";
  3047. } // addRefererInit()
  3048. /**
  3049. * Adds the method that adds an object into the referrer fkey collection.
  3050. * @param string &$script The script will be modified in this method.
  3051. */
  3052. protected function addRefFKAdd(&$script, ForeignKey $refFK)
  3053. {
  3054. $tblFK = $refFK->getTable();
  3055. $joinedTableObjectBuilder = $this->getNewObjectBuilder($refFK->getTable());
  3056. $className = $joinedTableObjectBuilder->getObjectClassname();
  3057. $collName = $this->getRefFKCollVarName($refFK);
  3058. $script .= "
  3059. /**
  3060. * Method called to associate a $className object to this object
  3061. * through the $className foreign key attribute.
  3062. *
  3063. * @param $className \$l $className
  3064. * @return void
  3065. * @throws PropelException
  3066. */
  3067. public function add".$this->getRefFKPhpNameAffix($refFK, $plural = false)."($className \$l)
  3068. {
  3069. if (\$this->$collName === null) {
  3070. \$this->init".$this->getRefFKPhpNameAffix($refFK, $plural = true)."();
  3071. }
  3072. if (!\$this->{$collName}->contains(\$l)) { // only add it if the **same** object is not already associated
  3073. \$this->{$collName}[]= \$l;
  3074. \$l->set".$this->getFKPhpNameAffix($refFK, $plural = false)."(\$this);
  3075. }
  3076. }
  3077. ";
  3078. } // addRefererAdd
  3079. /**
  3080. * Adds the method that returns the size of the referrer fkey collection.
  3081. * @param string &$script The script will be modified in this method.
  3082. */
  3083. protected function addRefFKCount(&$script, ForeignKey $refFK)
  3084. {
  3085. $table = $this->getTable();
  3086. $tblFK = $refFK->getTable();
  3087. $peerClassname = $this->getStubPeerBuilder()->getClassname();
  3088. $fkQueryClassname = $this->getNewStubQueryBuilder($refFK->getTable())->getClassname();
  3089. $relCol = $this->getRefFKPhpNameAffix($refFK, $plural = true);
  3090. $collName = $this->getRefFKCollVarName($refFK);
  3091. $joinedTableObjectBuilder = $this->getNewObjectBuilder($refFK->getTable());
  3092. $className = $joinedTableObjectBuilder->getObjectClassname();
  3093. $script .= "
  3094. /**
  3095. * Returns the number of related $className objects.
  3096. *
  3097. * @param Criteria \$criteria
  3098. * @param boolean \$distinct
  3099. * @param PropelPDO \$con
  3100. * @return int Count of related $className objects.
  3101. * @throws PropelException
  3102. */
  3103. public function count$relCol(Criteria \$criteria = null, \$distinct = false, PropelPDO \$con = null)
  3104. {
  3105. if(null === \$this->$collName || null !== \$criteria) {
  3106. if (\$this->isNew() && null === \$this->$collName) {
  3107. return 0;
  3108. } else {
  3109. \$query = $fkQueryClassname::create(null, \$criteria);
  3110. if(\$distinct) {
  3111. \$query->distinct();
  3112. }
  3113. return \$query
  3114. ->filterBy" . $this->getFKPhpNameAffix($refFK) . "(\$this)
  3115. ->count(\$con);
  3116. }
  3117. } else {
  3118. return count(\$this->$collName);
  3119. }
  3120. }
  3121. ";
  3122. } // addRefererCount
  3123. /**
  3124. * Adds the method that returns the referrer fkey collection.
  3125. * @param string &$script The script will be modified in this method.
  3126. */
  3127. protected function addRefFKGet(&$script, ForeignKey $refFK)
  3128. {
  3129. $table = $this->getTable();
  3130. $tblFK = $refFK->getTable();
  3131. $peerClassname = $this->getStubPeerBuilder()->getClassname();
  3132. $fkQueryClassname = $this->getNewStubQueryBuilder($refFK->getTable())->getClassname();
  3133. $relCol = $this->getRefFKPhpNameAffix($refFK, $plural = true);
  3134. $collName = $this->getRefFKCollVarName($refFK);
  3135. $joinedTableObjectBuilder = $this->getNewObjectBuilder($refFK->getTable());
  3136. $className = $joinedTableObjectBuilder->getObjectClassname();
  3137. $script .= "
  3138. /**
  3139. * Gets an array of $className objects which contain a foreign key that references this object.
  3140. *
  3141. * If the \$criteria is not null, it is used to always fetch the results from the database.
  3142. * Otherwise the results are fetched from the database the first time, then cached.
  3143. * Next time the same method is called without \$criteria, the cached collection is returned.
  3144. * If this ".$this->getObjectClassname()." is new, it will return
  3145. * an empty collection or the current collection; the criteria is ignored on a new object.
  3146. *
  3147. * @param Criteria \$criteria optional Criteria object to narrow the query
  3148. * @param PropelPDO \$con optional connection object
  3149. * @return PropelCollection|array {$className}[] List of $className objects
  3150. * @throws PropelException
  3151. */
  3152. public function get$relCol(\$criteria = null, PropelPDO \$con = null)
  3153. {
  3154. if(null === \$this->$collName || null !== \$criteria) {
  3155. if (\$this->isNew() && null === \$this->$collName) {
  3156. // return empty collection
  3157. \$this->init".$this->getRefFKPhpNameAffix($refFK, $plural = true)."();
  3158. } else {
  3159. \$$collName = $fkQueryClassname::create(null, \$criteria)
  3160. ->filterBy" . $this->getFKPhpNameAffix($refFK) . "(\$this)
  3161. ->find(\$con);
  3162. if (null !== \$criteria) {
  3163. return \$$collName;
  3164. }
  3165. \$this->$collName = \$$collName;
  3166. }
  3167. }
  3168. return \$this->$collName;
  3169. }
  3170. ";
  3171. } // addRefererGet()
  3172. /**
  3173. * Adds the method that gets a one-to-one related referrer fkey.
  3174. * This is for one-to-one relationship special case.
  3175. * @param string &$script The script will be modified in this method.
  3176. */
  3177. protected function addPKRefFKGet(&$script, ForeignKey $refFK)
  3178. {
  3179. $table = $this->getTable();
  3180. $tblFK = $refFK->getTable();
  3181. $joinedTableObjectBuilder = $this->getNewObjectBuilder($refFK->getTable());
  3182. $className = $joinedTableObjectBuilder->getObjectClassname();
  3183. $queryClassname = $this->getNewStubQueryBuilder($refFK->getTable())->getClassname();
  3184. $varName = $this->getPKRefFKVarName($refFK);
  3185. $script .= "
  3186. /**
  3187. * Gets a single $className object, which is related to this object by a one-to-one relationship.
  3188. *
  3189. * @param PropelPDO \$con optional connection object
  3190. * @return $className
  3191. * @throws PropelException
  3192. */
  3193. public function get".$this->getRefFKPhpNameAffix($refFK, $plural = false)."(PropelPDO \$con = null)
  3194. {
  3195. ";
  3196. $script .= "
  3197. if (\$this->$varName === null && !\$this->isNew()) {
  3198. \$this->$varName = $queryClassname::create()->findPk(\$this->getPrimaryKey(), \$con);
  3199. }
  3200. return \$this->$varName;
  3201. }
  3202. ";
  3203. } // addPKRefFKGet()
  3204. /**
  3205. * Adds the method that sets a one-to-one related referrer fkey.
  3206. * This is for one-to-one relationships special case.
  3207. * @param string &$script The script will be modified in this method.
  3208. * @param ForeignKey $refFK The referencing foreign key.
  3209. */
  3210. protected function addPKRefFKSet(&$script, ForeignKey $refFK)
  3211. {
  3212. $tblFK = $refFK->getTable();
  3213. $joinedTableObjectBuilder = $this->getNewObjectBuilder($refFK->getTable());
  3214. $className = $joinedTableObjectBuilder->getObjectClassname();
  3215. $varName = $this->getPKRefFKVarName($refFK);
  3216. $script .= "
  3217. /**
  3218. * Sets a single $className object as related to this object by a one-to-one relationship.
  3219. *
  3220. * @param $className \$v $className
  3221. * @return ".$this->getObjectClassname()." The current object (for fluent API support)
  3222. * @throws PropelException
  3223. */
  3224. public function set".$this->getRefFKPhpNameAffix($refFK, $plural = false)."($className \$v = null)
  3225. {
  3226. \$this->$varName = \$v;
  3227. // Make sure that that the passed-in $className isn't already associated with this object
  3228. if (\$v !== null && \$v->get".$this->getFKPhpNameAffix($refFK, $plural = false)."() === null) {
  3229. \$v->set".$this->getFKPhpNameAffix($refFK, $plural = false)."(\$this);
  3230. }
  3231. return \$this;
  3232. }
  3233. ";
  3234. } // addPKRefFKSet
  3235. protected function addCrossFKAttributes(&$script, ForeignKey $crossFK)
  3236. {
  3237. $joinedTableObjectBuilder = $this->getNewObjectBuilder($crossFK->getForeignTable());
  3238. $className = $joinedTableObjectBuilder->getObjectClassname();
  3239. $script .= "
  3240. /**
  3241. * @var array {$className}[] Collection to store aggregation of $className objects.
  3242. */
  3243. protected $" . $this->getCrossFKVarName($crossFK) . ";
  3244. ";
  3245. }
  3246. protected function getCrossFKVarName(ForeignKey $crossFK)
  3247. {
  3248. return 'coll' . $this->getFKPhpNameAffix($crossFK, $plural = true);
  3249. }
  3250. protected function addCrossFKMethods(&$script)
  3251. {
  3252. foreach ($this->getTable()->getCrossFks() as $fkList) {
  3253. list($refFK, $crossFK) = $fkList;
  3254. $this->declareClassFromBuilder($this->getNewStubObjectBuilder($crossFK->getForeignTable()));
  3255. $this->declareClassFromBuilder($this->getNewStubQueryBuilder($crossFK->getForeignTable()));
  3256. $this->addCrossFKClear($script, $crossFK);
  3257. $this->addCrossFKInit($script, $crossFK);
  3258. $this->addCrossFKGet($script, $refFK, $crossFK);
  3259. $this->addCrossFKCount($script, $refFK, $crossFK);
  3260. $this->addCrossFKAdd($script, $refFK, $crossFK);
  3261. }
  3262. }
  3263. /**
  3264. * Adds the method that clears the referrer fkey collection.
  3265. * @param string &$script The script will be modified in this method.
  3266. */
  3267. protected function addCrossFKClear(&$script, ForeignKey $crossFK) {
  3268. $relCol = $this->getFKPhpNameAffix($crossFK, $plural = true);
  3269. $collName = $this->getCrossFKVarName($crossFK);
  3270. $script .= "
  3271. /**
  3272. * Clears out the $collName collection
  3273. *
  3274. * This does not modify the database; however, it will remove any associated objects, causing
  3275. * them to be refetched by subsequent calls to accessor method.
  3276. *
  3277. * @return void
  3278. * @see add$relCol()
  3279. */
  3280. public function clear$relCol()
  3281. {
  3282. \$this->$collName = null; // important to set this to NULL since that means it is uninitialized
  3283. }
  3284. ";
  3285. } // addRefererClear()
  3286. /**
  3287. * Adds the method that initializes the referrer fkey collection.
  3288. * @param string &$script The script will be modified in this method.
  3289. */
  3290. protected function addCrossFKInit(&$script, ForeignKey $crossFK) {
  3291. $relCol = $this->getFKPhpNameAffix($crossFK, $plural = true);
  3292. $collName = $this->getCrossFKVarName($crossFK);
  3293. $relatedObjectClassName = $this->getNewStubObjectBuilder($crossFK->getForeignTable())->getClassname();
  3294. $script .= "
  3295. /**
  3296. * Initializes the $collName collection.
  3297. *
  3298. * By default this just sets the $collName collection to an empty collection (like clear$relCol());
  3299. * however, you may wish to override this method in your stub class to provide setting appropriate
  3300. * to your application -- for example, setting the initial array to the values stored in database.
  3301. *
  3302. * @return void
  3303. */
  3304. public function init$relCol()
  3305. {
  3306. \$this->$collName = new PropelObjectCollection();
  3307. \$this->{$collName}->setModel('$relatedObjectClassName');
  3308. }
  3309. ";
  3310. }
  3311. protected function addCrossFKGet(&$script, $refFK, $crossFK)
  3312. {
  3313. $relatedName = $this->getFKPhpNameAffix($crossFK, $plural = true);
  3314. $relatedObjectClassName = $this->getNewStubObjectBuilder($crossFK->getForeignTable())->getClassname();
  3315. $selfRelationName = $this->getFKPhpNameAffix($refFK, $plural = false);
  3316. $relatedQueryClassName = $this->getNewStubQueryBuilder($crossFK->getForeignTable())->getClassname();
  3317. $crossRefTableName = $crossFK->getTableName();
  3318. $collName = $this->getCrossFKVarName($crossFK);
  3319. $script .= "
  3320. /**
  3321. * Gets a collection of $relatedObjectClassName objects related by a many-to-many relationship
  3322. * to the current object by way of the $crossRefTableName cross-reference table.
  3323. *
  3324. * If the \$criteria is not null, it is used to always fetch the results from the database.
  3325. * Otherwise the results are fetched from the database the first time, then cached.
  3326. * Next time the same method is called without \$criteria, the cached collection is returned.
  3327. * If this ".$this->getObjectClassname()." is new, it will return
  3328. * an empty collection or the current collection; the criteria is ignored on a new object.
  3329. *
  3330. * @param Criteria \$criteria Optional query object to filter the query
  3331. * @param PropelPDO \$con Optional connection object
  3332. *
  3333. * @return PropelCollection|array {$relatedObjectClassName}[] List of {$relatedObjectClassName} objects
  3334. */
  3335. public function get{$relatedName}(\$criteria = null, PropelPDO \$con = null)
  3336. {
  3337. if(null === \$this->$collName || null !== \$criteria) {
  3338. if (\$this->isNew() && null === \$this->$collName) {
  3339. // return empty collection
  3340. \$this->init{$relatedName}();
  3341. } else {
  3342. \$$collName = $relatedQueryClassName::create(null, \$criteria)
  3343. ->filterBy{$selfRelationName}(\$this)
  3344. ->find(\$con);
  3345. if (null !== \$criteria) {
  3346. return \$$collName;
  3347. }
  3348. \$this->$collName = \$$collName;
  3349. }
  3350. }
  3351. return \$this->$collName;
  3352. }
  3353. ";
  3354. }
  3355. protected function addCrossFKCount(&$script, $refFK, $crossFK)
  3356. {
  3357. $relatedName = $this->getFKPhpNameAffix($crossFK, $plural = true);
  3358. $relatedObjectClassName = $this->getNewStubObjectBuilder($crossFK->getForeignTable())->getClassname();
  3359. $selfRelationName = $this->getFKPhpNameAffix($refFK, $plural = false);
  3360. $relatedQueryClassName = $this->getNewStubQueryBuilder($crossFK->getForeignTable())->getClassname();
  3361. $crossRefTableName = $refFK->getTableName();
  3362. $collName = $this->getCrossFKVarName($crossFK);
  3363. $script .= "
  3364. /**
  3365. * Gets the number of $relatedObjectClassName objects related by a many-to-many relationship
  3366. * to the current object by way of the $crossRefTableName cross-reference table.
  3367. *
  3368. * @param Criteria \$criteria Optional query object to filter the query
  3369. * @param boolean \$distinct Set to true to force count distinct
  3370. * @param PropelPDO \$con Optional connection object
  3371. *
  3372. * @return int the number of related $relatedObjectClassName objects
  3373. */
  3374. public function count{$relatedName}(\$criteria = null, \$distinct = false, PropelPDO \$con = null)
  3375. {
  3376. if(null === \$this->$collName || null !== \$criteria) {
  3377. if (\$this->isNew() && null === \$this->$collName) {
  3378. return 0;
  3379. } else {
  3380. \$query = $relatedQueryClassName::create(null, \$criteria);
  3381. if(\$distinct) {
  3382. \$query->distinct();
  3383. }
  3384. return \$query
  3385. ->filterBy{$selfRelationName}(\$this)
  3386. ->count(\$con);
  3387. }
  3388. } else {
  3389. return count(\$this->$collName);
  3390. }
  3391. }
  3392. ";
  3393. }
  3394. /**
  3395. * Adds the method that adds an object into the referrer fkey collection.
  3396. * @param string &$script The script will be modified in this method.
  3397. */
  3398. protected function addCrossFKAdd(&$script, ForeignKey $refFK, ForeignKey $crossFK)
  3399. {
  3400. $relCol = $this->getFKPhpNameAffix($crossFK, $plural = true);
  3401. $collName = $this->getCrossFKVarName($crossFK);
  3402. $tblFK = $refFK->getTable();
  3403. $joinedTableObjectBuilder = $this->getNewObjectBuilder($refFK->getTable());
  3404. $className = $joinedTableObjectBuilder->getObjectClassname();
  3405. $foreignObjectName = '$' . $tblFK->getStudlyPhpName();
  3406. $crossObjectName = '$' . $crossFK->getForeignTable()->getStudlyPhpName();
  3407. $crossObjectClassName = $this->getNewObjectBuilder($crossFK->getForeignTable())->getObjectClassname();
  3408. $script .= "
  3409. /**
  3410. * Associate a " . $crossObjectClassName . " object to this object
  3411. * through the " . $tblFK->getName() . " cross reference table.
  3412. *
  3413. * @param " . $crossObjectClassName . " " . $crossObjectName . " The $className object to relate
  3414. * @return void
  3415. */
  3416. public function add" . $this->getFKPhpNameAffix($crossFK, $plural = false) . "(" . $crossObjectName. ")
  3417. {
  3418. if (\$this->" . $collName . " === null) {
  3419. \$this->init" . $relCol . "();
  3420. }
  3421. if (!\$this->" . $collName . "->contains(" . $crossObjectName . ")) { // only add it if the **same** object is not already associated
  3422. " . $foreignObjectName . " = new " . $className . "();
  3423. " . $foreignObjectName . "->set" . $this->getFKPhpNameAffix($crossFK, $plural = false) . "(" . $crossObjectName . ");
  3424. \$this->add" . $this->getRefFKPhpNameAffix($refFK, $plural = false) . "(" . $foreignObjectName . ");
  3425. \$this->" . $collName . "[]= " . $crossObjectName . ";
  3426. }
  3427. }
  3428. ";
  3429. }
  3430. // ----------------------------------------------------------------
  3431. //
  3432. // M A N I P U L A T I O N M E T H O D S
  3433. //
  3434. // ----------------------------------------------------------------
  3435. /**
  3436. * Adds the workhourse doSave() method.
  3437. * @param string &$script The script will be modified in this method.
  3438. */
  3439. protected function addDoSave(&$script)
  3440. {
  3441. $table = $this->getTable();
  3442. $reloadOnUpdate = $table->isReloadOnUpdate();
  3443. $reloadOnInsert = $table->isReloadOnInsert();
  3444. $script .= "
  3445. /**
  3446. * Performs the work of inserting or updating the row in the database.
  3447. *
  3448. * If the object is new, it inserts it; otherwise an update is performed.
  3449. * All related objects are also updated in this method.
  3450. *
  3451. * @param PropelPDO \$con";
  3452. if ($reloadOnUpdate || $reloadOnInsert) {
  3453. $script .= "
  3454. * @param boolean \$skipReload Whether to skip the reload for this object from database.";
  3455. }
  3456. $script .= "
  3457. * @return int The number of rows affected by this insert/update and any referring fk objects' save() operations.
  3458. * @throws PropelException
  3459. * @see save()
  3460. */
  3461. protected function doSave(PropelPDO \$con".($reloadOnUpdate || $reloadOnInsert ? ", \$skipReload = false" : "").")
  3462. {
  3463. \$affectedRows = 0; // initialize var to track total num of affected rows
  3464. if (!\$this->alreadyInSave) {
  3465. \$this->alreadyInSave = true;
  3466. ";
  3467. if ($reloadOnInsert || $reloadOnUpdate) {
  3468. $script .= "
  3469. \$reloadObject = false;
  3470. ";
  3471. }
  3472. if (count($table->getForeignKeys())) {
  3473. $script .= "
  3474. // We call the save method on the following object(s) if they
  3475. // were passed to this object by their coresponding set
  3476. // method. This object relates to these object(s) by a
  3477. // foreign key reference.
  3478. ";
  3479. foreach ($table->getForeignKeys() as $fk) {
  3480. $aVarName = $this->getFKVarName($fk);
  3481. $script .= "
  3482. if (\$this->$aVarName !== null) {
  3483. if (\$this->" . $aVarName . "->isModified() || \$this->" . $aVarName . "->isNew()) {
  3484. \$affectedRows += \$this->" . $aVarName . "->save(\$con);
  3485. }
  3486. \$this->set".$this->getFKPhpNameAffix($fk, $plural = false)."(\$this->$aVarName);
  3487. }
  3488. ";
  3489. } // foreach foreign k
  3490. } // if (count(foreign keys))
  3491. if ($table->hasAutoIncrementPrimaryKey() ) {
  3492. $script .= "
  3493. if (\$this->isNew() ) {
  3494. \$this->modifiedColumns[] = " . $this->getColumnConstant($table->getAutoIncrementPrimaryKey() ) . ";
  3495. }";
  3496. }
  3497. $script .= "
  3498. // If this object has been modified, then save it to the database.
  3499. if (\$this->isModified()) {
  3500. if (\$this->isNew()) {
  3501. \$criteria = \$this->buildCriteria();";
  3502. foreach ($table->getColumns() as $col) {
  3503. if ($col->isPrimaryKey() && $col->isAutoIncrement() && $table->getIdMethod() != "none" && !$table->isAllowPkInsert()) {
  3504. $colConst = $this->getColumnConstant($col);
  3505. $script .= "
  3506. if (\$criteria->keyContainsValue(" . $colConst . ") ) {
  3507. throw new PropelException('Cannot insert a value for auto-increment primary key ('." . $colConst . ".')');
  3508. }
  3509. ";
  3510. if (!$this->getPlatform()->supportsInsertNullPk()) {
  3511. $script .= "
  3512. // remove pkey col since this table uses auto-increment and passing a null value for it is not valid
  3513. \$criteria->remove(" . $colConst . ");
  3514. ";
  3515. }
  3516. } elseif ($col->isPrimaryKey() && $col->isAutoIncrement() && $table->getIdMethod() != "none" && $table->isAllowPkInsert() && !$this->getPlatform()->supportsInsertNullPk()) {
  3517. $script .= "
  3518. // remove pkey col if it is null since this table does not accept that
  3519. if (\$criteria->containsKey(" . $colConst . ") && !\$criteria->keyContainsValue(" . $colConst . ") ) {
  3520. \$criteria->remove(" . $colConst . ");
  3521. }";
  3522. }
  3523. }
  3524. $script .= "
  3525. \$pk = " . $this->getNewPeerBuilder($table)->getBasePeerClassname() . "::doInsert(\$criteria, \$con);";
  3526. if ($reloadOnInsert) {
  3527. $script .= "
  3528. if (!\$skipReload) {
  3529. \$reloadObject = true;
  3530. }";
  3531. }
  3532. $operator = count($table->getForeignKeys()) ? '+=' : '=';
  3533. $script .= "
  3534. \$affectedRows " . $operator . " 1;";
  3535. if ($table->getIdMethod() != IDMethod::NO_ID_METHOD) {
  3536. if (count($pks = $table->getPrimaryKey())) {
  3537. foreach ($pks as $pk) {
  3538. if ($pk->isAutoIncrement()) {
  3539. if ($table->isAllowPkInsert()) {
  3540. $script .= "
  3541. if (\$pk !== null) {
  3542. \$this->set".$pk->getPhpName()."(\$pk); //[IMV] update autoincrement primary key
  3543. }";
  3544. } else {
  3545. $script .= "
  3546. \$this->set".$pk->getPhpName()."(\$pk); //[IMV] update autoincrement primary key";
  3547. }
  3548. }
  3549. }
  3550. }
  3551. } // if (id method != "none")
  3552. $script .= "
  3553. \$this->setNew(false);
  3554. } else {";
  3555. if ($reloadOnUpdate) {
  3556. $script .= "
  3557. if (!\$skipReload) {
  3558. \$reloadObject = true;
  3559. }";
  3560. }
  3561. $operator = count($table->getForeignKeys()) ? '+=' : '=';
  3562. $script .= "
  3563. \$affectedRows " . $operator . " ".$this->getPeerClassname()."::doUpdate(\$this, \$con);
  3564. }
  3565. ";
  3566. // We need to rewind any LOB columns
  3567. foreach ($table->getColumns() as $col) {
  3568. $clo = strtolower($col->getName());
  3569. if ($col->isLobType()) {
  3570. $script .= "
  3571. // Rewind the $clo LOB column, since PDO does not rewind after inserting value.
  3572. if (\$this->$clo !== null && is_resource(\$this->$clo)) {
  3573. rewind(\$this->$clo);
  3574. }
  3575. ";
  3576. }
  3577. }
  3578. $script .= "
  3579. \$this->resetModified(); // [HL] After being saved an object is no longer 'modified'
  3580. }
  3581. ";
  3582. foreach ($table->getReferrers() as $refFK) {
  3583. if ($refFK->isLocalPrimaryKey()) {
  3584. $varName = $this->getPKRefFKVarName($refFK);
  3585. $script .= "
  3586. if (\$this->$varName !== null) {
  3587. if (!\$this->{$varName}->isDeleted()) {
  3588. \$affectedRows += \$this->{$varName}->save(\$con);
  3589. }
  3590. }
  3591. ";
  3592. } else {
  3593. $collName = $this->getRefFKCollVarName($refFK);
  3594. $script .= "
  3595. if (\$this->$collName !== null) {
  3596. foreach (\$this->$collName as \$referrerFK) {
  3597. if (!\$referrerFK->isDeleted()) {
  3598. \$affectedRows += \$referrerFK->save(\$con);
  3599. }
  3600. }
  3601. }
  3602. ";
  3603. } // if refFK->isLocalPrimaryKey()
  3604. } /* foreach getReferrers() */
  3605. $script .= "
  3606. \$this->alreadyInSave = false;
  3607. ";
  3608. if ($reloadOnInsert || $reloadOnUpdate) {
  3609. $script .= "
  3610. if (\$reloadObject) {
  3611. \$this->reload(\$con);
  3612. }
  3613. ";
  3614. }
  3615. $script .= "
  3616. }
  3617. return \$affectedRows;
  3618. } // doSave()
  3619. ";
  3620. }
  3621. /**
  3622. * Adds the $alreadyInSave attribute, which prevents attempting to re-save the same object.
  3623. * @param string &$script The script will be modified in this method.
  3624. */
  3625. protected function addAlreadyInSaveAttribute(&$script)
  3626. {
  3627. $script .= "
  3628. /**
  3629. * Flag to prevent endless save loop, if this object is referenced
  3630. * by another object which falls in this transaction.
  3631. * @var boolean
  3632. */
  3633. protected \$alreadyInSave = false;
  3634. ";
  3635. }
  3636. /**
  3637. * Adds the save() method.
  3638. * @param string &$script The script will be modified in this method.
  3639. */
  3640. protected function addSave(&$script)
  3641. {
  3642. $this->addSaveComment($script);
  3643. $this->addSaveOpen($script);
  3644. $this->addSaveBody($script);
  3645. $this->addSaveClose($script);
  3646. }
  3647. /**
  3648. * Adds the comment for the save method
  3649. * @param string &$script The script will be modified in this method.
  3650. * @see addSave()
  3651. **/
  3652. protected function addSaveComment(&$script) {
  3653. $table = $this->getTable();
  3654. $reloadOnUpdate = $table->isReloadOnUpdate();
  3655. $reloadOnInsert = $table->isReloadOnInsert();
  3656. $script .= "
  3657. /**
  3658. * Persists this object to the database.
  3659. *
  3660. * If the object is new, it inserts it; otherwise an update is performed.
  3661. * All modified related objects will also be persisted in the doSave()
  3662. * method. This method wraps all precipitate database operations in a
  3663. * single transaction.";
  3664. if ($reloadOnUpdate) {
  3665. $script .= "
  3666. *
  3667. * Since this table was configured to reload rows on update, the object will
  3668. * be reloaded from the database if an UPDATE operation is performed (unless
  3669. * the \$skipReload parameter is TRUE).";
  3670. }
  3671. if ($reloadOnInsert) {
  3672. $script .= "
  3673. *
  3674. * Since this table was configured to reload rows on insert, the object will
  3675. * be reloaded from the database if an INSERT operation is performed (unless
  3676. * the \$skipReload parameter is TRUE).";
  3677. }
  3678. $script .= "
  3679. *
  3680. * @param PropelPDO \$con";
  3681. if ($reloadOnUpdate || $reloadOnInsert) {
  3682. $script .= "
  3683. * @param boolean \$skipReload Whether to skip the reload for this object from database.";
  3684. }
  3685. $script .= "
  3686. * @return int The number of rows affected by this insert/update and any referring fk objects' save() operations.
  3687. * @throws PropelException
  3688. * @see doSave()
  3689. */";
  3690. }
  3691. /**
  3692. * Adds the function declaration for the save method
  3693. * @param string &$script The script will be modified in this method.
  3694. * @see addSave()
  3695. **/
  3696. protected function addSaveOpen(&$script) {
  3697. $table = $this->getTable();
  3698. $reloadOnUpdate = $table->isReloadOnUpdate();
  3699. $reloadOnInsert = $table->isReloadOnInsert();
  3700. $script .= "
  3701. public function save(PropelPDO \$con = null".($reloadOnUpdate || $reloadOnInsert ? ", \$skipReload = false" : "").")
  3702. {";
  3703. }
  3704. /**
  3705. * Adds the function body for the save method
  3706. * @param string &$script The script will be modified in this method.
  3707. * @see addSave()
  3708. **/
  3709. protected function addSaveBody(&$script) {
  3710. $table = $this->getTable();
  3711. $reloadOnUpdate = $table->isReloadOnUpdate();
  3712. $reloadOnInsert = $table->isReloadOnInsert();
  3713. $script .= "
  3714. if (\$this->isDeleted()) {
  3715. throw new PropelException(\"You cannot save an object that has been deleted.\");
  3716. }
  3717. if (\$con === null) {
  3718. \$con = Propel::getConnection(".$this->getPeerClassname()."::DATABASE_NAME, Propel::CONNECTION_WRITE);
  3719. }
  3720. \$con->beginTransaction();
  3721. \$isInsert = \$this->isNew();
  3722. try {";
  3723. if($this->getGeneratorConfig()->getBuildProperty('addHooks')) {
  3724. // save with runtime hools
  3725. $script .= "
  3726. \$ret = \$this->preSave(\$con);";
  3727. $this->applyBehaviorModifier('preSave', $script, " ");
  3728. $script .= "
  3729. if (\$isInsert) {
  3730. \$ret = \$ret && \$this->preInsert(\$con);";
  3731. $this->applyBehaviorModifier('preInsert', $script, " ");
  3732. $script .= "
  3733. } else {
  3734. \$ret = \$ret && \$this->preUpdate(\$con);";
  3735. $this->applyBehaviorModifier('preUpdate', $script, " ");
  3736. $script .= "
  3737. }
  3738. if (\$ret) {
  3739. \$affectedRows = \$this->doSave(\$con".($reloadOnUpdate || $reloadOnInsert ? ", \$skipReload" : "").");
  3740. if (\$isInsert) {
  3741. \$this->postInsert(\$con);";
  3742. $this->applyBehaviorModifier('postInsert', $script, " ");
  3743. $script .= "
  3744. } else {
  3745. \$this->postUpdate(\$con);";
  3746. $this->applyBehaviorModifier('postUpdate', $script, " ");
  3747. $script .= "
  3748. }
  3749. \$this->postSave(\$con);";
  3750. $this->applyBehaviorModifier('postSave', $script, " ");
  3751. $script .= "
  3752. ".$this->getPeerClassname()."::addInstanceToPool(\$this);
  3753. } else {
  3754. \$affectedRows = 0;
  3755. }
  3756. \$con->commit();
  3757. return \$affectedRows;";
  3758. } else {
  3759. // save without runtime hooks
  3760. $this->applyBehaviorModifier('preSave', $script, " ");
  3761. if ($this->hasBehaviorModifier('preUpdate'))
  3762. {
  3763. $script .= "
  3764. if(!\$isInsert) {";
  3765. $this->applyBehaviorModifier('preUpdate', $script, " ");
  3766. $script .= "
  3767. }";
  3768. }
  3769. if ($this->hasBehaviorModifier('preInsert'))
  3770. {
  3771. $script .= "
  3772. if(\$isInsert) {";
  3773. $this->applyBehaviorModifier('preInsert', $script, " ");
  3774. $script .= "
  3775. }";
  3776. }
  3777. $script .= "
  3778. \$affectedRows = \$this->doSave(\$con".($reloadOnUpdate || $reloadOnInsert ? ", \$skipReload" : "").");";
  3779. $this->applyBehaviorModifier('postSave', $script, " ");
  3780. if ($this->hasBehaviorModifier('postUpdate'))
  3781. {
  3782. $script .= "
  3783. if(!\$isInsert) {";
  3784. $this->applyBehaviorModifier('postUpdate', $script, " ");
  3785. $script .= "
  3786. }";
  3787. }
  3788. if ($this->hasBehaviorModifier('postInsert'))
  3789. {
  3790. $script .= "
  3791. if(\$isInsert) {";
  3792. $this->applyBehaviorModifier('postInsert', $script, " ");
  3793. $script .= "
  3794. }";
  3795. }
  3796. $script .= "
  3797. \$con->commit();
  3798. ".$this->getPeerClassname()."::addInstanceToPool(\$this);
  3799. return \$affectedRows;";
  3800. }
  3801. $script .= "
  3802. } catch (PropelException \$e) {
  3803. \$con->rollBack();
  3804. throw \$e;
  3805. }";
  3806. }
  3807. /**
  3808. * Adds the function close for the save method
  3809. * @param string &$script The script will be modified in this method.
  3810. * @see addSave()
  3811. **/
  3812. protected function addSaveClose(&$script) {
  3813. $script .= "
  3814. }
  3815. ";
  3816. }
  3817. /**
  3818. * Adds the $alreadyInValidation attribute, which prevents attempting to re-validate the same object.
  3819. * @param string &$script The script will be modified in this method.
  3820. */
  3821. protected function addAlreadyInValidationAttribute(&$script)
  3822. {
  3823. $script .= "
  3824. /**
  3825. * Flag to prevent endless validation loop, if this object is referenced
  3826. * by another object which falls in this transaction.
  3827. * @var boolean
  3828. */
  3829. protected \$alreadyInValidation = false;
  3830. ";
  3831. }
  3832. /**
  3833. * Adds the validate() method.
  3834. * @param string &$script The script will be modified in this method.
  3835. */
  3836. protected function addValidate(&$script)
  3837. {
  3838. $script .= "
  3839. /**
  3840. * Validates the objects modified field values and all objects related to this table.
  3841. *
  3842. * If \$columns is either a column name or an array of column names
  3843. * only those columns are validated.
  3844. *
  3845. * @param mixed \$columns Column name or an array of column names.
  3846. * @return boolean Whether all columns pass validation.
  3847. * @see doValidate()
  3848. * @see getValidationFailures()
  3849. */
  3850. public function validate(\$columns = null)
  3851. {
  3852. \$res = \$this->doValidate(\$columns);
  3853. if (\$res === true) {
  3854. \$this->validationFailures = array();
  3855. return true;
  3856. } else {
  3857. \$this->validationFailures = \$res;
  3858. return false;
  3859. }
  3860. }
  3861. ";
  3862. } // addValidate()
  3863. /**
  3864. * Adds the workhourse doValidate() method.
  3865. * @param string &$script The script will be modified in this method.
  3866. */
  3867. protected function addDoValidate(&$script)
  3868. {
  3869. $table = $this->getTable();
  3870. $script .= "
  3871. /**
  3872. * This function performs the validation work for complex object models.
  3873. *
  3874. * In addition to checking the current object, all related objects will
  3875. * also be validated. If all pass then <code>true</code> is returned; otherwise
  3876. * an aggreagated array of ValidationFailed objects will be returned.
  3877. *
  3878. * @param array \$columns Array of column names to validate.
  3879. * @return mixed <code>true</code> if all validations pass; array of <code>ValidationFailed</code> objets otherwise.
  3880. */
  3881. protected function doValidate(\$columns = null)
  3882. {
  3883. if (!\$this->alreadyInValidation) {
  3884. \$this->alreadyInValidation = true;
  3885. \$retval = null;
  3886. \$failureMap = array();
  3887. ";
  3888. if (count($table->getForeignKeys()) != 0) {
  3889. $script .= "
  3890. // We call the validate method on the following object(s) if they
  3891. // were passed to this object by their coresponding set
  3892. // method. This object relates to these object(s) by a
  3893. // foreign key reference.
  3894. ";
  3895. foreach ($table->getForeignKeys() as $fk) {
  3896. $aVarName = $this->getFKVarName($fk);
  3897. $script .= "
  3898. if (\$this->".$aVarName." !== null) {
  3899. if (!\$this->".$aVarName."->validate(\$columns)) {
  3900. \$failureMap = array_merge(\$failureMap, \$this->".$aVarName."->getValidationFailures());
  3901. }
  3902. }
  3903. ";
  3904. } /* for () */
  3905. } /* if count(fkeys) */
  3906. $script .= "
  3907. if ((\$retval = ".$this->getPeerClassname()."::doValidate(\$this, \$columns)) !== true) {
  3908. \$failureMap = array_merge(\$failureMap, \$retval);
  3909. }
  3910. ";
  3911. foreach ($table->getReferrers() as $refFK) {
  3912. if ($refFK->isLocalPrimaryKey()) {
  3913. $varName = $this->getPKRefFKVarName($refFK);
  3914. $script .= "
  3915. if (\$this->$varName !== null) {
  3916. if (!\$this->".$varName."->validate(\$columns)) {
  3917. \$failureMap = array_merge(\$failureMap, \$this->".$varName."->getValidationFailures());
  3918. }
  3919. }
  3920. ";
  3921. } else {
  3922. $collName = $this->getRefFKCollVarName($refFK);
  3923. $script .= "
  3924. if (\$this->$collName !== null) {
  3925. foreach (\$this->$collName as \$referrerFK) {
  3926. if (!\$referrerFK->validate(\$columns)) {
  3927. \$failureMap = array_merge(\$failureMap, \$referrerFK->getValidationFailures());
  3928. }
  3929. }
  3930. }
  3931. ";
  3932. }
  3933. } /* foreach getReferrers() */
  3934. $script .= "
  3935. \$this->alreadyInValidation = false;
  3936. }
  3937. return (!empty(\$failureMap) ? \$failureMap : true);
  3938. }
  3939. ";
  3940. } // addDoValidate()
  3941. /**
  3942. * Adds the ensureConsistency() method to ensure that internal state is correct.
  3943. * @param string &$script The script will be modified in this method.
  3944. */
  3945. protected function addEnsureConsistency(&$script)
  3946. {
  3947. $table = $this->getTable();
  3948. $script .= "
  3949. /**
  3950. * Checks and repairs the internal consistency of the object.
  3951. *
  3952. * This method is executed after an already-instantiated object is re-hydrated
  3953. * from the database. It exists to check any foreign keys to make sure that
  3954. * the objects related to the current object are correct based on foreign key.
  3955. *
  3956. * You can override this method in the stub class, but you should always invoke
  3957. * the base method from the overridden method (i.e. parent::ensureConsistency()),
  3958. * in case your model changes.
  3959. *
  3960. * @throws PropelException
  3961. */
  3962. public function ensureConsistency()
  3963. {
  3964. ";
  3965. foreach ($table->getColumns() as $col) {
  3966. $clo=strtolower($col->getName());
  3967. if ($col->isForeignKey()) {
  3968. foreach ($col->getForeignKeys() as $fk) {
  3969. $tblFK = $table->getDatabase()->getTable($fk->getForeignTableName());
  3970. $colFK = $tblFK->getColumn($fk->getMappedForeignColumn($col->getName()));
  3971. $varName = $this->getFKVarName($fk);
  3972. $script .= "
  3973. if (\$this->".$varName." !== null && \$this->$clo !== \$this->".$varName."->get".$colFK->getPhpName()."()) {
  3974. \$this->$varName = null;
  3975. }";
  3976. } // foraech
  3977. } /* if col is foreign key */
  3978. } // foreach
  3979. $script .= "
  3980. } // ensureConsistency
  3981. ";
  3982. } // addCheckRelConsistency
  3983. /**
  3984. * Adds the copy() method, which (in complex OM) includes the $deepCopy param for making copies of related objects.
  3985. * @param string &$script The script will be modified in this method.
  3986. */
  3987. protected function addCopy(&$script)
  3988. {
  3989. $this->addCopyInto($script);
  3990. $table = $this->getTable();
  3991. $script .= "
  3992. /**
  3993. * Makes a copy of this object that will be inserted as a new row in table when saved.
  3994. * It creates a new object filling in the simple attributes, but skipping any primary
  3995. * keys that are defined for the table.
  3996. *
  3997. * If desired, this method can also make copies of all associated (fkey referrers)
  3998. * objects.
  3999. *
  4000. * @param boolean \$deepCopy Whether to also copy all rows that refer (by fkey) to the current row.
  4001. * @return ".$this->getObjectClassname()." Clone of current object.
  4002. * @throws PropelException
  4003. */
  4004. public function copy(\$deepCopy = false)
  4005. {
  4006. // we use get_class(), because this might be a subclass
  4007. \$clazz = get_class(\$this);
  4008. " . $this->buildObjectInstanceCreationCode('$copyObj', '$clazz') . "
  4009. \$this->copyInto(\$copyObj, \$deepCopy);
  4010. return \$copyObj;
  4011. }
  4012. ";
  4013. } // addCopy()
  4014. /**
  4015. * Adds the copyInto() method, which takes an object and sets contents to match current object.
  4016. * In complex OM this method includes the $deepCopy param for making copies of related objects.
  4017. * @param string &$script The script will be modified in this method.
  4018. */
  4019. protected function addCopyInto(&$script)
  4020. {
  4021. $table = $this->getTable();
  4022. $script .= "
  4023. /**
  4024. * Sets contents of passed object to values from current object.
  4025. *
  4026. * If desired, this method can also make copies of all associated (fkey referrers)
  4027. * objects.
  4028. *
  4029. * @param object \$copyObj An object of ".$this->getObjectClassname()." (or compatible) type.
  4030. * @param boolean \$deepCopy Whether to also copy all rows that refer (by fkey) to the current row.
  4031. * @param boolean \$makeNew Whether to reset autoincrement PKs and make the object new.
  4032. * @throws PropelException
  4033. */
  4034. public function copyInto(\$copyObj, \$deepCopy = false, \$makeNew = true)
  4035. {";
  4036. $autoIncCols = array();
  4037. foreach ($table->getColumns() as $col) {
  4038. /* @var $col Column */
  4039. if ($col->isAutoIncrement()) {
  4040. $autoIncCols[] = $col;
  4041. }
  4042. }
  4043. foreach ($table->getColumns() as $col) {
  4044. if (!in_array($col, $autoIncCols, true)) {
  4045. $script .= "
  4046. \$copyObj->set".$col->getPhpName()."(\$this->get".$col->getPhpName()."());";
  4047. }
  4048. } // foreach
  4049. // Avoid useless code by checking to see if there are any referrers
  4050. // to this table:
  4051. if (count($table->getReferrers()) > 0) {
  4052. $script .= "
  4053. if (\$deepCopy) {
  4054. // important: temporarily setNew(false) because this affects the behavior of
  4055. // the getter/setter methods for fkey referrer objects.
  4056. \$copyObj->setNew(false);
  4057. ";
  4058. foreach ($table->getReferrers() as $fk) {
  4059. //HL: commenting out self-referrential check below
  4060. // it seems to work as expected and is probably desireable to have those referrers from same table deep-copied.
  4061. //if ( $fk->getTable()->getName() != $table->getName() ) {
  4062. if ($fk->isLocalPrimaryKey()) {
  4063. $afx = $this->getRefFKPhpNameAffix($fk, $plural = false);
  4064. $script .= "
  4065. \$relObj = \$this->get$afx();
  4066. if (\$relObj) {
  4067. \$copyObj->set$afx(\$relObj->copy(\$deepCopy));
  4068. }
  4069. ";
  4070. } else {
  4071. $script .= "
  4072. foreach (\$this->get".$this->getRefFKPhpNameAffix($fk, true)."() as \$relObj) {
  4073. if (\$relObj !== \$this) { // ensure that we don't try to copy a reference to ourselves
  4074. \$copyObj->add".$this->getRefFKPhpNameAffix($fk)."(\$relObj->copy(\$deepCopy));
  4075. }
  4076. }
  4077. ";
  4078. }
  4079. // HL: commenting out close of self-referential check
  4080. // } /* if tblFK != table */
  4081. } /* foreach */
  4082. $script .= "
  4083. } // if (\$deepCopy)
  4084. ";
  4085. } /* if (count referrers > 0 ) */
  4086. $script .= "
  4087. if (\$makeNew) {
  4088. \$copyObj->setNew(true);";
  4089. // Note: we're no longer resetting non-autoincrement primary keys to default values
  4090. // due to: http://propel.phpdb.org/trac/ticket/618
  4091. foreach ($autoIncCols as $col) {
  4092. $coldefval = $col->getPhpDefaultValue();
  4093. $coldefval = var_export($coldefval, true);
  4094. $script .= "
  4095. \$copyObj->set".$col->getPhpName() ."($coldefval); // this is a auto-increment column, so set to default value";
  4096. } // foreach
  4097. $script .= "
  4098. }
  4099. }
  4100. ";
  4101. } // addCopyInto()
  4102. /**
  4103. * Adds clear method
  4104. * @param string &$script The script will be modified in this method.
  4105. */
  4106. protected function addClear(&$script)
  4107. {
  4108. $table = $this->getTable();
  4109. $script .= "
  4110. /**
  4111. * Clears the current object and sets all attributes to their default values
  4112. */
  4113. public function clear()
  4114. {";
  4115. foreach ($table->getColumns() as $col) {
  4116. $clo = strtolower($col->getName());
  4117. $script .= "
  4118. \$this->".$clo." = null;";
  4119. if($col->isLazyLoad()){
  4120. $script .= "
  4121. \$this->".$clo."_isLoaded = false;";
  4122. }
  4123. }
  4124. $script .= "
  4125. \$this->alreadyInSave = false;
  4126. \$this->alreadyInValidation = false;
  4127. \$this->clearAllReferences();";
  4128. if ($this->hasDefaultValues()) {
  4129. $script .= "
  4130. \$this->applyDefaultValues();";
  4131. }
  4132. $script .= "
  4133. \$this->resetModified();
  4134. \$this->setNew(true);
  4135. \$this->setDeleted(false);
  4136. }
  4137. ";
  4138. }
  4139. /**
  4140. * Adds clearAllReferencers() method which resets all the collections of referencing
  4141. * fk objects.
  4142. * @param string &$script The script will be modified in this method.
  4143. */
  4144. protected function addClearAllReferences(&$script)
  4145. {
  4146. $table = $this->getTable();
  4147. $script .= "
  4148. /**
  4149. * Resets all references to other model objects or collections of model objects.
  4150. *
  4151. * This method is a user-space workaround for PHP's inability to garbage collect
  4152. * objects with circular references (even in PHP 5.3). This is currently necessary
  4153. * when using Propel in certain daemon or large-volumne/high-memory operations.
  4154. *
  4155. * @param boolean \$deep Whether to also clear the references on all referrer objects.
  4156. */
  4157. public function clearAllReferences(\$deep = false)
  4158. {
  4159. if (\$deep) {";
  4160. $vars = array();
  4161. foreach ($this->getTable()->getReferrers() as $refFK) {
  4162. if ($refFK->isLocalPrimaryKey()) {
  4163. $varName = $this->getPKRefFKVarName($refFK);
  4164. $script .= "
  4165. if (\$this->$varName) {
  4166. \$this->{$varName}->clearAllReferences(\$deep);
  4167. }";
  4168. } else {
  4169. $varName = $this->getRefFKCollVarName($refFK);
  4170. $script .= "
  4171. if (\$this->$varName) {
  4172. foreach (\$this->$varName as \$o) {
  4173. \$o->clearAllReferences(\$deep);
  4174. }
  4175. }";
  4176. }
  4177. $vars[] = $varName;
  4178. }
  4179. foreach ($this->getTable()->getCrossFks() as $fkList) {
  4180. list($refFK, $crossFK) = $fkList;
  4181. $varName = $this->getCrossFKVarName($crossFK);
  4182. $script .= "
  4183. if (\$this->$varName) {
  4184. foreach (\$this->$varName as \$o) {
  4185. \$o->clearAllReferences(\$deep);
  4186. }
  4187. }";
  4188. $vars[] = $varName;
  4189. }
  4190. $script .= "
  4191. } // if (\$deep)
  4192. ";
  4193. $this->applyBehaviorModifier('objectClearReferences', $script, " ");
  4194. foreach ($vars as $varName) {
  4195. $script .= "
  4196. if (\$this->$varName instanceof PropelCollection) {
  4197. \$this->{$varName}->clearIterator();
  4198. }
  4199. \$this->$varName = null;";
  4200. }
  4201. foreach ($table->getForeignKeys() as $fk) {
  4202. $varName = $this->getFKVarName($fk);
  4203. $script .= "
  4204. \$this->$varName = null;";
  4205. }
  4206. $script .= "
  4207. }
  4208. ";
  4209. }
  4210. /**
  4211. * Adds a magic __toString() method if a string column was defined as primary string
  4212. * @param string &$script The script will be modified in this method.
  4213. */
  4214. protected function addPrimaryString(&$script)
  4215. {
  4216. foreach ($this->getTable()->getColumns() as $column) {
  4217. if ($column->isPrimaryString()) {
  4218. $script .= "
  4219. /**
  4220. * Return the string representation of this object
  4221. *
  4222. * @return string The value of the '{$column->getName()}' column
  4223. */
  4224. public function __toString()
  4225. {
  4226. return (string) \$this->get{$column->getPhpName()}();
  4227. }
  4228. ";
  4229. return;
  4230. }
  4231. }
  4232. // no primary string column, falling back to default string format
  4233. $script .= "
  4234. /**
  4235. * Return the string representation of this object
  4236. *
  4237. * @return string
  4238. */
  4239. public function __toString()
  4240. {
  4241. return (string) \$this->exportTo(" . $this->getPeerClassname() . "::DEFAULT_STRING_FORMAT);
  4242. }
  4243. ";
  4244. }
  4245. /**
  4246. * Adds a magic __call() method
  4247. * @param string &$script The script will be modified in this method.
  4248. */
  4249. protected function addMagicCall(&$script)
  4250. {
  4251. $script .= "
  4252. /**
  4253. * Catches calls to virtual methods
  4254. */
  4255. public function __call(\$name, \$params)
  4256. {";
  4257. $this->applyBehaviorModifier('objectCall', $script, " ");
  4258. $script .= "
  4259. if (preg_match('/get(\w+)/', \$name, \$matches)) {
  4260. \$virtualColumn = \$matches[1];
  4261. if (\$this->hasVirtualColumn(\$virtualColumn)) {
  4262. return \$this->getVirtualColumn(\$virtualColumn);
  4263. }
  4264. // no lcfirst in php<5.3...
  4265. \$virtualColumn[0] = strtolower(\$virtualColumn[0]);
  4266. if (\$this->hasVirtualColumn(\$virtualColumn)) {
  4267. return \$this->getVirtualColumn(\$virtualColumn);
  4268. }
  4269. }
  4270. return parent::__call(\$name, \$params);
  4271. }
  4272. ";
  4273. }
  4274. } // PHP5ObjectBuilder