PageRenderTime 54ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

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

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

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