PageRenderTime 72ms CodeModel.GetById 29ms RepoModel.GetById 1ms 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

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

  1. <?php
  2. /**
  3. * This file is part of the Propel package.
  4. * For the full copyright and license information, please view the LICENSE
  5. * file that was distributed with this source code.
  6. *
  7. * @license MIT License
  8. */
  9. require_once 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 …

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