PageRenderTime 64ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 1ms

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

https://github.com/Tactics/Propel
PHP | 5578 lines | 3397 code | 532 blank | 1649 comment | 400 complexity | 171963c55c7556b4ee39bf6d3083bade 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 default key type. if not presented in configuration default will be 'TYPE_PHPNAME'
  41. * @return string
  42. */
  43. public function getDefaultKeyType()
  44. {
  45. $defaultKeyType = $this->getBuildProperty('defaultKeyType') ? $this->getBuildProperty('defaultKeyType') : 'phpName';
  46. return "TYPE_".strtoupper($defaultKeyType);
  47. }
  48. /**
  49. * Returns the name of the current class being built.
  50. * @return string
  51. */
  52. public function getUnprefixedClassname()
  53. {
  54. return $this->getBuildProperty('basePrefix') . $this->getStubObjectBuilder()->getUnprefixedClassname();
  55. }
  56. /**
  57. * Validates the current table to make sure that it won't
  58. * result in generated code that will not parse.
  59. *
  60. * This method may emit warnings for code which may cause problems
  61. * and will throw exceptions for errors that will definitely cause
  62. * problems.
  63. */
  64. protected function validateModel()
  65. {
  66. parent::validateModel();
  67. $table = $this->getTable();
  68. // Check to see whether any generated foreign key names
  69. // will conflict with column names.
  70. $colPhpNames = array();
  71. $fkPhpNames = array();
  72. foreach ($table->getColumns() as $col) {
  73. $colPhpNames[] = $col->getPhpName();
  74. }
  75. foreach ($table->getForeignKeys() as $fk) {
  76. $fkPhpNames[] = $this->getFKPhpNameAffix($fk, $plural = false);
  77. }
  78. $intersect = array_intersect($colPhpNames, $fkPhpNames);
  79. if (!empty($intersect)) {
  80. throw new EngineException("One or more of your column names for [" . $table->getName() . "] table conflict with foreign key names (" . implode(", ", $intersect) . ")");
  81. }
  82. // Check foreign keys to see if there are any foreign keys that
  83. // are also matched with an inversed referencing foreign key
  84. // (this is currently unsupported behavior)
  85. // see: http://propel.phpdb.org/trac/ticket/549
  86. foreach ($table->getForeignKeys() as $fk) {
  87. if ($fk->isMatchedByInverseFK()) {
  88. 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.)" );
  89. }
  90. }
  91. }
  92. /**
  93. * Returns the appropriate formatter (from platform) for a date/time column.
  94. * @param Column $col
  95. * @return string
  96. */
  97. protected function getTemporalFormatter(Column $col)
  98. {
  99. $fmt = null;
  100. if ($col->getType() === PropelTypes::DATE) {
  101. $fmt = $this->getPlatform()->getDateFormatter();
  102. } elseif ($col->getType() === PropelTypes::TIME) {
  103. $fmt = $this->getPlatform()->getTimeFormatter();
  104. } elseif ($col->getType() === PropelTypes::TIMESTAMP) {
  105. $fmt = $this->getPlatform()->getTimestampFormatter();
  106. }
  107. return $fmt;
  108. }
  109. /**
  110. * Returns the type-casted and stringified default value for the specified Column.
  111. * This only works for scalar default values currently.
  112. * @return string The default value or 'null' if there is none.
  113. * @throws EngineException
  114. */
  115. protected function getDefaultValueString(Column $col)
  116. {
  117. $defaultValue = var_export(null, true);
  118. $val = $col->getPhpDefaultValue();
  119. if ($val === null) {
  120. return $defaultValue;
  121. }
  122. if ($col->isTemporalType()) {
  123. $fmt = $this->getTemporalFormatter($col);
  124. try {
  125. if (!($this->getPlatform() instanceof MysqlPlatform &&
  126. ($val === '0000-00-00 00:00:00' || $val === '0000-00-00'))) {
  127. // while technically this is not a default value of null,
  128. // this seems to be closest in meaning.
  129. $defDt = new DateTime($val);
  130. $defaultValue = var_export($defDt->format($fmt), true);
  131. }
  132. } catch (Exception $x) {
  133. // prevent endless loop when timezone is undefined
  134. date_default_timezone_set('America/Los_Angeles');
  135. throw new EngineException(sprintf('Unable to parse default temporal value "%s" for column "%s"', $col->getDefaultValueString(), $col->getFullyQualifiedName()), $x);
  136. }
  137. } elseif ($col->isEnumType()) {
  138. $valueSet = $col->getValueSet();
  139. if (!in_array($val, $valueSet)) {
  140. throw new EngineException(sprintf('Default Value "%s" is not among the enumerated values', $val));
  141. }
  142. $defaultValue = array_search($val, $valueSet);
  143. } elseif ($col->isPhpPrimitiveType()) {
  144. settype($val, $col->getPhpType());
  145. $defaultValue = var_export($val, true);
  146. } elseif ($col->isPhpObjectType()) {
  147. $defaultValue = 'new '.$col->getPhpType().'(' . var_export($val, true) . ')';
  148. } elseif ($col->isPhpArrayType()) {
  149. $defaultValue = var_export($val, true);
  150. } else {
  151. throw new EngineException("Cannot get default value string for " . $col->getFullyQualifiedName());
  152. }
  153. return $defaultValue;
  154. }
  155. /**
  156. * Adds the include() statements for files that this class depends on or utilizes.
  157. * @param string &$script The script will be modified in this method.
  158. */
  159. protected function addIncludes(&$script)
  160. {
  161. } // addIncludes()
  162. /**
  163. * Adds class phpdoc comment and openning of class.
  164. * @param string &$script The script will be modified in this method.
  165. */
  166. protected function addClassOpen(&$script)
  167. {
  168. $table = $this->getTable();
  169. $tableName = $table->getName();
  170. $tableDesc = $table->getDescription();
  171. $interface = $this->getInterface();
  172. $parentClass = $this->getBehaviorContent('parentClass');
  173. $parentClass = (null !== $parentClass) ? $parentClass : ClassTools::classname($this->getBaseClass());
  174. if ($this->getBuildProperty('addClassLevelComment')) {
  175. $script .= "
  176. /**
  177. * Base class that represents a row from the '$tableName' table.
  178. *
  179. * $tableDesc
  180. *";
  181. if ($this->getBuildProperty('addTimeStamp')) {
  182. $now = strftime('%c');
  183. $script .= "
  184. * This class was autogenerated by Propel " . $this->getBuildProperty('version') . " on:
  185. *
  186. * $now
  187. *";
  188. }
  189. $script .= "
  190. * @package propel.generator.".$this->getPackage()."
  191. */";
  192. }
  193. $script .= "
  194. abstract class ".$this->getClassname()." extends ".$parentClass." ";
  195. if ($interface = $this->getTable()->getInterface()) {
  196. $script .= "implements " . ClassTools::classname($interface);
  197. if ($interface !== ClassTools::classname($interface)) {
  198. $this->declareClass($interface);
  199. } else {
  200. $this->declareClassFromBuilder($this->getInterfaceBuilder());
  201. }
  202. } elseif ($interface = ClassTools::getInterface($table)) {
  203. $script .= "implements " . ClassTools::classname($interface);
  204. }
  205. $script .= "
  206. {";
  207. }
  208. /**
  209. * Specifies the methods that are added as part of the basic OM class.
  210. * This can be overridden by subclasses that wish to add more methods.
  211. * @see ObjectBuilder::addClassBody()
  212. */
  213. protected function addClassBody(&$script)
  214. {
  215. $this->declareClassFromBuilder($this->getStubObjectBuilder());
  216. $this->declareClassFromBuilder($this->getStubPeerBuilder());
  217. $this->declareClassFromBuilder($this->getStubQueryBuilder());
  218. $this->declareClasses(
  219. 'Propel', 'PropelException', 'PDO', 'PropelPDO', 'Criteria',
  220. 'BaseObject', 'Persistent', 'BasePeer', 'PropelCollection',
  221. 'PropelObjectCollection', 'Exception'
  222. );
  223. $table = $this->getTable();
  224. if (!$table->isAlias()) {
  225. $this->addConstants($script);
  226. $this->addAttributes($script);
  227. }
  228. if ($table->hasCrossForeignKeys()) {
  229. foreach ($table->getCrossFks() as $fkList) {
  230. list($refFK, $crossFK) = $fkList;
  231. $fkName = $this->getFKPhpNameAffix($crossFK, $plural = true);
  232. $this->addScheduledForDeletionAttribute($script, $fkName);
  233. }
  234. }
  235. foreach ($table->getReferrers() as $refFK) {
  236. $fkName = $this->getRefFKPhpNameAffix($refFK, $plural = true);
  237. $this->addScheduledForDeletionAttribute($script, $fkName);
  238. }
  239. if ($this->hasDefaultValues()) {
  240. $this->addApplyDefaultValues($script);
  241. $this->addConstructor($script);
  242. }
  243. $this->addColumnAccessorMethods($script);
  244. $this->addColumnMutatorMethods($script);
  245. $this->addHasOnlyDefaultValues($script);
  246. $this->addHydrate($script);
  247. $this->addEnsureConsistency($script);
  248. if (!$table->isReadOnly()) {
  249. $this->addManipulationMethods($script);
  250. }
  251. if ($this->isAddValidateMethod()) {
  252. $this->addValidationMethods($script);
  253. }
  254. if ($this->isAddGenericAccessors()) {
  255. $this->addGetByName($script);
  256. $this->addGetByPosition($script);
  257. $this->addToArray($script);
  258. }
  259. if ($this->isAddGenericMutators()) {
  260. $this->addSetByName($script);
  261. $this->addSetByPosition($script);
  262. $this->addFromArray($script);
  263. }
  264. $this->addBuildCriteria($script);
  265. $this->addBuildPkeyCriteria($script);
  266. $this->addGetPrimaryKey($script);
  267. $this->addSetPrimaryKey($script);
  268. $this->addIsPrimaryKeyNull($script);
  269. $this->addCopy($script);
  270. if (!$table->isAlias()) {
  271. $this->addGetPeer($script);
  272. }
  273. $this->addFKMethods($script);
  274. $this->addRefFKMethods($script);
  275. $this->addCrossFKMethods($script);
  276. $this->addClear($script);
  277. $this->addClearAllReferences($script);
  278. $this->addPrimaryString($script);
  279. $this->addIsAlreadyInSave($script);
  280. // apply behaviors
  281. $this->applyBehaviorModifier('objectMethods', $script, " ");
  282. $this->addMagicCall($script);
  283. }
  284. /**
  285. * Closes class.
  286. * @param string &$script The script will be modified in this method.
  287. */
  288. protected function addClassClose(&$script)
  289. {
  290. $script .= "
  291. }
  292. ";
  293. $this->applyBehaviorModifier('objectFilter', $script, "");
  294. }
  295. /**
  296. * Adds any constants to the class.
  297. * @param string &$script The script will be modified in this method.
  298. */
  299. protected function addConstants(&$script)
  300. {
  301. $script .= "
  302. /**
  303. * Peer class name
  304. */
  305. const PEER = '" . addslashes($this->getStubPeerBuilder()->getFullyQualifiedClassname()) . "';
  306. ";
  307. }
  308. /**
  309. * Adds class attributes.
  310. * @param string &$script The script will be modified in this method.
  311. */
  312. protected function addAttributes(&$script)
  313. {
  314. $table = $this->getTable();
  315. $script .= "
  316. /**
  317. * The Peer class.
  318. * Instance provides a convenient way of calling static methods on a class
  319. * that calling code may not be able to identify.
  320. * @var ".$this->getPeerClassname()."
  321. */
  322. protected static \$peer;
  323. /**
  324. * The flag var to prevent infinit loop in deep copy
  325. * @var boolean
  326. */
  327. protected \$startCopy = false;
  328. ";
  329. if (!$table->isAlias()) {
  330. $this->addColumnAttributes($script);
  331. }
  332. foreach ($table->getForeignKeys() as $fk) {
  333. $this->addFKAttributes($script, $fk);
  334. }
  335. foreach ($table->getReferrers() as $refFK) {
  336. $this->addRefFKAttributes($script, $refFK);
  337. }
  338. // many-to-many relationships
  339. foreach ($table->getCrossFks() as $fkList) {
  340. $crossFK = $fkList[1];
  341. $this->addCrossFKAttributes($script, $crossFK);
  342. }
  343. $this->addAlreadyInSaveAttribute($script);
  344. $this->addAlreadyInValidationAttribute($script);
  345. // apply behaviors
  346. $this->applyBehaviorModifier('objectAttributes', $script, " ");
  347. }
  348. /**
  349. * Adds variables that store column values.
  350. * @param string &$script The script will be modified in this method.
  351. * @see addColumnNameConstants()
  352. */
  353. protected function addColumnAttributes(&$script)
  354. {
  355. $table = $this->getTable();
  356. foreach ($table->getColumns() as $col) {
  357. $this->addColumnAttributeComment($script, $col);
  358. $this->addColumnAttributeDeclaration($script, $col);
  359. if ($col->isLazyLoad() ) {
  360. $this->addColumnAttributeLoaderComment($script, $col);
  361. $this->addColumnAttributeLoaderDeclaration($script, $col);
  362. }
  363. if ($col->getType() == PropelTypes::OBJECT || $col->getType() == PropelTypes::PHP_ARRAY) {
  364. $this->addColumnAttributeUnserializedComment($script, $col);
  365. $this->addColumnAttributeUnserializedDeclaration($script, $col);
  366. }
  367. }
  368. }
  369. /**
  370. * Add comment about the attribute (variable) that stores column values
  371. * @param string &$script The script will be modified in this method.
  372. * @param Column $col
  373. **/
  374. protected function addColumnAttributeComment(&$script, Column $col)
  375. {
  376. $cptype = $col->getPhpType();
  377. $clo = strtolower($col->getName());
  378. $script .= "
  379. /**
  380. * The value for the $clo field.";
  381. if ($col->getDefaultValue()) {
  382. if ($col->getDefaultValue()->isExpression()) {
  383. $script .= "
  384. * Note: this column has a database default value of: (expression) ".$col->getDefaultValue()->getValue();
  385. } else {
  386. $script .= "
  387. * Note: this column has a database default value of: ". $this->getDefaultValueString($col);
  388. }
  389. }
  390. $script .= "
  391. * @var $cptype
  392. */";
  393. }
  394. /**
  395. * Adds the declaration of a column value storage attribute
  396. * @param string &$script The script will be modified in this method.
  397. * @param Column $col
  398. **/
  399. protected function addColumnAttributeDeclaration(&$script, Column $col)
  400. {
  401. $clo = strtolower($col->getName());
  402. $script .= "
  403. protected \$" . $clo . ";
  404. ";
  405. }
  406. /**
  407. * Adds the comment about the attribute keeping track if an attribute value has been loaded
  408. * @param string &$script The script will be modified in this method.
  409. * @param Column $col
  410. **/
  411. protected function addColumnAttributeLoaderComment(&$script, Column $col)
  412. {
  413. $clo = strtolower($col->getName());
  414. $script .= "
  415. /**
  416. * Whether the lazy-loaded \$$clo value has been loaded from database.
  417. * This is necessary to avoid repeated lookups if \$$clo column is null in the db.
  418. * @var boolean
  419. */";
  420. }
  421. /**
  422. * Adds the declaration of the attribute keeping track of an attribute's loaded state
  423. * @param string &$script The script will be modified in this method.
  424. * @param Column $col
  425. **/
  426. protected function addColumnAttributeLoaderDeclaration(&$script, Column $col)
  427. {
  428. $clo = strtolower($col->getName());
  429. $script .= "
  430. protected \$".$clo."_isLoaded = false;
  431. ";
  432. }
  433. /**
  434. * Adds the comment about the serialized attribute
  435. * @param string &$script The script will be modified in this method.
  436. * @param Column $col
  437. **/
  438. protected function addColumnAttributeUnserializedComment(&$script, Column $col)
  439. {
  440. $clo = strtolower($col->getName());
  441. $script .= "
  442. /**
  443. * The unserialized \$$clo value - i.e. the persisted object.
  444. * This is necessary to avoid repeated calls to unserialize() at runtime.
  445. * @var object
  446. */";
  447. }
  448. /**
  449. * Adds the declaration of the serialized attribute
  450. * @param string &$script The script will be modified in this method.
  451. * @param Column $col
  452. **/
  453. protected function addColumnAttributeUnserializedDeclaration(&$script, Column $col)
  454. {
  455. $clo = strtolower($col->getName()) . "_unserialized";
  456. $script .= "
  457. protected \$" . $clo . ";
  458. ";
  459. }
  460. /**
  461. * Adds the getPeer() method.
  462. * This is a convenient, non introspective way of getting the Peer class for a particular object.
  463. * @param string &$script The script will be modified in this method.
  464. */
  465. protected function addGetPeer(&$script)
  466. {
  467. $this->addGetPeerComment($script);
  468. $this->addGetPeerFunctionOpen($script);
  469. $this->addGetPeerFunctionBody($script);
  470. $this->addGetPeerFunctionClose($script);
  471. }
  472. /**
  473. * Add the comment for the getPeer method
  474. * @param string &$script The script will be modified in this method.
  475. **/
  476. protected function addGetPeerComment(&$script)
  477. {
  478. $script .= "
  479. /**
  480. * Returns a peer instance associated with this om.
  481. *
  482. * Since Peer classes are not to have any instance attributes, this method returns the
  483. * same instance for all member of this class. The method could therefore
  484. * be static, but this would prevent one from overriding the behavior.
  485. *
  486. * @return ".$this->getPeerClassname()."
  487. */";
  488. }
  489. /**
  490. * Adds the function declaration (function opening) for the getPeer method
  491. * @param string &$script The script will be modified in this method.
  492. **/
  493. protected function addGetPeerFunctionOpen(&$script)
  494. {
  495. $script .= "
  496. public function getPeer()
  497. {";
  498. }
  499. /**
  500. * Adds the body of the getPeer method
  501. * @param string &$script The script will be modified in this method.
  502. **/
  503. protected function addGetPeerFunctionBody(&$script)
  504. {
  505. $script .= "
  506. if (self::\$peer === null) {
  507. " . $this->buildObjectInstanceCreationCode('self::$peer', $this->getPeerClassname()) . "
  508. }
  509. return self::\$peer;";
  510. }
  511. /**
  512. * Add the function close for the getPeer method
  513. * 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
  514. * @param string &$script The script will be modified in this method.
  515. **/
  516. protected function addGetPeerFunctionClose(&$script)
  517. {
  518. $script .= "
  519. }
  520. ";
  521. }
  522. /**
  523. * Adds the constructor for this object.
  524. * @param string &$script The script will be modified in this method.
  525. * @see addConstructor()
  526. */
  527. protected function addConstructor(&$script)
  528. {
  529. $this->addConstructorComment($script);
  530. $this->addConstructorOpen($script);
  531. $this->addConstructorBody($script);
  532. $this->addConstructorClose($script);
  533. }
  534. /**
  535. * Adds the comment for the constructor
  536. * @param string &$script The script will be modified in this method.
  537. **/
  538. protected function addConstructorComment(&$script)
  539. {
  540. $script .= "
  541. /**
  542. * Initializes internal state of ".$this->getClassname()." object.
  543. * @see applyDefaults()
  544. */";
  545. }
  546. /**
  547. * Adds the function declaration for the constructor
  548. * @param string &$script The script will be modified in this method.
  549. **/
  550. protected function addConstructorOpen(&$script)
  551. {
  552. $script .= "
  553. public function __construct()
  554. {";
  555. }
  556. /**
  557. * Adds the function body for the constructor
  558. * @param string &$script The script will be modified in this method.
  559. **/
  560. protected function addConstructorBody(&$script)
  561. {
  562. $script .= "
  563. parent::__construct();
  564. \$this->applyDefaultValues();";
  565. }
  566. /**
  567. * Adds the function close for the constructor
  568. * @param string &$script The script will be modified in this method.
  569. **/
  570. protected function addConstructorClose(&$script)
  571. {
  572. $script .= "
  573. }
  574. ";
  575. }
  576. /**
  577. * Adds the applyDefaults() method, which is called from the constructor.
  578. * @param string &$script The script will be modified in this method.
  579. * @see addConstructor()
  580. */
  581. protected function addApplyDefaultValues(&$script)
  582. {
  583. $this->addApplyDefaultValuesComment($script);
  584. $this->addApplyDefaultValuesOpen($script);
  585. $this->addApplyDefaultValuesBody($script);
  586. $this->addApplyDefaultValuesClose($script);
  587. }
  588. /**
  589. * Adds the comment for the applyDefaults method
  590. * @param string &$script The script will be modified in this method.
  591. * @see addApplyDefaultValues()
  592. **/
  593. protected function addApplyDefaultValuesComment(&$script)
  594. {
  595. $script .= "
  596. /**
  597. * Applies default values to this object.
  598. * This method should be called from the object's constructor (or
  599. * equivalent initialization method).
  600. * @see __construct()
  601. */";
  602. }
  603. /**
  604. * Adds the function declaration for the applyDefaults method
  605. * @param string &$script The script will be modified in this method.
  606. * @see addApplyDefaultValues()
  607. **/
  608. protected function addApplyDefaultValuesOpen(&$script)
  609. {
  610. $script .= "
  611. public function applyDefaultValues()
  612. {";
  613. }
  614. /**
  615. * Adds the function body of the applyDefault method
  616. * @param string &$script The script will be modified in this method.
  617. * @see addApplyDefaultValues()
  618. **/
  619. protected function addApplyDefaultValuesBody(&$script)
  620. {
  621. $table = $this->getTable();
  622. // FIXME - Apply support for PHP default expressions here
  623. // see: http://propel.phpdb.org/trac/ticket/378
  624. $colsWithDefaults = array();
  625. foreach ($table->getColumns() as $col) {
  626. $def = $col->getDefaultValue();
  627. if ($def !== null && !$def->isExpression()) {
  628. $colsWithDefaults[] = $col;
  629. }
  630. }
  631. $colconsts = array();
  632. foreach ($colsWithDefaults as $col) {
  633. $clo = strtolower($col->getName());
  634. $defaultValue = $this->getDefaultValueString($col);
  635. $script .= "
  636. \$this->".$clo." = $defaultValue;";
  637. }
  638. }
  639. /**
  640. * Adds the function close for the applyDefaults method
  641. * @param string &$script The script will be modified in this method.
  642. * @see addApplyDefaultValues()
  643. **/
  644. protected function addApplyDefaultValuesClose(&$script)
  645. {
  646. $script .= "
  647. }
  648. ";
  649. }
  650. // --------------------------------------------------------------
  651. //
  652. // A C C E S S O R M E T H O D S
  653. //
  654. // --------------------------------------------------------------
  655. /**
  656. * Adds a date/time/timestamp getter method.
  657. * @param string &$script The script will be modified in this method.
  658. * @param Column $col The current column.
  659. * @see parent::addColumnAccessors()
  660. */
  661. protected function addTemporalAccessor(&$script, Column $col)
  662. {
  663. $this->addTemporalAccessorComment($script, $col);
  664. $this->addTemporalAccessorOpen($script, $col);
  665. $this->addTemporalAccessorBody($script, $col);
  666. $this->addTemporalAccessorClose($script, $col);
  667. } // addTemporalAccessor
  668. /**
  669. * Adds the comment for a temporal accessor
  670. * @param string &$script The script will be modified in this method.
  671. * @param Column $col The current column.
  672. * @see addTemporalAccessor
  673. **/
  674. public function addTemporalAccessorComment(&$script, Column $col)
  675. {
  676. $clo = strtolower($col->getName());
  677. $useDateTime = $this->getBuildProperty('useDateTimeClass');
  678. $dateTimeClass = $this->getBuildProperty('dateTimeClass');
  679. if (!$dateTimeClass) {
  680. $dateTimeClass = 'DateTime';
  681. }
  682. $handleMysqlDate = false;
  683. if ($this->getPlatform() instanceof MysqlPlatform) {
  684. if ($col->getType() === PropelTypes::TIMESTAMP) {
  685. $handleMysqlDate = true;
  686. $mysqlInvalidDateString = '0000-00-00 00:00:00';
  687. } elseif ($col->getType() === PropelTypes::DATE) {
  688. $handleMysqlDate = true;
  689. $mysqlInvalidDateString = '0000-00-00';
  690. }
  691. // 00:00:00 is a valid time, so no need to check for that.
  692. }
  693. $script .= "
  694. /**
  695. * Get the [optionally formatted] temporal [$clo] column value.
  696. * ".$col->getDescription();
  697. if (!$useDateTime) {
  698. $script .= "
  699. * This accessor only only work with unix epoch dates. Consider enabling the propel.useDateTimeClass
  700. * option in order to avoid converstions to integers (which are limited in the dates they can express).";
  701. }
  702. $script .= "
  703. *
  704. * @param string \$format The date/time format string (either date()-style or strftime()-style).
  705. * If format is null, then the raw ".($useDateTime ? 'DateTime object' : 'unix timestamp integer')." will be returned.";
  706. if ($useDateTime) {
  707. $script .= "
  708. * @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 : '');
  709. } else {
  710. $script .= "
  711. * @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 : '');
  712. }
  713. $script .= "
  714. * @throws PropelException - if unable to parse/validate the date/time value.
  715. */";
  716. }
  717. /**
  718. * Adds the function declaration for a temporal accessor
  719. * @param string &$script The script will be modified in this method.
  720. * @param Column $col The current column.
  721. * @see addTemporalAccessor
  722. **/
  723. public function addTemporalAccessorOpen(&$script, Column $col)
  724. {
  725. $cfc = $col->getPhpName();
  726. $defaultfmt = null;
  727. $visibility = $col->getAccessorVisibility();
  728. // Default date/time formatter strings are specified in build.properties
  729. if ($col->getType() === PropelTypes::DATE) {
  730. $defaultfmt = $this->getBuildProperty('defaultDateFormat');
  731. } elseif ($col->getType() === PropelTypes::TIME) {
  732. $defaultfmt = $this->getBuildProperty('defaultTimeFormat');
  733. } elseif ($col->getType() === PropelTypes::TIMESTAMP) {
  734. $defaultfmt = $this->getBuildProperty('defaultTimeStampFormat');
  735. }
  736. if (empty($defaultfmt)) {
  737. $defaultfmt = 'null';
  738. } else {
  739. $defaultfmt = var_export($defaultfmt, true);
  740. }
  741. $script .= "
  742. ".$visibility." function get$cfc(\$format = ".$defaultfmt."";
  743. if ($col->isLazyLoad()) $script .= ", \$con = null";
  744. $script .= ")
  745. {";
  746. }
  747. protected function getAccessorLazyLoadSnippet(Column $col)
  748. {
  749. if ($col->isLazyLoad()) {
  750. $clo = strtolower($col->getName());
  751. $defaultValueString = 'null';
  752. $def = $col->getDefaultValue();
  753. if ($def !== null && !$def->isExpression()) {
  754. $defaultValueString = $this->getDefaultValueString($col);
  755. }
  756. return "
  757. if (!\$this->{$clo}_isLoaded && \$this->{$clo} === {$defaultValueString} && !\$this->isNew()) {
  758. \$this->load{$col->getPhpName()}(\$con);
  759. }
  760. ";
  761. }
  762. }
  763. /**
  764. * Adds the body of the temporal accessor
  765. * @param string &$script The script will be modified in this method.
  766. * @param Column $col The current column.
  767. * @see addTemporalAccessor
  768. **/
  769. protected function addTemporalAccessorBody(&$script, Column $col)
  770. {
  771. $cfc = $col->getPhpName();
  772. $clo = strtolower($col->getName());
  773. $useDateTime = $this->getBuildProperty('useDateTimeClass');
  774. $dateTimeClass = $this->getBuildProperty('dateTimeClass');
  775. if (!$dateTimeClass) {
  776. $dateTimeClass = 'DateTime';
  777. }
  778. $this->declareClasses($dateTimeClass);
  779. $defaultfmt = null;
  780. // Default date/time formatter strings are specified in build.properties
  781. if ($col->getType() === PropelTypes::DATE) {
  782. $defaultfmt = $this->getBuildProperty('defaultDateFormat');
  783. } elseif ($col->getType() === PropelTypes::TIME) {
  784. $defaultfmt = $this->getBuildProperty('defaultTimeFormat');
  785. } elseif ($col->getType() === PropelTypes::TIMESTAMP) {
  786. $defaultfmt = $this->getBuildProperty('defaultTimeStampFormat');
  787. }
  788. if (empty($defaultfmt)) {
  789. $defaultfmt = null;
  790. }
  791. $handleMysqlDate = false;
  792. if ($this->getPlatform() instanceof MysqlPlatform) {
  793. if ($col->getType() === PropelTypes::TIMESTAMP) {
  794. $handleMysqlDate = true;
  795. $mysqlInvalidDateString = '0000-00-00 00:00:00';
  796. } elseif ($col->getType() === PropelTypes::DATE) {
  797. $handleMysqlDate = true;
  798. $mysqlInvalidDateString = '0000-00-00';
  799. }
  800. // 00:00:00 is a valid time, so no need to check for that.
  801. }
  802. if ($col->isLazyLoad()) {
  803. $script .= $this->getAccessorLazyLoadSnippet($col);
  804. }
  805. $script .= "
  806. if (\$this->$clo === null) {
  807. return null;
  808. }
  809. ";
  810. if ($handleMysqlDate) {
  811. $script .= "
  812. if (\$this->$clo === '$mysqlInvalidDateString') {
  813. // while technically this is not a default value of null,
  814. // this seems to be closest in meaning.
  815. return null;
  816. } else {
  817. try {
  818. \$dt = new $dateTimeClass(\$this->$clo);
  819. } catch (Exception \$x) {
  820. throw new PropelException(\"Internally stored date/time/timestamp value could not be converted to $dateTimeClass: \" . var_export(\$this->$clo, true), \$x);
  821. }
  822. }
  823. ";
  824. } else {
  825. $script .= "
  826. try {
  827. \$dt = new $dateTimeClass(\$this->$clo);
  828. } catch (Exception \$x) {
  829. throw new PropelException(\"Internally stored date/time/timestamp value could not be converted to $dateTimeClass: \" . var_export(\$this->$clo, true), \$x);
  830. }
  831. ";
  832. } // if handleMyqlDate
  833. $script .= "
  834. if (\$format === null) {";
  835. if ($useDateTime) {
  836. $script .= "
  837. // Because propel.useDateTimeClass is true, we return a $dateTimeClass object.
  838. return \$dt;";
  839. } else {
  840. $script .= "
  841. // We cast here to maintain BC in API; obviously we will lose data if we're dealing with pre-/post-epoch dates.
  842. return (int) \$dt->format('U');";
  843. }
  844. $script .= "
  845. } elseif (strpos(\$format, '%') !== false) {
  846. return strftime(\$format, \$dt->format('U'));
  847. } else {
  848. return \$dt->format(\$format);
  849. }";
  850. }
  851. /**
  852. * Adds the body of the temporal accessor
  853. * @param string &$script The script will be modified in this method.
  854. * @param Column $col The current column.
  855. * @see addTemporalAccessorClose
  856. **/
  857. protected function addTemporalAccessorClose(&$script, Column $col)
  858. {
  859. $script .= "
  860. }
  861. ";
  862. }
  863. /**
  864. * Adds an object getter method.
  865. * @param string &$script The script will be modified in this method.
  866. * @param Column $col The current column.
  867. * @see parent::addColumnAccessors()
  868. */
  869. protected function addObjectAccessor(&$script, Column $col)
  870. {
  871. $this->addDefaultAccessorComment($script, $col);
  872. $this->addDefaultAccessorOpen($script, $col);
  873. $this->addObjectAccessorBody($script, $col);
  874. $this->addDefaultAccessorClose($script, $col);
  875. }
  876. /**
  877. * Adds the function body for an object accessor method
  878. * @param string &$script The script will be modified in this method.
  879. * @param Column $col The current column.
  880. * @see addDefaultAccessor()
  881. **/
  882. protected function addObjectAccessorBody(&$script, Column $col)
  883. {
  884. $cfc = $col->getPhpName();
  885. $clo = strtolower($col->getName());
  886. $cloUnserialized = $clo.'_unserialized';
  887. if ($col->isLazyLoad()) {
  888. $script .= $this->getAccessorLazyLoadSnippet($col);
  889. }
  890. $script .= "
  891. if (null == \$this->$cloUnserialized && null !== \$this->$clo) {
  892. \$this->$cloUnserialized = unserialize(\$this->$clo);
  893. }
  894. return \$this->$cloUnserialized;";
  895. }
  896. /**
  897. * Adds an array getter method.
  898. * @param string &$script The script will be modified in this method.
  899. * @param Column $col The current column.
  900. * @see parent::addColumnAccessors()
  901. */
  902. protected function addArrayAccessor(&$script, Column $col)
  903. {
  904. $this->addDefaultAccessorComment($script, $col);
  905. $this->addDefaultAccessorOpen($script, $col);
  906. $this->addArrayAccessorBody($script, $col);
  907. $this->addDefaultAccessorClose($script, $col);
  908. }
  909. /**
  910. * Adds the function body for an array accessor method
  911. * @param string &$script The script will be modified in this method.
  912. * @param Column $col The current column.
  913. * @see addDefaultAccessor()
  914. **/
  915. protected function addArrayAccessorBody(&$script, Column $col)
  916. {
  917. $cfc = $col->getPhpName();
  918. $clo = strtolower($col->getName());
  919. $cloUnserialized = $clo.'_unserialized';
  920. if ($col->isLazyLoad()) {
  921. $script .= $this->getAccessorLazyLoadSnippet($col);
  922. }
  923. $script .= "
  924. if (null === \$this->$cloUnserialized) {
  925. \$this->$cloUnserialized = array();
  926. }
  927. if (!\$this->$cloUnserialized && null !== \$this->$clo) {
  928. \$$cloUnserialized = substr(\$this->$clo, 2, -2);
  929. \$this->$cloUnserialized = \$$cloUnserialized ? explode(' | ', \$$cloUnserialized) : array();
  930. }
  931. return \$this->$cloUnserialized;";
  932. }
  933. /**
  934. * Adds an enum getter method.
  935. * @param string &$script The script will be modified in this method.
  936. * @param Column $col The current column.
  937. * @see parent::addColumnAccessors()
  938. */
  939. protected function addEnumAccessor(&$script, Column $col)
  940. {
  941. $clo = strtolower($col->getName());
  942. $script .= "
  943. /**
  944. * Get the [$clo] column value.
  945. * ".$col->getDescription();
  946. if ($col->isLazyLoad()) {
  947. $script .= "
  948. * @param PropelPDO \$con An optional PropelPDO connection to use for fetching this lazy-loaded column.";
  949. }
  950. $script .= "
  951. * @return ".$col->getPhpType()."
  952. * @throws PropelException - if the stored enum key is unknown.
  953. */";
  954. $this->addDefaultAccessorOpen($script, $col);
  955. $this->addEnumAccessorBody($script, $col);
  956. $this->addDefaultAccessorClose($script, $col);
  957. }
  958. /**
  959. * Adds the function body for an enum accessor method
  960. * @param string &$script The script will be modified in this method.
  961. * @param Column $col The current column.
  962. * @see addDefaultAccessor()
  963. **/
  964. protected function addEnumAccessorBody(&$script, Column $col)
  965. {
  966. $cfc = $col->getPhpName();
  967. $clo = strtolower($col->getName());
  968. if ($col->isLazyLoad()) {
  969. $script .= $this->getAccessorLazyLoadSnippet($col);
  970. }
  971. $script .= "
  972. if (null === \$this->$clo) {
  973. return null;
  974. }
  975. \$valueSet = " . $this->getPeerClassname() . "::getValueSet(" . $this->getColumnConstant($col) . ");
  976. if (!isset(\$valueSet[\$this->$clo])) {
  977. throw new PropelException('Unknown stored enum key: ' . \$this->$clo);
  978. }
  979. return \$valueSet[\$this->$clo];";
  980. }
  981. /**
  982. * Adds a tester method for an array column.
  983. * @param string &$script The script will be modified in this method.
  984. * @param Column $col The current column.
  985. */
  986. protected function addHasArrayElement(&$script, Column $col)
  987. {
  988. $clo = strtolower($col->getName());
  989. $cfc = $col->getPhpName();
  990. $visibility = $col->getAccessorVisibility();
  991. $singularPhpName = rtrim($cfc, 's');
  992. $script .= "
  993. /**
  994. * Test the presence of a value in the [$clo] array column value.
  995. * @param mixed \$value
  996. * ".$col->getDescription();
  997. if ($col->isLazyLoad()) {
  998. $script .= "
  999. * @param PropelPDO \$con An optional PropelPDO connection to use for fetching this lazy-loaded column.";
  1000. }
  1001. $script .= "
  1002. * @return boolean
  1003. */
  1004. $visibility function has$singularPhpName(\$value";
  1005. if ($col->isLazyLoad()) $script .= ", PropelPDO \$con = null";
  1006. $script .= ")
  1007. {
  1008. return in_array(\$value, \$this->get$cfc(";
  1009. if ($col->isLazyLoad()) $script .= "\$con";
  1010. $script .= "));
  1011. } // has$singularPhpName()
  1012. ";
  1013. }
  1014. /**
  1015. * Adds a normal (non-temporal) getter 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 addDefaultAccessor(&$script, Column $col)
  1021. {
  1022. $this->addDefaultAccessorComment($script, $col);
  1023. $this->addDefaultAccessorOpen($script, $col);
  1024. $this->addDefaultAccessorBody($script, $col);
  1025. $this->addDefaultAccessorClose($script, $col);
  1026. }
  1027. /**
  1028. * Add the comment for a default accessor method (a getter)
  1029. * @param string &$script The script will be modified in this method.
  1030. * @param Column $col The current column.
  1031. * @see addDefaultAccessor()
  1032. **/
  1033. public function addDefaultAccessorComment(&$script, Column $col)
  1034. {
  1035. $clo=strtolower($col->getName());
  1036. $script .= "
  1037. /**
  1038. * Get the [$clo] column value.
  1039. * ".$col->getDescription();
  1040. if ($col->isLazyLoad()) {
  1041. $script .= "
  1042. * @param PropelPDO \$con An optional PropelPDO connection to use for fetching this lazy-loaded column.";
  1043. }
  1044. $script .= "
  1045. * @return ".$col->getPhpType()."
  1046. */";
  1047. }
  1048. /**
  1049. * Adds the function declaration for a default accessor
  1050. * @param string &$script The script will be modified in this method.
  1051. * @param Column $col The current column.
  1052. * @see addDefaultAccessor()
  1053. **/
  1054. public function addDefaultAccessorOpen(&$script, Column $col)
  1055. {
  1056. $cfc = $col->getPhpName();
  1057. $visibility = $col->getAccessorVisibility();
  1058. $script .= "
  1059. ".$visibility." function get$cfc(";
  1060. if ($col->isLazyLoad()) $script .= "PropelPDO \$con = null";
  1061. $script .= ")
  1062. {";
  1063. }
  1064. /**
  1065. * Adds the function body for a default accessor method
  1066. * @param string &$script The script will be modified in this method.
  1067. * @param Column $col The current column.
  1068. * @see addDefaultAccessor()
  1069. **/
  1070. protected function addDefaultAccessorBody(&$script, Column $col)
  1071. {
  1072. $cfc = $col->getPhpName();
  1073. $clo = strtolower($col->getName());
  1074. if ($col->isLazyLoad()) {
  1075. $script .= $this->getAccessorLazyLoadSnippet($col);
  1076. }
  1077. $script .= "
  1078. return \$this->$clo;";
  1079. }
  1080. /**
  1081. * Adds the function close for a default accessor method
  1082. * @param string &$script The script will be modified in this method.
  1083. * @param Column $col The current column.
  1084. * @see addDefaultAccessor()
  1085. **/
  1086. protected function addDefaultAccessorClose(&$script, Column $col)
  1087. {
  1088. $script .= "
  1089. }
  1090. ";
  1091. }
  1092. /**
  1093. * Adds the lazy loader method.
  1094. * @param string &$script The script will be modified in this method.
  1095. * @param Column $col The current column.
  1096. * @see parent::addColumnAccessors()
  1097. */
  1098. protected function addLazyLoader(&$script, Column $col)
  1099. {
  1100. $this->addLazyLoaderComment($script, $col);
  1101. $this->addLazyLoaderOpen($script, $col);
  1102. $this->addLazyLoaderBody($script, $col);
  1103. $this->addLazyLoaderClose($script, $col);
  1104. }
  1105. /**
  1106. * Adds the comment for the lazy loader method
  1107. * @param string &$script The script will be modified in this method.
  1108. * @param Column $col The current column.
  1109. * @see addLazyLoader()
  1110. **/
  1111. protected function addLazyLoaderComment(&$script, Column $col)
  1112. {
  1113. $clo = strtolower($col->getName());
  1114. $script .= "
  1115. /**
  1116. * Load the value for the lazy-loaded [$clo] column.
  1117. *
  1118. * This method performs an additional query to return the value for
  1119. * the [$clo] column, since it is not populated by
  1120. * the hydrate() method.
  1121. *
  1122. * @param PropelPDO \$con (optional) The PropelPDO connection to use.
  1123. * @return void
  1124. * @throws PropelException - any underlying error will be wrapped and re-thrown.
  1125. */";
  1126. }
  1127. /**
  1128. * Adds the function declaration for the lazy loader method
  1129. * @param string &$script The script will be modified in this method.
  1130. * @param Column $col The current column.
  1131. * @see addLazyLoader()
  1132. **/
  1133. protected function addLazyLoaderOpen(&$script, Column $col)
  1134. {
  1135. $cfc = $col->getPhpName();
  1136. $script .= "
  1137. protected function load$cfc(PropelPDO \$con = null)
  1138. {";
  1139. }
  1140. /**
  1141. * Adds the function body for the lazy loader method
  1142. * @param string &$script The script will be modified in this method.
  1143. * @param Column $col The current column.
  1144. * @see addLazyLoader()
  1145. **/
  1146. protected function addLazyLoaderBody(&$script, Column $col)
  1147. {
  1148. $platform = $this->getPlatform();
  1149. $clo = strtolower($col->getName());
  1150. // pdo_sqlsrv driver requires the use of PDOStatement::bindColumn() or a hex string will be returned
  1151. if ($col->getType() === PropelTypes::BLOB && $platform instanceof SqlsrvPlatform) {
  1152. $script .= "
  1153. \$c = \$this->buildPkeyCriteria();
  1154. \$c->addSelectColumn(".$this->getColumnConstant($col).");
  1155. try {
  1156. \$row = array(0 => null);
  1157. \$stmt = ".$this->getPeerClassname()."::doSelectStmt(\$c, \$con);
  1158. \$stmt->bindColumn(1, \$row[0], PDO::PARAM_LOB, 0, PDO::SQLSRV_ENCODING_BINARY);
  1159. \$stmt->fetch(PDO::FETCH_BOUND);
  1160. \$stmt->closeCursor();";
  1161. } else {
  1162. $script .= "
  1163. \$c = \$this->buildPkeyCriteria();
  1164. \$c->addSelectColumn(".$this->getColumnConstant($col).");
  1165. try {
  1166. \$stmt = ".$this->getPeerClassname()."::doSelectStmt(\$c, \$con);
  1167. \$row = \$stmt->fetch(PDO::FETCH_NUM);
  1168. \$stmt->closeCursor();";
  1169. }
  1170. if ($col->getType() === PropelTypes::CLOB && $platform instanceof OraclePlatform) {
  1171. // PDO_OCI returns a stream for CLOB objects, while other PDO adapters return a string...
  1172. $script .= "
  1173. \$this->$clo = stream_get_contents(\$row[0]);";
  1174. } elseif ($col->isLobType() && !$platform->hasStreamBlobImpl()) {
  1175. $script .= "
  1176. if (\$row[0] !== null) {
  1177. \$this->$clo = fopen('php://memory', 'r+');
  1178. fwrite(\$this->$clo, \$row[0]);
  1179. rewind(\$this->$clo);
  1180. } else {
  1181. \$this->$clo = null;
  1182. }";
  1183. } elseif ($col->isPhpPrimitiveType()) {
  1184. $script .= "
  1185. \$this->$clo = (\$row[0] !== null) ? (".$col->getPhpType().") \$row[0] : null;";
  1186. } elseif ($col->isPhpObjectType()) {
  1187. $script .= "
  1188. \$this->$clo = (\$row[0] !== null) ? new ".$col->getPhpType()."(\$row[0]) : null;";
  1189. } else {
  1190. $script .= "
  1191. \$this->$clo = \$row[0];";
  1192. }
  1193. $script .= "
  1194. \$this->".$clo."_isLoaded = true;
  1195. } catch (Exception \$e) {
  1196. throw new PropelException(\"Error loading value for [$clo] column on demand.\", \$e);
  1197. }";
  1198. }
  1199. /**
  1200. * Adds the function close for the lazy loader
  1201. * @param string &$script The script will be modified in this method.
  1202. * @param Column $col The current column.
  1203. * @see addLazyLoader()
  1204. **/
  1205. protected function addLazyLoaderClose(&$script, Column $col)
  1206. {
  1207. $script .= "
  1208. }";
  1209. } // addLazyLoader()
  1210. // --------------------------------------------------------------
  1211. //
  1212. // M U T A T O R M E T H O D S
  1213. //
  1214. // --------------------------------------------------------------
  1215. /**
  1216. * Adds the open of the mutator (setter) method for a column.
  1217. * @param string &$script The script will be modified in this method.
  1218. * @param Column $col The current column.
  1219. */
  1220. protected function addMutatorOpen(&$script, Column $col)
  1221. {
  1222. $this->addMutatorComment($script, $col);
  1223. $this->addMutatorOpenOpen($script, $col);
  1224. $this->addMutatorOpenBody($script, $col);
  1225. }
  1226. /**
  1227. * Adds the comment for a mutator
  1228. * @param string &$script The script will be modified in this method.
  1229. * @param Column $col The current column.
  1230. * @see addMutatorOpen()
  1231. **/
  1232. public function addMutatorComment(&$script, Column $col)
  1233. {
  1234. $clo = strtolower($col->getName());
  1235. $script .= "
  1236. /**
  1237. * Set the value of [$clo] column.
  1238. * ".$col->getDescription()."
  1239. * @param ".$col->getPhpType()." \$v new value
  1240. * @return ".$this->getObjectClassname()." The current object (for fluent API support)
  1241. */";
  1242. }
  1243. /**
  1244. * Adds the mutator function declaration
  1245. * @param string &$script The script will be modified in this method.
  1246. * @param Column $col The current column.
  1247. * @see addMutatorOpen()
  1248. **/
  1249. public function addMutatorOpenOpen(&$script, Column $col)
  1250. {
  1251. $cfc = $col->getPhpName();
  1252. $visibility = $col->getMutatorVisibility();
  1253. $script .= "
  1254. ".$visibility." function set$cfc(\$v)
  1255. {";
  1256. }
  1257. /**
  1258. * Adds the mutator open body part
  1259. * @param string &$script The script will be modified in this method.
  1260. * @param Column $col The current column.
  1261. * @see addMutatorOpen()
  1262. **/
  1263. protected function addMutatorOpenBody(&$script, Column $col)
  1264. {
  1265. $clo = strtolower($col->getName());
  1266. $cfc = $col->getPhpName();
  1267. if ($col->isLazyLoad()) {
  1268. $script .= "
  1269. // Allow unsetting the lazy loaded column even when its not loaded.
  1270. if (!\$this->".$clo."_isLoaded && \$v === null) {
  1271. \$this->modifiedColumns[] = ".$this->getColumnConstant($col).";
  1272. }
  1273. // explicitly set the is-loaded flag to true for this lazy load col;
  1274. // it doesn't matter if the value is actually set or not (logic below) as
  1275. // any attempt to set the value means that no db lookup should be performed
  1276. // when the get$cfc() method is called.
  1277. \$this->".$clo."_isLoaded = true;
  1278. ";
  1279. }
  1280. }
  1281. /**
  1282. * Adds the close of the mutator (setter) method for a column.
  1283. *
  1284. * @param string &$script The script will be modified in this method.
  1285. * @param Column $col The current column.
  1286. */
  1287. protected function addMutatorClose(&$script, Column $col)
  1288. {
  1289. $this->addMutatorCloseBody($script, $col);
  1290. $this->addMutatorCloseClose($script, $col);
  1291. }
  1292. /**
  1293. * Adds the body of the close part of a mutator
  1294. * @param string &$script The script will be modified in this method.
  1295. * @param Column $col The current column.
  1296. * @see addMutatorClose()
  1297. **/
  1298. protected function addMutatorCloseBody(&$script, Column $col)
  1299. {
  1300. $table = $this->getTable();
  1301. $cfc = $col->getPhpName();
  1302. $clo = strtolower($col->getName());
  1303. if ($col->isForeignKey()) {
  1304. foreach ($col->getForeignKeys() as $fk) {
  1305. $tblFK = $table->getDatabase()->getTable($fk->getForeignTableName());
  1306. $colFK = $tblFK->getColumn($fk->getMappedForeignColumn($col->getName()));
  1307. $varName = $this->getFKVarName($fk);
  1308. $script .= "
  1309. if (\$this->$varName !== null && \$this->".$varName."->get".$colFK->g…

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