PageRenderTime 48ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/generator/lib/builder/om/PHP5ObjectNoCollectionBuilder.php

https://github.com/1989gaurav/Propel
PHP | 969 lines | 564 code | 117 blank | 288 comment | 79 complexity | 260fec5e138263b894c5a9115ffe0b09 MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of the Propel package.
  4. * For the full copyright and license information, please view the LICENSE
  5. * file that was distributed with this source code.
  6. *
  7. * @license MIT License
  8. */
  9. require_once dirname(__FILE__) . '/PHP5ObjectBuilder.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. * This class overrides PHP5BaseObject to use Peer methods and Criteria
  17. * instead of Query objects for fetching foreign keys. This can be useful if
  18. * some legacy Propel 1.4 code assumes that the getters returns arrays
  19. * instead of collections.
  20. *
  21. * This class is not used by default. You must override
  22. * the propel.builder.object.class setting in build.properties to use it:
  23. * <code>
  24. * propel.builder.object.class = builder.om.PHP5ObjectNoCollectionBuilder
  25. * </code>
  26. *
  27. * @deprecated Since Propel 1.5
  28. * @author Hans Lellelid <hans@xmpl.org>
  29. * @package propel.generator.builder.om
  30. */
  31. class PHP5ObjectNoCollectionBuilder extends PHP5ObjectBuilder
  32. {
  33. /**
  34. * Adds the lazy loader method.
  35. * @param string &$script The script will be modified in this method.
  36. * @param Column $col The current column.
  37. * @see parent::addColumnAccessors()
  38. */
  39. protected function addLazyLoader(&$script, Column $col)
  40. {
  41. $this->addLazyLoaderComment($script, $col);
  42. $this->addLazyLoaderOpen($script, $col);
  43. $this->addLazyLoaderBody($script, $col);
  44. $this->addLazyLoaderClose($script, $col);
  45. }
  46. /**
  47. * Adds the comment for the lazy loader method
  48. * @param string &$script The script will be modified in this method.
  49. * @param Column $col The current column.
  50. * @see addLazyLoader()
  51. **/
  52. protected function addLazyLoaderComment(&$script, Column $col) {
  53. $clo = strtolower($col->getName());
  54. $script .= "
  55. /**
  56. * Load the value for the lazy-loaded [$clo] column.
  57. *
  58. * This method performs an additional query to return the value for
  59. * the [$clo] column, since it is not populated by
  60. * the hydrate() method.
  61. *
  62. * @param \$con PropelPDO (optional) The PropelPDO connection to use.
  63. * @return void
  64. * @throws PropelException - any underlying error will be wrapped and re-thrown.
  65. */";
  66. }
  67. /**
  68. * Adds the function declaration for the lazy loader method
  69. * @param string &$script The script will be modified in this method.
  70. * @param Column $col The current column.
  71. * @see addLazyLoader()
  72. **/
  73. protected function addLazyLoaderOpen(&$script, Column $col) {
  74. $cfc = $col->getPhpName();
  75. $script .= "
  76. protected function load$cfc(PropelPDO \$con = null)
  77. {";
  78. }
  79. /**
  80. * Adds the function body for the lazy loader method
  81. * @param string &$script The script will be modified in this method.
  82. * @param Column $col The current column.
  83. * @see addLazyLoader()
  84. **/
  85. protected function addLazyLoaderBody(&$script, Column $col) {
  86. $platform = $this->getPlatform();
  87. $clo = strtolower($col->getName());
  88. $script .= "
  89. \$c = \$this->buildPkeyCriteria();
  90. \$c->addSelectColumn(".$this->getColumnConstant($col).");
  91. try {
  92. \$stmt = ".$this->getPeerClassname()."::doSelectStmt(\$c, \$con);
  93. \$row = \$stmt->fetch(PDO::FETCH_NUM);
  94. \$stmt->closeCursor();";
  95. if ($col->getType() === PropelTypes::CLOB && $this->getPlatform() instanceof OraclePlatform) {
  96. // PDO_OCI returns a stream for CLOB objects, while other PDO adapters return a string...
  97. $script .= "
  98. \$this->$clo = stream_get_contents(\$row[0]);";
  99. } elseif ($col->isLobType() && !$platform->hasStreamBlobImpl()) {
  100. $script .= "
  101. if (\$row[0] !== null) {
  102. \$this->$clo = fopen('php://memory', 'r+');
  103. fwrite(\$this->$clo, \$row[0]);
  104. rewind(\$this->$clo);
  105. } else {
  106. \$this->$clo = null;
  107. }";
  108. } elseif ($col->isPhpPrimitiveType()) {
  109. $script .= "
  110. \$this->$clo = (\$row[0] !== null) ? (".$col->getPhpType().") \$row[0] : null;";
  111. } elseif ($col->isPhpObjectType()) {
  112. $script .= "
  113. \$this->$clo = (\$row[0] !== null) ? new ".$col->getPhpType()."(\$row[0]) : null;";
  114. } else {
  115. $script .= "
  116. \$this->$clo = \$row[0];";
  117. }
  118. $script .= "
  119. \$this->".$clo."_isLoaded = true;
  120. } catch (Exception \$e) {
  121. throw new PropelException(\"Error loading value for [$clo] column on demand.\", \$e);
  122. }";
  123. }
  124. /**
  125. * Adds the function close for the lazy loader
  126. * @param string &$script The script will be modified in this method.
  127. * @param Column $col The current column.
  128. * @see addLazyLoader()
  129. **/
  130. protected function addLazyLoaderClose(&$script, Column $col) {
  131. $script .= "
  132. }";
  133. } // addLazyLoader()
  134. /**
  135. * Adds the buildPkeyCriteria method
  136. * @param string &$script The script will be modified in this method.
  137. **/
  138. protected function addBuildPkeyCriteria(&$script) {
  139. $this->addBuildPkeyCriteriaComment($script);
  140. $this->addBuildPkeyCriteriaOpen($script);
  141. $this->addBuildPkeyCriteriaBody($script);
  142. $this->addBuildPkeyCriteriaClose($script);
  143. }
  144. /**
  145. * Adds the comment for the buildPkeyCriteria method
  146. * @param string &$script The script will be modified in this method.
  147. * @see addBuildPkeyCriteria()
  148. **/
  149. protected function addBuildPkeyCriteriaComment(&$script) {
  150. $script .= "
  151. /**
  152. * Builds a Criteria object containing the primary key for this object.
  153. *
  154. * Unlike buildCriteria() this method includes the primary key values regardless
  155. * of whether or not they have been modified.
  156. *
  157. * @return Criteria The Criteria object containing value(s) for primary key(s).
  158. */";
  159. }
  160. /**
  161. * Adds the function declaration for the buildPkeyCriteria method
  162. * @param string &$script The script will be modified in this method.
  163. * @see addBuildPkeyCriteria()
  164. **/
  165. protected function addBuildPkeyCriteriaOpen(&$script) {
  166. $script .= "
  167. public function buildPkeyCriteria()
  168. {";
  169. }
  170. /**
  171. * Adds the function body for the buildPkeyCriteria method
  172. * @param string &$script The script will be modified in this method.
  173. * @see addBuildPkeyCriteria()
  174. **/
  175. protected function addBuildPkeyCriteriaBody(&$script) {
  176. $script .= "
  177. \$criteria = new Criteria(".$this->getPeerClassname()."::DATABASE_NAME);";
  178. foreach ($this->getTable()->getColumns() as $col) {
  179. $clo = strtolower($col->getName());
  180. if ($col->isPrimaryKey()) {
  181. $script .= "
  182. \$criteria->add(".$this->getColumnConstant($col).", \$this->$clo);";
  183. }
  184. }
  185. }
  186. /**
  187. * Adds the function close for the buildPkeyCriteria method
  188. * @param string &$script The script will be modified in this method.
  189. * @see addBuildPkeyCriteria()
  190. **/
  191. protected function addBuildPkeyCriteriaClose(&$script) {
  192. $script .= "
  193. return \$criteria;
  194. }
  195. ";
  196. }
  197. /**
  198. * Adds the buildCriteria method
  199. * @param string &$script The script will be modified in this method.
  200. **/
  201. protected function addBuildCriteria(&$script)
  202. {
  203. $this->addBuildCriteriaComment($script);
  204. $this->addBuildCriteriaOpen($script);
  205. $this->addBuildCriteriaBody($script);
  206. $this->addBuildCriteriaClose($script);
  207. }
  208. /**
  209. * Adds comment for the buildCriteria method
  210. * @param string &$script The script will be modified in this method.
  211. * @see addBuildCriteria()
  212. **/
  213. protected function addBuildCriteriaComment(&$script) {
  214. $script .= "
  215. /**
  216. * Build a Criteria object containing the values of all modified columns in this object.
  217. *
  218. * @return Criteria The Criteria object containing all modified values.
  219. */";
  220. }
  221. /**
  222. * Adds the function declaration of the buildCriteria method
  223. * @param string &$script The script will be modified in this method.
  224. * @see addBuildCriteria()
  225. **/
  226. protected function addBuildCriteriaOpen(&$script) {
  227. $script .= "
  228. public function buildCriteria()
  229. {";
  230. }
  231. /**
  232. * Adds the function body of the buildCriteria method
  233. * @param string &$script The script will be modified in this method.
  234. * @see addBuildCriteria()
  235. **/
  236. protected function addBuildCriteriaBody(&$script) {
  237. $script .= "
  238. \$criteria = new Criteria(".$this->getPeerClassname()."::DATABASE_NAME);
  239. ";
  240. foreach ($this->getTable()->getColumns() as $col) {
  241. $clo = strtolower($col->getName());
  242. $script .= "
  243. if (\$this->isColumnModified(".$this->getColumnConstant($col).")) \$criteria->add(".$this->getColumnConstant($col).", \$this->$clo);";
  244. }
  245. }
  246. /**
  247. * Adds the function close of the buildCriteria method
  248. * @param string &$script The script will be modified in this method.
  249. * @see addBuildCriteria()
  250. **/
  251. protected function addBuildCriteriaClose(&$script) {
  252. $script .= "
  253. return \$criteria;
  254. }
  255. ";
  256. }
  257. /**
  258. * Adds the function body for the delete function
  259. * @param string &$script The script will be modified in this method.
  260. * @see addDelete()
  261. **/
  262. protected function addDeleteBody(&$script) {
  263. $script .= "
  264. if (\$this->isDeleted()) {
  265. throw new PropelException(\"This object has already been deleted.\");
  266. }
  267. if (\$con === null) {
  268. \$con = Propel::getConnection(".$this->getPeerClassname()."::DATABASE_NAME, Propel::CONNECTION_WRITE);
  269. }
  270. \$con->beginTransaction();
  271. try {";
  272. if($this->getGeneratorConfig()->getBuildProperty('addHooks')) {
  273. $script .= "
  274. \$ret = \$this->preDelete(\$con);";
  275. // apply behaviors
  276. $this->applyBehaviorModifier('preDelete', $script, " ");
  277. $script .= "
  278. if (\$ret) {
  279. ".$this->getPeerClassname()."::doDelete(\$this, \$con);
  280. \$this->postDelete(\$con);";
  281. // apply behaviors
  282. $this->applyBehaviorModifier('postDelete', $script, " ");
  283. $script .= "
  284. \$con->commit();
  285. \$this->setDeleted(true);
  286. } else {
  287. \$con->commit();
  288. }";
  289. } else {
  290. // apply behaviors
  291. $this->applyBehaviorModifier('preDelete', $script, " ");
  292. $script .= "
  293. ".$this->getPeerClassname()."::doDelete(\$this, \$con);";
  294. // apply behaviors
  295. $this->applyBehaviorModifier('postDelete', $script, " ");
  296. $script .= "
  297. \$con->commit();
  298. \$this->setDeleted(true);";
  299. }
  300. $script .= "
  301. } catch (PropelException \$e) {
  302. \$con->rollBack();
  303. throw \$e;
  304. }";
  305. }
  306. /**
  307. * Adds a reload() method to re-fetch the data for this object from the database.
  308. * @param string &$script The script will be modified in this method.
  309. */
  310. protected function addReload(&$script)
  311. {
  312. $table = $this->getTable();
  313. $script .= "
  314. /**
  315. * Reloads this object from datastore based on primary key and (optionally) resets all associated objects.
  316. *
  317. * This will only work if the object has been saved and has a valid primary key set.
  318. *
  319. * @param boolean \$deep (optional) Whether to also de-associated any related objects.
  320. * @param PropelPDO \$con (optional) The PropelPDO connection to use.
  321. * @return void
  322. * @throws PropelException - if this object is deleted, unsaved or doesn't have pk match in db
  323. */
  324. public function reload(\$deep = false, PropelPDO \$con = null)
  325. {
  326. if (\$this->isDeleted()) {
  327. throw new PropelException(\"Cannot reload a deleted object.\");
  328. }
  329. if (\$this->isNew()) {
  330. throw new PropelException(\"Cannot reload an unsaved object.\");
  331. }
  332. if (\$con === null) {
  333. \$con = Propel::getConnection(".$this->getPeerClassname()."::DATABASE_NAME, Propel::CONNECTION_READ);
  334. }
  335. // We don't need to alter the object instance pool; we're just modifying this instance
  336. // already in the pool.
  337. \$stmt = ".$this->getPeerClassname()."::doSelectStmt(\$this->buildPkeyCriteria(), \$con);
  338. \$row = \$stmt->fetch(PDO::FETCH_NUM);
  339. \$stmt->closeCursor();
  340. if (!\$row) {
  341. throw new PropelException('Cannot find matching row in the database to reload object values.');
  342. }
  343. \$this->hydrate(\$row, 0, true); // rehydrate
  344. ";
  345. // support for lazy load columns
  346. foreach ($table->getColumns() as $col) {
  347. if ($col->isLazyLoad()) {
  348. $clo = strtolower($col->getName());
  349. $script .= "
  350. // Reset the $clo lazy-load column
  351. \$this->" . $clo . " = null;
  352. \$this->".$clo."_isLoaded = false;
  353. ";
  354. }
  355. }
  356. $script .= "
  357. if (\$deep) { // also de-associate any related objects?
  358. ";
  359. foreach ($table->getForeignKeys() as $fk) {
  360. $varName = $this->getFKVarName($fk);
  361. $script .= "
  362. \$this->".$varName." = null;";
  363. }
  364. foreach ($table->getReferrers() as $refFK) {
  365. if ($refFK->isLocalPrimaryKey()) {
  366. $script .= "
  367. \$this->".$this->getPKRefFKVarName($refFK)." = null;
  368. ";
  369. } else {
  370. $script .= "
  371. \$this->".$this->getRefFKCollVarName($refFK)." = null;
  372. \$this->".$this->getRefFKLastCriteriaVarName($refFK)." = null;
  373. ";
  374. }
  375. }
  376. $script .= "
  377. } // if (deep)
  378. }
  379. ";
  380. } // addReload()
  381. /**
  382. * Gets variable name for the Criteria which was used to fetch the objects which
  383. * referencing current table by specified foreign key.
  384. * @param ForeignKey $fk
  385. * @return string
  386. */
  387. protected function getRefFKLastCriteriaVarName(ForeignKey $fk)
  388. {
  389. return 'last' . $this->getRefFKPhpNameAffix($fk, $plural = false) . 'Criteria';
  390. }
  391. /**
  392. * Adds the accessor (getter) method for getting an fkey related object.
  393. * @param string &$script The script will be modified in this method.
  394. */
  395. protected function addFKAccessor(&$script, ForeignKey $fk)
  396. {
  397. $table = $this->getTable();
  398. $varName = $this->getFKVarName($fk);
  399. $fkPeerBuilder = $this->getNewPeerBuilder($this->getForeignTable($fk));
  400. $fkObjectBuilder = $this->getNewObjectBuilder($this->getForeignTable($fk))->getStubObjectBuilder();
  401. $className = $fkObjectBuilder->getClassname(); // get the Classname that has maybe a prefix
  402. $and = "";
  403. $conditional = "";
  404. $localColumns = array(); // foreign key local attributes names
  405. $argmap = array(); // foreign -> local mapping
  406. // If the related columns are a primary key on the foreign table
  407. // then use retrieveByPk() instead of doSelect() to take advantage
  408. // of instance pooling
  409. $useRetrieveByPk = $fk->isForeignPrimaryKey();
  410. foreach ($fk->getLocalColumns() as $columnName) {
  411. $lfmap = $fk->getLocalForeignMapping();
  412. $localColumn = $table->getColumn($columnName);
  413. $foreignColumn = $fk->getForeignTable()->getColumn($lfmap[$columnName]);
  414. $column = $table->getColumn($columnName);
  415. $cptype = $column->getPhpType();
  416. $clo = strtolower($column->getName());
  417. $localColumns[$foreignColumn->getPosition()] = '$this->'.$clo;
  418. if ($cptype == "integer" || $cptype == "float" || $cptype == "double") {
  419. $conditional .= $and . "\$this->". $clo ." != 0";
  420. } elseif ($cptype == "string") {
  421. $conditional .= $and . "(\$this->" . $clo ." !== \"\" && \$this->".$clo." !== null)";
  422. } else {
  423. $conditional .= $and . "\$this->" . $clo ." !== null";
  424. }
  425. $argmap[] = array('foreign' => $foreignColumn, 'local' => $localColumn);
  426. $and = " && ";
  427. }
  428. ksort($localColumns); // restoring the order of the foreign PK
  429. $localColumns = count($localColumns) > 1 ?
  430. ('array('.implode(', ', $localColumns).')') : reset($localColumns);
  431. // If the related column is a primary kay and if it's a simple association,
  432. // The use retrieveByPk() instead of doSelect() to take advantage of instance pooling
  433. $useRetrieveByPk = count($argmap) == 1 && $argmap[0]['foreign']->isPrimaryKey();
  434. $script .= "
  435. /**
  436. * Get the associated $className object
  437. *
  438. * @param PropelPDO Optional Connection object.
  439. * @return $className The associated $className object.
  440. * @throws PropelException
  441. */
  442. public function get".$this->getFKPhpNameAffix($fk, $plural = false)."(PropelPDO \$con = null)
  443. {";
  444. $script .= "
  445. if (\$this->$varName === null && ($conditional)) {";
  446. if ($useRetrieveByPk) {
  447. $script .= "
  448. \$this->$varName = ".$fkPeerBuilder->getPeerClassname()."::retrieveByPk($localColumns, \$con);";
  449. } else {
  450. $script .= "
  451. \$c = new Criteria(".$fkPeerBuilder->getPeerClassname()."::DATABASE_NAME);";
  452. foreach ($argmap as $el) {
  453. $fcol = $el['foreign'];
  454. $lcol = $el['local'];
  455. $clo = strtolower($lcol->getName());
  456. $script .= "
  457. \$c->add(".$fkPeerBuilder->getColumnConstant($fcol).", \$this->".$clo.");";
  458. }
  459. $script .= "
  460. \$this->$varName = ".$fkPeerBuilder->getPeerClassname()."::doSelectOne(\$c, \$con);";
  461. }
  462. if ($fk->isLocalPrimaryKey()) {
  463. $script .= "
  464. // Because this foreign key represents a one-to-one relationship, we will create a bi-directional association.
  465. \$this->{$varName}->set".$this->getRefFKPhpNameAffix($fk, $plural = false)."(\$this);";
  466. } else {
  467. $script .= "
  468. /* The following can be used additionally to
  469. guarantee the related object contains a reference
  470. to this object. This level of coupling may, however, be
  471. undesirable since it could result in an only partially populated collection
  472. in the referenced object.
  473. \$this->{$varName}->add".$this->getRefFKPhpNameAffix($fk, $plural = true)."(\$this);
  474. */";
  475. }
  476. $script .= "
  477. }
  478. return \$this->$varName;
  479. }
  480. ";
  481. } // addFKAccessor
  482. /**
  483. * Adds the method that fetches fkey-related (referencing) objects but also joins in data from another table.
  484. * @param string &$script The script will be modified in this method.
  485. */
  486. protected function addRefFKGetJoinMethods(&$script, ForeignKey $refFK)
  487. {
  488. $table = $this->getTable();
  489. $tblFK = $refFK->getTable();
  490. $join_behavior = $this->getGeneratorConfig()->getBuildProperty('useLeftJoinsInDoJoinMethods') ? 'Criteria::LEFT_JOIN' : 'Criteria::INNER_JOIN';
  491. $peerClassname = $this->getStubPeerBuilder()->getClassname();
  492. $relCol = $this->getRefFKPhpNameAffix($refFK, $plural=true);
  493. $collName = $this->getRefFKCollVarName($refFK);
  494. $lastCriteriaName = $this->getRefFKLastCriteriaVarName($refFK);
  495. $fkPeerBuilder = $this->getNewPeerBuilder($tblFK);
  496. $lastTable = "";
  497. foreach ($tblFK->getForeignKeys() as $fk2) {
  498. $tblFK2 = $this->getForeignTable($fk2);
  499. $doJoinGet = !$tblFK2->isForReferenceOnly();
  500. // it doesn't make sense to join in rows from the curent table, since we are fetching
  501. // objects related to *this* table (i.e. the joined rows will all be the same row as current object)
  502. if ($this->getTable()->getPhpName() == $tblFK2->getPhpName()) {
  503. $doJoinGet = false;
  504. }
  505. $relCol2 = $this->getFKPhpNameAffix($fk2, $plural = false);
  506. if ( $this->getRelatedBySuffix($refFK) != "" &&
  507. ($this->getRelatedBySuffix($refFK) == $this->getRelatedBySuffix($fk2))) {
  508. $doJoinGet = false;
  509. }
  510. if ($doJoinGet) {
  511. $script .= "
  512. /**
  513. * If this collection has already been initialized with
  514. * an identical criteria, it returns the collection.
  515. * Otherwise if this ".$table->getPhpName()." is new, it will return
  516. * an empty collection; or if this ".$table->getPhpName()." has previously
  517. * been saved, it will retrieve related $relCol from storage.
  518. *
  519. * This method is protected by default in order to keep the public
  520. * api reasonable. You can provide public methods for those you
  521. * actually need in ".$table->getPhpName().".
  522. */
  523. public function get".$relCol."Join".$relCol2."(\$criteria = null, \$con = null, \$join_behavior = $join_behavior)
  524. {";
  525. $script .= "
  526. if (\$criteria === null) {
  527. \$criteria = new Criteria($peerClassname::DATABASE_NAME);
  528. }
  529. elseif (\$criteria instanceof Criteria)
  530. {
  531. \$criteria = clone \$criteria;
  532. }
  533. if (\$this->$collName === null) {
  534. if (\$this->isNew()) {
  535. \$this->$collName = array();
  536. } else {
  537. ";
  538. foreach ($refFK->getForeignColumns() as $columnName) {
  539. $column = $table->getColumn($columnName);
  540. $flMap = $refFK->getForeignLocalMapping();
  541. $colFKName = $flMap[$columnName];
  542. $colFK = $tblFK->getColumn($colFKName);
  543. if ($colFK === null) {
  544. throw new EngineException("Column $colFKName not found in " . $tblFK->getName());
  545. }
  546. $clo = strtolower($column->getName());
  547. $script .= "
  548. \$criteria->add(".$fkPeerBuilder->getColumnConstant($colFK).", \$this->$clo);
  549. ";
  550. } // end foreach ($fk->getForeignColumns()
  551. $script .= "
  552. \$this->$collName = ".$fkPeerBuilder->getPeerClassname()."::doSelectJoin$relCol2(\$criteria, \$con, \$join_behavior);
  553. }
  554. } else {
  555. // the following code is to determine if a new query is
  556. // called for. If the criteria is the same as the last
  557. // one, just return the collection.
  558. ";
  559. foreach ($refFK->getForeignColumns() as $columnName) {
  560. $column = $table->getColumn($columnName);
  561. $flMap = $refFK->getForeignLocalMapping();
  562. $colFKName = $flMap[$columnName];
  563. $colFK = $tblFK->getColumn($colFKName);
  564. $clo = strtolower($column->getName());
  565. $script .= "
  566. \$criteria->add(".$fkPeerBuilder->getColumnConstant($colFK).", \$this->$clo);
  567. ";
  568. } /* end foreach ($fk->getForeignColumns() */
  569. $script .= "
  570. if (!isset(\$this->$lastCriteriaName) || !\$this->".$lastCriteriaName."->equals(\$criteria)) {
  571. \$this->$collName = ".$fkPeerBuilder->getPeerClassname()."::doSelectJoin$relCol2(\$criteria, \$con, \$join_behavior);
  572. }
  573. }
  574. \$this->$lastCriteriaName = \$criteria;
  575. return \$this->$collName;
  576. }
  577. ";
  578. } /* end if ($doJoinGet) */
  579. } /* end foreach ($tblFK->getForeignKeys() as $fk2) { */
  580. } // function
  581. /**
  582. * Adds the method that initializes the referrer fkey collection.
  583. * @param string &$script The script will be modified in this method.
  584. */
  585. protected function addRefFKInit(&$script, ForeignKey $refFK) {
  586. $relCol = $this->getRefFKPhpNameAffix($refFK, $plural = true);
  587. $collName = $this->getRefFKCollVarName($refFK);
  588. $script .= "
  589. /**
  590. * Initializes the $collName collection (array).
  591. *
  592. * By default this just sets the $collName collection to an empty array (like clear$collName());
  593. * however, you may wish to override this method in your stub class to provide setting appropriate
  594. * to your application -- for example, setting the initial array to the values stored in database.
  595. *
  596. * @return void
  597. */
  598. public function init$relCol()
  599. {
  600. \$this->$collName = array();
  601. }
  602. ";
  603. } // addRefererInit()
  604. /**
  605. * Adds the method that adds an object into the referrer fkey collection.
  606. * @param string &$script The script will be modified in this method.
  607. */
  608. protected function addRefFKAdd(&$script, ForeignKey $refFK)
  609. {
  610. $tblFK = $refFK->getTable();
  611. $joinedTableObjectBuilder = $this->getNewObjectBuilder($refFK->getTable());
  612. $className = $joinedTableObjectBuilder->getObjectClassname();
  613. $collName = $this->getRefFKCollVarName($refFK);
  614. $script .= "
  615. /**
  616. * Method called to associate a $className object to this object
  617. * through the $className foreign key attribute.
  618. *
  619. * @param $className \$l $className
  620. * @return void
  621. * @throws PropelException
  622. */
  623. public function add".$this->getRefFKPhpNameAffix($refFK, $plural = false)."($className \$l)
  624. {
  625. if (\$this->$collName === null) {
  626. \$this->init".$this->getRefFKPhpNameAffix($refFK, $plural = true)."();
  627. }
  628. if (!in_array(\$l, \$this->$collName, true)) { // only add it if the **same** object is not already associated
  629. array_push(\$this->$collName, \$l);
  630. \$l->set".$this->getFKPhpNameAffix($refFK, $plural = false)."(\$this);
  631. }
  632. }
  633. ";
  634. } // addRefererAdd
  635. /**
  636. * Adds the method that returns the size of the referrer fkey collection.
  637. * @param string &$script The script will be modified in this method.
  638. */
  639. protected function addRefFKCount(&$script, ForeignKey $refFK)
  640. {
  641. $table = $this->getTable();
  642. $tblFK = $refFK->getTable();
  643. $peerClassname = $this->getStubPeerBuilder()->getClassname();
  644. $fkPeerBuilder = $this->getNewPeerBuilder($refFK->getTable());
  645. $relCol = $this->getRefFKPhpNameAffix($refFK, $plural = true);
  646. $collName = $this->getRefFKCollVarName($refFK);
  647. $lastCriteriaName = $this->getRefFKLastCriteriaVarName($refFK);
  648. $className = $fkPeerBuilder->getObjectClassname();
  649. $script .= "
  650. /**
  651. * Returns the number of related $className objects.
  652. *
  653. * @param Criteria \$criteria
  654. * @param boolean \$distinct
  655. * @param PropelPDO \$con
  656. * @return int Count of related $className objects.
  657. * @throws PropelException
  658. */
  659. public function count$relCol(Criteria \$criteria = null, \$distinct = false, PropelPDO \$con = null)
  660. {";
  661. $script .= "
  662. if (\$criteria === null) {
  663. \$criteria = new Criteria($peerClassname::DATABASE_NAME);
  664. } else {
  665. \$criteria = clone \$criteria;
  666. }
  667. if (\$distinct) {
  668. \$criteria->setDistinct();
  669. }
  670. \$count = null;
  671. if (\$this->$collName === null) {
  672. if (\$this->isNew()) {
  673. \$count = 0;
  674. } else {
  675. ";
  676. foreach ($refFK->getLocalColumns() as $colFKName) {
  677. // $colFKName is local to the referring table (i.e. foreign to this table)
  678. $lfmap = $refFK->getLocalForeignMapping();
  679. $localColumn = $this->getTable()->getColumn($lfmap[$colFKName]);
  680. $colFK = $refFK->getTable()->getColumn($colFKName);
  681. $clo = strtolower($localColumn->getName());
  682. $script .= "
  683. \$criteria->add(".$fkPeerBuilder->getColumnConstant($colFK).", \$this->$clo);
  684. ";
  685. } // end foreach ($fk->getForeignColumns()
  686. $script .= "
  687. \$count = ".$fkPeerBuilder->getPeerClassname()."::doCount(\$criteria, false, \$con);
  688. }
  689. } else {
  690. // criteria has no effect for a new object
  691. if (!\$this->isNew()) {
  692. // the following code is to determine if a new query is
  693. // called for. If the criteria is the same as the last
  694. // one, just return count of the collection.
  695. ";
  696. foreach ($refFK->getLocalColumns() as $colFKName) {
  697. // $colFKName is local to the referring table (i.e. foreign to this table)
  698. $lfmap = $refFK->getLocalForeignMapping();
  699. $localColumn = $this->getTable()->getColumn($lfmap[$colFKName]);
  700. $colFK = $refFK->getTable()->getColumn($colFKName);
  701. $clo = strtolower($localColumn->getName());
  702. $script .= "
  703. \$criteria->add(".$fkPeerBuilder->getColumnConstant($colFK).", \$this->$clo);
  704. ";
  705. } // foreach ($fk->getForeignColumns()
  706. $script .= "
  707. if (!isset(\$this->$lastCriteriaName) || !\$this->".$lastCriteriaName."->equals(\$criteria)) {
  708. \$count = ".$fkPeerBuilder->getPeerClassname()."::doCount(\$criteria, false, \$con);
  709. } else {
  710. \$count = count(\$this->$collName);
  711. }
  712. } else {
  713. \$count = count(\$this->$collName);
  714. }
  715. }
  716. return \$count;
  717. }
  718. ";
  719. } // addRefererCount
  720. /**
  721. * Adds the method that returns the referrer fkey collection.
  722. * @param string &$script The script will be modified in this method.
  723. */
  724. protected function addRefFKGet(&$script, ForeignKey $refFK)
  725. {
  726. $table = $this->getTable();
  727. $tblFK = $refFK->getTable();
  728. $peerClassname = $this->getStubPeerBuilder()->getClassname();
  729. $fkPeerBuilder = $this->getNewPeerBuilder($refFK->getTable());
  730. $relCol = $this->getRefFKPhpNameAffix($refFK, $plural = true);
  731. $collName = $this->getRefFKCollVarName($refFK);
  732. $lastCriteriaName = $this->getRefFKLastCriteriaVarName($refFK);
  733. $className = $fkPeerBuilder->getObjectClassname();
  734. $script .= "
  735. /**
  736. * Gets an array of $className objects which contain a foreign key that references this object.
  737. *
  738. * If this collection has already been initialized with an identical Criteria, it returns the collection.
  739. * Otherwise if this ".$this->getObjectClassname()." has previously been saved, it will retrieve
  740. * related $relCol from storage. If this ".$this->getObjectClassname()." is new, it will return
  741. * an empty collection or the current collection, the criteria is ignored on a new object.
  742. *
  743. * @param PropelPDO \$con
  744. * @param Criteria \$criteria
  745. * @return array {$className}[]
  746. * @throws PropelException
  747. */
  748. public function get$relCol(\$criteria = null, PropelPDO \$con = null)
  749. {";
  750. $script .= "
  751. if (\$criteria === null) {
  752. \$criteria = new Criteria($peerClassname::DATABASE_NAME);
  753. }
  754. elseif (\$criteria instanceof Criteria)
  755. {
  756. \$criteria = clone \$criteria;
  757. }
  758. if (\$this->$collName === null) {
  759. if (\$this->isNew()) {
  760. \$this->$collName = array();
  761. } else {
  762. ";
  763. foreach ($refFK->getLocalColumns() as $colFKName) {
  764. // $colFKName is local to the referring table (i.e. foreign to this table)
  765. $lfmap = $refFK->getLocalForeignMapping();
  766. $localColumn = $this->getTable()->getColumn($lfmap[$colFKName]);
  767. $colFK = $refFK->getTable()->getColumn($colFKName);
  768. $clo = strtolower($localColumn->getName());
  769. $script .= "
  770. \$criteria->add(".$fkPeerBuilder->getColumnConstant($colFK).", \$this->$clo);
  771. ";
  772. } // end foreach ($fk->getForeignColumns()
  773. $script .= "
  774. ".$fkPeerBuilder->getPeerClassname()."::addSelectColumns(\$criteria);
  775. \$this->$collName = ".$fkPeerBuilder->getPeerClassname()."::doSelect(\$criteria, \$con);
  776. }
  777. } else {
  778. // criteria has no effect for a new object
  779. if (!\$this->isNew()) {
  780. // the following code is to determine if a new query is
  781. // called for. If the criteria is the same as the last
  782. // one, just return the collection.
  783. ";
  784. foreach ($refFK->getLocalColumns() as $colFKName) {
  785. // $colFKName is local to the referring table (i.e. foreign to this table)
  786. $lfmap = $refFK->getLocalForeignMapping();
  787. $localColumn = $this->getTable()->getColumn($lfmap[$colFKName]);
  788. $colFK = $refFK->getTable()->getColumn($colFKName);
  789. $clo = strtolower($localColumn->getName());
  790. $script .= "
  791. \$criteria->add(".$fkPeerBuilder->getColumnConstant($colFK).", \$this->$clo);
  792. ";
  793. } // foreach ($fk->getForeignColumns()
  794. $script .= "
  795. ".$fkPeerBuilder->getPeerClassname()."::addSelectColumns(\$criteria);
  796. if (!isset(\$this->$lastCriteriaName) || !\$this->".$lastCriteriaName."->equals(\$criteria)) {
  797. \$this->$collName = ".$fkPeerBuilder->getPeerClassname()."::doSelect(\$criteria, \$con);
  798. }
  799. }
  800. }
  801. \$this->$lastCriteriaName = \$criteria;
  802. return \$this->$collName;
  803. }
  804. ";
  805. } // addRefererGet()
  806. /**
  807. * Adds the method that gets a one-to-one related referrer fkey.
  808. * This is for one-to-one relationship special case.
  809. * @param string &$script The script will be modified in this method.
  810. */
  811. protected function addPKRefFKGet(&$script, ForeignKey $refFK)
  812. {
  813. $table = $this->getTable();
  814. $tblFK = $refFK->getTable();
  815. $joinedTableObjectBuilder = $this->getNewObjectBuilder($refFK->getTable());
  816. $joinedTablePeerBuilder = $this->getNewObjectBuilder($refFK->getTable());
  817. $className = $joinedTableObjectBuilder->getObjectClassname();
  818. $varName = $this->getPKRefFKVarName($refFK);
  819. $script .= "
  820. /**
  821. * Gets a single $className object, which is related to this object by a one-to-one relationship.
  822. *
  823. * @param PropelPDO \$con
  824. * @return $className
  825. * @throws PropelException
  826. */
  827. public function get".$this->getRefFKPhpNameAffix($refFK, $plural = false)."(PropelPDO \$con = null)
  828. {
  829. ";
  830. $script .= "
  831. if (\$this->$varName === null && !\$this->isNew()) {";
  832. $lfmap = $refFK->getLocalForeignMapping();
  833. // remember: this object represents the foreign table,
  834. // so we need foreign columns of the reffk to know the local columns
  835. // that we need to set :)
  836. $localcols = $refFK->getForeignColumns();
  837. // we know that at least every column in the primary key of the foreign table
  838. // is represented in this foreign key
  839. $params = array();
  840. foreach ($tblFK->getPrimaryKey() as $col) {
  841. $localColumn = $table->getColumn($lfmap[$col->getName()]);
  842. $clo = strtolower($localColumn->getName());
  843. $params[] = "\$this->$clo";
  844. }
  845. $script .= "
  846. \$this->$varName = ".$joinedTableObjectBuilder->getPeerClassname()."::retrieveByPK(".implode(", ", $params).", \$con);
  847. }
  848. return \$this->$varName;
  849. }
  850. ";
  851. } // addPKRefFKGet()
  852. } // PHP5ObjectBuilder