PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/package/app/app/symfony/vendor/propel-generator/classes/propel/engine/builder/om/php5/PHP5BasicPeerBuilder.php

https://bitbucket.org/pandaos/kaltura
PHP | 1472 lines | 812 code | 146 blank | 514 comment | 78 complexity | b8bb3f9675eac159d13d81d973776b3c MD5 | raw file
Possible License(s): AGPL-3.0, GPL-3.0, BSD-3-Clause, LGPL-2.1, GPL-2.0, LGPL-3.0, JSON, MPL-2.0-no-copyleft-exception, Apache-2.0
  1. <?php
  2. /*
  3. * $Id: PHP5BasicPeerBuilder.php 358 2006-04-18 17:40:40Z oliver $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information please see
  19. * <http://propel.phpdb.org>.
  20. */
  21. require_once 'propel/engine/builder/om/PeerBuilder.php';
  22. /**
  23. * Generates a PHP5 base Peer class for user object model (OM).
  24. *
  25. * This class produces the base peer class (e.g. BaseMyPeer) which contains all
  26. * the custom-built query and manipulator methods.
  27. *
  28. * This class replaces the Peer.tpl, with the intent of being easier for users
  29. * to customize (through extending & overriding).
  30. *
  31. * @author Hans Lellelid <hans@xmpl.org>
  32. * @package propel.engine.builder.om.php5
  33. */
  34. class PHP5BasicPeerBuilder extends PeerBuilder {
  35. /**
  36. * Returns the name of the current class being built.
  37. * @return string
  38. */
  39. public function getClassname()
  40. {
  41. return $this->getBuildProperty('basePrefix') . $this->getStubPeerBuilder()->getClassname();
  42. }
  43. /**
  44. * Gets the package for the [base] peer classes.
  45. * @return string
  46. */
  47. public function getPackage()
  48. {
  49. return parent::getPackage() . ".om";
  50. }
  51. /**
  52. * Adds the include() statements for files that this class depends on or utilizes.
  53. * @param string &$script The script will be modified in this method.
  54. */
  55. protected function addIncludes(&$script) {
  56. $table = $this->getTable();
  57. $basePeerFile = $this->getFilePath($this->basePeerClass);
  58. $objectFile = $this->getStubObjectBuilder()->getClassFilePath();
  59. $script .= "
  60. require_once '$basePeerFile';
  61. // The object class -- needed for instanceof checks in this class.
  62. // actual class may be a subclass -- as returned by ".$this->getPeerClassname()."::getOMClass()
  63. include_once '$objectFile';";
  64. $script .= "
  65. ";
  66. } // addIncludes()
  67. /**
  68. * Adds class phpdoc comment and openning of class.
  69. * @param string &$script The script will be modified in this method.
  70. */
  71. protected function addClassOpen(&$script) {
  72. $tableName = $this->getTable()->getName();
  73. $tableDesc = $this->getTable()->getDescription();
  74. $script .= "
  75. /**
  76. * Base static class for performing query and update operations on the '$tableName' table.
  77. *
  78. * $tableDesc
  79. *";
  80. if ($this->getBuildProperty('addTimeStamp')) {
  81. $now = strftime('%c');
  82. $script .= "
  83. * This class was autogenerated by Propel on:
  84. *
  85. * $now
  86. *";
  87. }
  88. $script .= "
  89. * @package ".$this->getPackage()."
  90. */
  91. abstract class ".$this->getClassname()." {
  92. ";
  93. }
  94. /**
  95. * Closes class.
  96. * Adds closing brace at end of class and the static map builder registration code.
  97. * @param string &$script The script will be modified in this method.
  98. * @see addStaticMapBuilderRegistration()
  99. */
  100. protected function addClassClose(&$script)
  101. {
  102. $script .= "
  103. } // " . $this->getClassname() . "
  104. ";
  105. $this->addStaticMapBuilderRegistration($script);
  106. }
  107. /**
  108. * Adds the static map builder registraction code.
  109. * @param string &$script The script will be modified in this method.
  110. */
  111. protected function addStaticMapBuilderRegistration(&$script)
  112. {
  113. $table = $this->getTable();
  114. $mapBuilderFile = $this->getMapBuilderBuilder()->getClassFilePath();
  115. $script .= "
  116. // static code to register the map builder for this Peer with the main Propel class
  117. if (Propel::isInit()) {
  118. // the MapBuilder classes register themselves with Propel during initialization
  119. // so we need to load them here.
  120. try {
  121. ".$this->getClassname()."::getMapBuilder();
  122. } catch (Exception \$e) {
  123. Propel::log('Could not initialize Peer: ' . \$e->getMessage(), Propel::LOG_ERR);
  124. }
  125. } else {
  126. // even if Propel is not yet initialized, the map builder class can be registered
  127. // now and then it will be loaded when Propel initializes.
  128. require_once '$mapBuilderFile';
  129. Propel::registerMapBuilder('".$this->getMapBuilderBuilder()->getClasspath()."');
  130. }
  131. ";
  132. }
  133. /**
  134. * Adds constant and variable declarations that go at the top of the class.
  135. * @param string &$script The script will be modified in this method.
  136. * @see addColumnNameConstants()
  137. */
  138. protected function addConstantsAndAttributes(&$script)
  139. {
  140. $tableName = $this->getTable()->getName();
  141. $dbName = $this->getDatabase()->getName();
  142. $script .= "
  143. /** the default database name for this class */
  144. const DATABASE_NAME = '$dbName';
  145. /** the table name for this class */
  146. const TABLE_NAME = '$tableName';
  147. /** A class that can be returned by this peer. */
  148. const CLASS_DEFAULT = '".$this->getStubObjectBuilder()->getClasspath()."';
  149. /** The total number of columns. */
  150. const NUM_COLUMNS = ".$this->getTable()->getNumColumns().";
  151. /** The number of lazy-loaded columns. */
  152. const NUM_LAZY_LOAD_COLUMNS = ".$this->getTable()->getNumLazyLoadColumns().";
  153. ";
  154. $this->addColumnNameConstants($script);
  155. $this->addInheritanceColumnConstants($script);
  156. $script .= "
  157. /** The PHP to DB Name Mapping */
  158. private static \$phpNameMap = null;
  159. ";
  160. $this->addFieldNamesAttribute($script);
  161. $this->addFieldKeysAttribute($script);
  162. }
  163. /**
  164. * Adds the COLUMN_NAME contants to the class definition.
  165. * @param string &$script The script will be modified in this method.
  166. */
  167. protected function addColumnNameConstants(&$script)
  168. {
  169. foreach ($this->getTable()->getColumns() as $col) {
  170. $script .= "
  171. /** the column name for the ".strtoupper($col->getName()) ." field */
  172. const ".$this->getColumnName($col) ." = '".$this->getTable()->getName().".".strtoupper($col->getName())."';
  173. ";
  174. } // foreach
  175. }
  176. protected function addFieldNamesAttribute(&$script)
  177. {
  178. $table = $this->getTable();
  179. $tableColumns = $table->getColumns();
  180. $tablePhpname = $table->getPhpName();
  181. $script .= "
  182. /**
  183. * holds an array of fieldnames
  184. *
  185. * first dimension keys are the type constants
  186. * e.g. self::\$fieldNames[self::TYPE_PHPNAME][0] = 'Id'
  187. */
  188. private static \$fieldNames = array (
  189. BasePeer::TYPE_PHPNAME => array (";
  190. foreach ($tableColumns as $col) {
  191. $script .= "'".$col->getPhpName()."', ";
  192. }
  193. $script .= "),
  194. BasePeer::TYPE_COLNAME => array (";
  195. foreach ($tableColumns as $col) {
  196. $script .= $this->getColumnConstant($col).", ";
  197. }
  198. $script .= "),
  199. BasePeer::TYPE_FIELDNAME => array (";
  200. foreach ($tableColumns as $col) {
  201. $script .= "'".$col->getName()."', ";
  202. }
  203. $script .= "),
  204. BasePeer::TYPE_NUM => array (";
  205. foreach ($tableColumns as $num => $col) {
  206. $script .= "$num, ";
  207. }
  208. $script .= ")
  209. );
  210. ";
  211. }
  212. protected function addFieldKeysAttribute(&$script)
  213. {
  214. $table = $this->getTable();
  215. $tableColumns = $table->getColumns();
  216. $tablePhpname = $table->getPhpName();
  217. $script .= "
  218. /**
  219. * holds an array of keys for quick access to the fieldnames array
  220. *
  221. * first dimension keys are the type constants
  222. * e.g. self::\$fieldNames[BasePeer::TYPE_PHPNAME]['Id'] = 0
  223. */
  224. private static \$fieldKeys = array (
  225. BasePeer::TYPE_PHPNAME => array (";
  226. foreach ($tableColumns as $num => $col) {
  227. $script .= "'".$col->getPhpName()."' => $num, ";
  228. }
  229. $script .= "),
  230. BasePeer::TYPE_COLNAME => array (";
  231. foreach ($tableColumns as $num => $col) {
  232. $script .= $this->getColumnConstant($col)." => $num, ";
  233. }
  234. $script .= "),
  235. BasePeer::TYPE_FIELDNAME => array (";
  236. foreach ($tableColumns as $num => $col) {
  237. $script .= "'".$col->getName()."' => $num, ";
  238. }
  239. $script .= "),
  240. BasePeer::TYPE_NUM => array (";
  241. foreach ($tableColumns as $num => $col) {
  242. $script .= "$num, ";
  243. }
  244. $script .= ")
  245. );
  246. ";
  247. } // addFielKeysAttribute
  248. protected function addGetFieldNames(&$script)
  249. {
  250. $script .= "
  251. /**
  252. * Returns an array of of field names.
  253. *
  254. * @param string \$type The type of fieldnames to return:
  255. * One of the class type constants TYPE_PHPNAME,
  256. * TYPE_COLNAME, TYPE_FIELDNAME, TYPE_NUM
  257. * @return array A list of field names
  258. */
  259. static public function getFieldNames(\$type = BasePeer::TYPE_PHPNAME)
  260. {
  261. if (!array_key_exists(\$type, self::\$fieldNames)) {
  262. throw new PropelException('Method getFieldNames() expects the parameter \$type to be one of the class constants TYPE_PHPNAME, TYPE_COLNAME, TYPE_FIELDNAME, TYPE_NUM. ' . \$type . ' was given.');
  263. }
  264. return self::\$fieldNames[\$type];
  265. }
  266. ";
  267. } // addGetFieldNames()
  268. protected function addTranslateFieldName(&$script)
  269. {
  270. $script .= "
  271. /**
  272. * Translates a fieldname to another type
  273. *
  274. * @param string \$name field name
  275. * @param string \$fromType One of the class type constants TYPE_PHPNAME,
  276. * TYPE_COLNAME, TYPE_FIELDNAME, TYPE_NUM
  277. * @param string \$toType One of the class type constants
  278. * @return string translated name of the field.
  279. */
  280. static public function translateFieldName(\$name, \$fromType, \$toType)
  281. {
  282. \$toNames = self::getFieldNames(\$toType);
  283. \$key = isset(self::\$fieldKeys[\$fromType][\$name]) ? self::\$fieldKeys[\$fromType][\$name] : null;
  284. if (\$key === null) {
  285. throw new PropelException(\"'\$name' could not be found in the field names of type '\$fromType'. These are: \" . print_r(self::\$fieldKeys[\$fromType], true));
  286. }
  287. return \$toNames[\$key];
  288. }
  289. ";
  290. } // addTranslateFieldName()
  291. /**
  292. * Adds the getMapBuilder() method.
  293. * @param string &$script The script will be modified in this method.
  294. */
  295. protected function addGetMapBuilder(&$script)
  296. {
  297. $script .= "
  298. /**
  299. * @return MapBuilder the map builder for this peer
  300. * @throws PropelException Any exceptions caught during processing will be
  301. * rethrown wrapped into a PropelException.
  302. */
  303. public static function getMapBuilder()
  304. {
  305. include_once '" . $this->getMapBuilderBuilder()->getClassFilePath()."';
  306. return ".$this->basePeerClassname."::getMapBuilder('". $this->getMapBuilderBuilder()->getClasspath() ."');
  307. }";
  308. }
  309. /**
  310. * Adds the getPhpNameMap() method.
  311. * @param string &$script The script will be modified in this method.
  312. * @todo Replace with static version (this can be built at build-time).
  313. */
  314. protected function addGetPhpNameMap(&$script)
  315. {
  316. $script .= "
  317. /**
  318. * Gets a map (hash) of PHP names to DB column names.
  319. *
  320. * @return array The PHP to DB name map for this peer
  321. * @throws PropelException Any exceptions caught during processing will be
  322. * rethrown wrapped into a PropelException.
  323. * @deprecated Use the getFieldNames() and translateFieldName() methods instead of this.
  324. */
  325. public static function getPhpNameMap()
  326. {
  327. if (self::\$phpNameMap === null) {
  328. \$map = ".$this->getTable()->getPhpName()."Peer::getTableMap();
  329. \$columns = \$map->getColumns();
  330. \$nameMap = array();
  331. foreach (\$columns as \$column) {
  332. \$nameMap[\$column->getPhpName()] = \$column->getColumnName();
  333. }
  334. self::\$phpNameMap = \$nameMap;
  335. }
  336. return self::\$phpNameMap;
  337. }";
  338. }
  339. /**
  340. * Adds the CLASSKEY_* and CLASSNAME_* constants used for inheritance.
  341. * @param string &$script The script will be modified in this method.
  342. */
  343. public function addInheritanceColumnConstants(&$script)
  344. {
  345. if ($this->getTable()->getChildrenColumn()) {
  346. $col = $this->getTable()->getChildrenColumn();
  347. $tfc = $this->getTable()->getPhpName();
  348. $cfc = $col->getPhpName();
  349. if ($col->isEnumeratedClasses()) {
  350. if ($col->isPrimitiveNumeric()) $quote = "";
  351. else $quote = '"';
  352. foreach ($col->getChildren() as $child) {
  353. $childBuilder = $this->getMultiExtendObjectBuilder();
  354. $childBuilder->setChild($child);
  355. $script .= "
  356. /** A key representing a particular subclass */
  357. const CLASSKEY_".strtoupper($child->getKey())." = '" . $child->getKey() . "';
  358. /** A key representing a particular subclass */
  359. const CLASSKEY_".strtoupper($child->getClassName())." = '" . $child->getKey() . "';
  360. /** A class that can be returned by this peer. */
  361. const CLASSNAME_".strtoupper($child->getKey())." = '". $childBuilder->getClasspath() . "';
  362. ";
  363. } /* foreach children */
  364. } /* if col->isenumerated...() */
  365. } /* if table->getchildrencolumn() */
  366. } //
  367. /**
  368. * Adds the alias() utility method.
  369. * @param string &$script The script will be modified in this method.
  370. */
  371. protected function addAlias(&$script)
  372. {
  373. $script .= "
  374. /**
  375. * Convenience method which changes table.column to alias.column.
  376. *
  377. * Using this method you can maintain SQL abstraction while using column aliases.
  378. * <code>
  379. * \$c->addAlias(\"alias1\", TablePeer::TABLE_NAME);
  380. * \$c->addJoin(TablePeer::alias(\"alias1\", TablePeer::PRIMARY_KEY_COLUMN), TablePeer::PRIMARY_KEY_COLUMN);
  381. * </code>
  382. * @param string \$alias The alias for the current table.
  383. * @param string \$column The column name for current table. (i.e. ".$this->getTable()->getPhpName()."Peer::COLUMN_NAME).
  384. * @return string
  385. */
  386. public static function alias(\$alias, \$column)
  387. {
  388. return str_replace(".$this->getPeerClassname()."::TABLE_NAME.'.', \$alias.'.', \$column);
  389. }
  390. ";
  391. } // addAliasMethod
  392. /**
  393. * Adds the addSelectColumns() method.
  394. * @param string &$script The script will be modified in this method.
  395. */
  396. protected function addAddSelectColumns(&$script)
  397. {
  398. $script .= "
  399. /**
  400. * Add all the columns needed to create a new object.
  401. *
  402. * Note: any columns that were marked with lazyLoad=\"true\" in the
  403. * XML schema will not be added to the select list and only loaded
  404. * on demand.
  405. *
  406. * @param criteria object containing the columns to add.
  407. * @throws PropelException Any exceptions caught during processing will be
  408. * rethrown wrapped into a PropelException.
  409. */
  410. public static function addSelectColumns(Criteria \$criteria)
  411. {
  412. ";
  413. foreach ($this->getTable()->getColumns() as $col) {
  414. if (!$col->isLazyLoad()) {
  415. $script .= "
  416. \$criteria->addSelectColumn(".$this->getPeerClassname()."::".$this->getColumnName($col).");
  417. ";
  418. } // if !col->isLazyLoad
  419. } // foreach
  420. $script .="
  421. }
  422. ";
  423. } // addAddSelectColumns()
  424. /**
  425. * Adds the COUNT constants.
  426. * @param string &$script The script will be modified in this method.
  427. */
  428. protected function addCountConstants(&$script)
  429. {
  430. $table = $this->getTable();
  431. $count_col = "*";
  432. /*
  433. * FIXME
  434. * (HL) wanted to remove this because AFAIK count(*) is generally
  435. * optimized in databases, and furthermore the code below isn't correct
  436. * (multi-pkey needs to be accounted for)....
  437. */
  438. if ($table->hasPrimaryKey()) {
  439. $pk = $table->getPrimaryKey();
  440. $count_col = $table->getName().".".strtoupper($pk[0]->getName());
  441. }
  442. $script .= "
  443. const COUNT = 'COUNT($count_col)';
  444. const COUNT_DISTINCT = 'COUNT(DISTINCT $count_col)';
  445. ";
  446. }
  447. /**
  448. * Adds the doCount() method.
  449. * @param string &$script The script will be modified in this method.
  450. */
  451. protected function addDoCount(&$script)
  452. {
  453. $script .= "
  454. /**
  455. * Returns the number of rows matching criteria.
  456. *
  457. * @param Criteria \$criteria
  458. * @param boolean \$distinct Whether to select only distinct columns (You can also set DISTINCT modifier in Criteria).
  459. * @param Connection \$con
  460. * @return int Number of matching rows.
  461. */
  462. public static function doCount(Criteria \$criteria, \$distinct = false, \$con = null)
  463. {
  464. // we're going to modify criteria, so copy it first
  465. \$criteria = clone \$criteria;
  466. // clear out anything that might confuse the ORDER BY clause
  467. \$criteria->clearSelectColumns()->clearOrderByColumns();
  468. if (\$distinct || in_array(Criteria::DISTINCT, \$criteria->getSelectModifiers())) {
  469. \$criteria->addSelectColumn(".$this->getPeerClassname()."::COUNT_DISTINCT);
  470. } else {
  471. \$criteria->addSelectColumn(".$this->getPeerClassname()."::COUNT);
  472. }
  473. // just in case we're grouping: add those columns to the select statement
  474. foreach(\$criteria->getGroupByColumns() as \$column)
  475. {
  476. \$criteria->addSelectColumn(\$column);
  477. }
  478. \$rs = ".$this->getPeerClassname()."::doSelectRS(\$criteria, \$con);
  479. if (\$rs->next()) {
  480. return \$rs->getInt(1);
  481. } else {
  482. // no rows returned; we infer that means 0 matches.
  483. return 0;
  484. }
  485. }";
  486. }
  487. /**
  488. * Adds the doSelectOne() method.
  489. * @param string &$script The script will be modified in this method.
  490. */
  491. protected function addDoSelectOne(&$script)
  492. {
  493. $script .= "
  494. /**
  495. * Method to select one object from the DB.
  496. *
  497. * @param Criteria \$criteria object used to create the SELECT statement.
  498. * @param Connection \$con
  499. * @return ".$this->getTable()->getPhpName()."
  500. * @throws PropelException Any exceptions caught during processing will be
  501. * rethrown wrapped into a PropelException.
  502. */
  503. public static function doSelectOne(Criteria \$criteria, \$con = null)
  504. {
  505. \$critcopy = clone \$criteria;
  506. \$critcopy->setLimit(1);
  507. \$objects = ".$this->getPeerClassname()."::doSelect(\$critcopy, \$con);
  508. if (\$objects) {
  509. return \$objects[0];
  510. }
  511. return null;
  512. }";
  513. }
  514. /**
  515. * Adds the doSelect() method.
  516. * @param string &$script The script will be modified in this method.
  517. */
  518. protected function addDoSelect(&$script)
  519. {
  520. $script .= "
  521. /**
  522. * Method to do selects.
  523. *
  524. * @param Criteria \$criteria The Criteria object used to build the SELECT statement.
  525. * @param Connection \$con
  526. * @return array Array of selected Objects
  527. * @throws PropelException Any exceptions caught during processing will be
  528. * rethrown wrapped into a PropelException.
  529. */
  530. public static function doSelect(Criteria \$criteria, \$con = null)
  531. {
  532. return ".$this->getPeerClassname()."::populateObjects(".$this->getPeerClassname()."::doSelectRS(\$criteria, \$con));
  533. }";
  534. }
  535. /**
  536. * Adds the doSelectRS() method.
  537. * @param string &$script The script will be modified in this method.
  538. */
  539. protected function addDoSelectRS(&$script)
  540. {
  541. $script .= "
  542. /**
  543. * Prepares the Criteria object and uses the parent doSelect()
  544. * method to get a ResultSet.
  545. *
  546. * Use this method directly if you want to just get the resultset
  547. * (instead of an array of objects).
  548. *
  549. * @param Criteria \$criteria The Criteria object used to build the SELECT statement.
  550. * @param Connection \$con the connection to use
  551. * @throws PropelException Any exceptions caught during processing will be
  552. * rethrown wrapped into a PropelException.
  553. * @return ResultSet The resultset object with numerically-indexed fields.
  554. * @see ".$this->basePeerClassname."::doSelect()
  555. */
  556. public static function doSelectRS(Criteria \$criteria, \$con = null)
  557. {
  558. if (\$con === null) {
  559. \$con = Propel::getConnection(self::DATABASE_NAME);
  560. }
  561. if (!\$criteria->getSelectColumns()) {
  562. \$criteria = clone \$criteria;
  563. ".$this->getPeerClassname()."::addSelectColumns(\$criteria);
  564. }
  565. // Set the correct dbName
  566. \$criteria->setDbName(self::DATABASE_NAME);
  567. // BasePeer returns a Creole ResultSet, set to return
  568. // rows indexed numerically.
  569. return ".$this->basePeerClassname."::doSelect(\$criteria, \$con);
  570. }";
  571. }
  572. /**
  573. * Adds the populateObjects() method.
  574. * @param string &$script The script will be modified in this method.
  575. */
  576. protected function addPopulateObjects(&$script)
  577. {
  578. $table = $this->getTable();
  579. $script .= "
  580. /**
  581. * The returned array will contain objects of the default type or
  582. * objects that inherit from the default.
  583. *
  584. * @throws PropelException Any exceptions caught during processing will be
  585. * rethrown wrapped into a PropelException.
  586. */
  587. public static function populateObjects(ResultSet \$rs)
  588. {
  589. \$results = array();
  590. ";
  591. if (!$table->getChildrenColumn()) {
  592. $script .= "
  593. // set the class once to avoid overhead in the loop
  594. \$cls = ".$this->getPeerClassname()."::getOMClass();
  595. \$cls = Propel::import(\$cls);";
  596. }
  597. $script .= "
  598. // populate the object(s)
  599. while(\$rs->next()) {
  600. ";
  601. if ($table->getChildrenColumn()) {
  602. $script .= "
  603. // class must be set each time from the record row
  604. \$cls = Propel::import(".$this->getPeerClassname()."::getOMClass(\$rs, 1));
  605. \$obj = new \$cls();
  606. \$obj->hydrate(\$rs);
  607. \$results[] = \$obj;
  608. ";
  609. } else {
  610. $script .= "
  611. \$obj = new \$cls();
  612. \$obj->hydrate(\$rs);
  613. \$results[] = \$obj;
  614. ";
  615. }
  616. $script .= "
  617. }
  618. return \$results;
  619. }";
  620. }
  621. /**
  622. * Adds a getOMClass() for non-abstract tables that have inheritance.
  623. * @param string &$script The script will be modified in this method.
  624. */
  625. protected function addGetOMClass_Inheritance(&$script)
  626. {
  627. $col = $this->getTable()->getChildrenColumn();
  628. $script .= "
  629. /**
  630. * The returned Class will contain objects of the default type or
  631. * objects that inherit from the default.
  632. *
  633. * @param ResultSet \$rs ResultSet with pointer to record containing om class.
  634. * @param int \$colnum Column to examine for OM class information (first is 1).
  635. * @throws PropelException Any exceptions caught during processing will be
  636. * rethrown wrapped into a PropelException.
  637. */
  638. public static function getOMClass(ResultSet \$rs, \$colnum)
  639. {
  640. try {
  641. ";
  642. if ($col->isEnumeratedClasses()) {
  643. $script .= "
  644. \$omClass = null;
  645. \$classKey = \$rs->getString(\$colnum - 1 + " . $col->getPosition() . ");
  646. switch(\$classKey) {
  647. ";
  648. foreach ($col->getChildren() as $child) {
  649. $script .= "
  650. case self::CLASSKEY_".$child->getKey().":
  651. \$omClass = self::CLASSNAME_".strtoupper($child->getKey()).";
  652. break;
  653. ";
  654. } /* foreach */
  655. $script .= "
  656. default:
  657. \$omClass = self::CLASS_DEFAULT;
  658. ";
  659. $script .= "
  660. } // switch
  661. ";
  662. } else { /* if not enumerated */
  663. $script .= "
  664. \$omClass = Propel::import(\$rs->getString(\$colnum - 1 + ".$col->getPosition()."));
  665. ";
  666. }
  667. $script .= "
  668. } catch (Exception \$e) {
  669. throw new PropelException('Unable to get OM class.', \$e);
  670. }
  671. return \$omClass;
  672. }
  673. ";
  674. }
  675. /**
  676. * Adds a getOMClass() signature for abstract tables that have inheritance.
  677. * @param string &$script The script will be modified in this method.
  678. */
  679. protected function addGetOMClass_Inheritance_Abstract(&$script)
  680. {
  681. $script .= "
  682. /**
  683. * The returned Class will contain objects of the default type or
  684. * objects that inherit from the default.
  685. *
  686. * This method must be overridden by the stub subclass, because
  687. * ".$this->getTable()->getPhpName()." is declared abstract in the schema.
  688. *
  689. * @param ResultSet \$rs ResultSet with pointer to record containing om class.
  690. * @param int \$colnum Column to examine for OM class information (first is 1).
  691. * @throws PropelException Any exceptions caught during processing will be
  692. * rethrown wrapped into a PropelException.
  693. */
  694. abstract public static function getOMClass();
  695. ";
  696. }
  697. /**
  698. * Adds a getOMClass() for non-abstract tables that do note use inheritance.
  699. * @param string &$script The script will be modified in this method.
  700. */
  701. protected function addGetOMClass_NoInheritance(&$script)
  702. {
  703. $script .= "
  704. /**
  705. * The class that the Peer will make instances of.
  706. *
  707. * This uses a dot-path notation which is tranalted into a path
  708. * relative to a location on the PHP include_path.
  709. * (e.g. path.to.MyClass -> 'path/to/MyClass.php')
  710. *
  711. * @return string path.to.ClassName
  712. */
  713. public static function getOMClass()
  714. {
  715. return ".$this->getPeerClassname()."::CLASS_DEFAULT;
  716. }
  717. ";
  718. }
  719. /**
  720. * Adds a getOMClass() signature for abstract tables that do not have inheritance.
  721. * @param string &$script The script will be modified in this method.
  722. */
  723. protected function addGetOMClass_NoInheritance_Abstract(&$script)
  724. {
  725. $script .= "
  726. /**
  727. * The class that the Peer will make instances of.
  728. *
  729. * This method must be overridden by the stub subclass, because
  730. * ".$this->getTable()->getPhpName()." is declared abstract in the schema.
  731. */
  732. abstract public static function getOMClass();
  733. ";
  734. }
  735. /**
  736. * Adds the doInsert() method.
  737. * @param string &$script The script will be modified in this method.
  738. */
  739. protected function addDoInsert(&$script)
  740. {
  741. $table = $this->getTable();
  742. $script .= "
  743. /**
  744. * Method perform an INSERT on the database, given a ".$table->getPhpName()." or Criteria object.
  745. *
  746. * @param mixed \$values Criteria or ".$table->getPhpName()." object containing data that is used to create the INSERT statement.
  747. * @param Connection \$con the connection to use
  748. * @return mixed The new primary key.
  749. * @throws PropelException Any exceptions caught during processing will be
  750. * rethrown wrapped into a PropelException.
  751. */
  752. public static function doInsert(\$values, \$con = null)
  753. {
  754. if (\$con === null) {
  755. \$con = Propel::getConnection(self::DATABASE_NAME);
  756. }
  757. if (\$values instanceof Criteria) {
  758. \$criteria = clone \$values; // rename for clarity
  759. } else {
  760. \$criteria = \$values->buildCriteria(); // build Criteria from ".$table->getPhpName()." object
  761. }
  762. ";
  763. foreach ($table->getColumns() as $col) {
  764. $cfc = $col->getPhpName();
  765. if ($col->isPrimaryKey() && $col->isAutoIncrement() && $table->getIdMethod() != "none") {
  766. $script .= "
  767. \$criteria->remove(".$this->getColumnConstant($col)."); // remove pkey col since this table uses auto-increment
  768. ";
  769. }
  770. }
  771. $script .= "
  772. // Set the correct dbName
  773. \$criteria->setDbName(self::DATABASE_NAME);
  774. try {
  775. // use transaction because \$criteria could contain info
  776. // for more than one table (I guess, conceivably)
  777. \$con->begin();
  778. \$pk = ".$this->basePeerClassname."::doInsert(\$criteria, \$con);
  779. \$con->commit();
  780. } catch(PropelException \$e) {
  781. \$con->rollback();
  782. throw \$e;
  783. }
  784. return \$pk;
  785. }
  786. ";
  787. }
  788. /**
  789. * Adds the doUpdate() method.
  790. * @param string &$script The script will be modified in this method.
  791. */
  792. protected function addDoUpdate(&$script)
  793. {
  794. $table = $this->getTable();
  795. $script .= "
  796. /**
  797. * Method perform an UPDATE on the database, given a ".$table->getPhpName()." or Criteria object.
  798. *
  799. * @param mixed \$values Criteria or ".$table->getPhpName()." object containing data that is used to create the UPDATE statement.
  800. * @param Connection \$con The connection to use (specify Connection object to exert more control over transactions).
  801. * @return int The number of affected rows (if supported by underlying database driver).
  802. * @throws PropelException Any exceptions caught during processing will be
  803. * rethrown wrapped into a PropelException.
  804. */
  805. public static function doUpdate(\$values, \$con = null)
  806. {
  807. if (\$con === null) {
  808. \$con = Propel::getConnection(self::DATABASE_NAME);
  809. }
  810. \$selectCriteria = new Criteria(self::DATABASE_NAME);
  811. if (\$values instanceof Criteria) {
  812. \$criteria = clone \$values; // rename for clarity
  813. ";
  814. foreach ($table->getColumns() as $col) {
  815. if($col->isPrimaryKey()) {
  816. $script .= "
  817. \$comparison = \$criteria->getComparison(".$this->getColumnConstant($col).");
  818. \$selectCriteria->add(".$this->getColumnConstant($col).", \$criteria->remove(".$this->getColumnConstant($col)."), \$comparison);
  819. ";
  820. } /* if col is prim key */
  821. } /* foreach */
  822. $script .= "
  823. } else { // \$values is ".$table->getPhpName()." object
  824. \$criteria = \$values->buildCriteria(); // gets full criteria
  825. \$selectCriteria = \$values->buildPkeyCriteria(); // gets criteria w/ primary key(s)
  826. }
  827. // set the correct dbName
  828. \$criteria->setDbName(self::DATABASE_NAME);
  829. return {$this->basePeerClassname}::doUpdate(\$selectCriteria, \$criteria, \$con);
  830. }
  831. ";
  832. }
  833. /**
  834. * Adds the doDeleteAll() method.
  835. * @param string &$script The script will be modified in this method.
  836. */
  837. protected function addDoDeleteAll(&$script)
  838. {
  839. $table = $this->getTable();
  840. $script .= "
  841. /**
  842. * Method to DELETE all rows from the ".$table->getName()." table.
  843. *
  844. * @return int The number of affected rows (if supported by underlying database driver).
  845. */
  846. public static function doDeleteAll(\$con = null)
  847. {
  848. if (\$con === null) {
  849. \$con = Propel::getConnection(self::DATABASE_NAME);
  850. }
  851. \$affectedRows = 0; // initialize var to track total num of affected rows
  852. try {
  853. // use transaction because \$criteria could contain info
  854. // for more than one table or we could emulating ON DELETE CASCADE, etc.
  855. \$con->begin();
  856. ";
  857. if ($this->isDeleteCascadeEmulationNeeded()) {
  858. $script .="\$affectedRows += ".$this->getPeerClassname()."::doOnDeleteCascade(new Criteria(), \$con);
  859. ";
  860. }
  861. if ($this->isDeleteSetNullEmulationNeeded()) {
  862. $script .= $this->getPeerClassname() . "::doOnDeleteSetNull(new Criteria(), \$con);
  863. ";
  864. }
  865. $script .= "\$affectedRows += BasePeer::doDeleteAll(".$this->getPeerClassname()."::TABLE_NAME, \$con);
  866. \$con->commit();
  867. return \$affectedRows;
  868. } catch (PropelException \$e) {
  869. \$con->rollback();
  870. throw \$e;
  871. }
  872. }
  873. ";
  874. }
  875. /**
  876. * Adds the doDelete() method.
  877. * @param string &$script The script will be modified in this method.
  878. */
  879. protected function addDoDelete(&$script)
  880. {
  881. $table = $this->getTable();
  882. $script .= "
  883. /**
  884. * Method perform a DELETE on the database, given a ".$table->getPhpName()." or Criteria object OR a primary key value.
  885. *
  886. * @param mixed \$values Criteria or ".$table->getPhpName()." object or primary key or array of primary keys
  887. * which is used to create the DELETE statement
  888. * @param Connection \$con the connection to use
  889. * @return int The number of affected rows (if supported by underlying database driver). This includes CASCADE-related rows
  890. * if supported by native driver or if emulated using Propel.
  891. * @throws PropelException Any exceptions caught during processing will be
  892. * rethrown wrapped into a PropelException.
  893. */
  894. public static function doDelete(\$values, \$con = null)
  895. {
  896. if (\$con === null) {
  897. \$con = Propel::getConnection(".$this->getPeerClassname()."::DATABASE_NAME);
  898. }
  899. if (\$values instanceof Criteria) {
  900. \$criteria = clone \$values; // rename for clarity
  901. } elseif (\$values instanceof ".$table->getPhpName().") {
  902. ";
  903. if (count($table->getPrimaryKey()) > 0) {
  904. $script .= "
  905. \$criteria = \$values->buildPkeyCriteria();";
  906. } else {
  907. $script .= "
  908. \$criteria = \$values->buildCriteria();";
  909. }
  910. $script .= "
  911. } else {
  912. // it must be the primary key
  913. \$criteria = new Criteria(self::DATABASE_NAME);";
  914. if (count($table->getPrimaryKey()) === 1) {
  915. $pkey = $table->getPrimaryKey();
  916. $col = array_shift($pkey);
  917. $script .= "
  918. \$criteria->add(".$this->getColumnConstant($col).", (array) \$values, Criteria::IN);";
  919. } else {
  920. $script .= "
  921. // primary key is composite; we therefore, expect
  922. // the primary key passed to be an array of pkey
  923. // values
  924. if(count(\$values) == count(\$values, COUNT_RECURSIVE))
  925. {
  926. // array is not multi-dimensional
  927. \$values = array(\$values);
  928. }
  929. \$vals = array();
  930. foreach(\$values as \$value)
  931. {
  932. ";
  933. $i=0;
  934. foreach($table->getPrimaryKey() as $col) {
  935. $script .= "
  936. \$vals[$i][] = \$value[$i];";
  937. $i++;
  938. }
  939. $script .= "
  940. }
  941. ";
  942. $i=0;
  943. foreach($table->getPrimaryKey() as $col) {
  944. $script .= "
  945. \$criteria->add(".$this->getColumnConstant($col).", \$vals[$i], Criteria::IN);";
  946. $i++;
  947. }
  948. } /* if count(table->getPrimaryKeys()) */
  949. $script .= "
  950. }
  951. // Set the correct dbName
  952. \$criteria->setDbName(self::DATABASE_NAME);
  953. \$affectedRows = 0; // initialize var to track total num of affected rows
  954. try {
  955. // use transaction because \$criteria could contain info
  956. // for more than one table or we could emulating ON DELETE CASCADE, etc.
  957. \$con->begin();
  958. ";
  959. if ($this->isDeleteCascadeEmulationNeeded()) {
  960. $script .= "\$affectedRows += ".$this->getPeerClassname()."::doOnDeleteCascade(\$criteria, \$con);";
  961. }
  962. if ($this->isDeleteSetNullEmulationNeeded()) {
  963. $script .= $this->getPeerClassname() . "::doOnDeleteSetNull(\$criteria, \$con);";
  964. }
  965. $script .= "
  966. \$affectedRows += {$this->basePeerClassname}::doDelete(\$criteria, \$con);
  967. \$con->commit();
  968. return \$affectedRows;
  969. } catch (PropelException \$e) {
  970. \$con->rollback();
  971. throw \$e;
  972. }
  973. }
  974. ";
  975. }
  976. /**
  977. * Adds the doOnDeleteCascade() method, which provides ON DELETE CASCADE emulation.
  978. * @param string &$script The script will be modified in this method.
  979. */
  980. protected function addDoOnDeleteCascade(&$script)
  981. {
  982. $table = $this->getTable();
  983. $script .= "
  984. /**
  985. * This is a method for emulating ON DELETE CASCADE for DBs that don't support this
  986. * feature (like MySQL or SQLite).
  987. *
  988. * This method is not very speedy because it must perform a query first to get
  989. * the implicated records and then perform the deletes by calling those Peer classes.
  990. *
  991. * This method should be used within a transaction if possible.
  992. *
  993. * @param Criteria \$criteria
  994. * @param Connection \$con
  995. * @return int The number of affected rows (if supported by underlying database driver).
  996. */
  997. protected static function doOnDeleteCascade(Criteria \$criteria, Connection \$con)
  998. {
  999. // initialize var to track total num of affected rows
  1000. \$affectedRows = 0;
  1001. // first find the objects that are implicated by the \$criteria
  1002. \$objects = ".$this->getPeerClassname()."::doSelect(\$criteria, \$con);
  1003. foreach(\$objects as \$obj) {
  1004. ";
  1005. foreach ($table->getReferrers() as $fk) {
  1006. // $fk is the foreign key in the other table, so localTableName will
  1007. // actually be the table name of other table
  1008. $tblFK = $fk->getTable();
  1009. $joinedTablePeerBuilder = OMBuilder::getNewPeerBuilder($tblFK);
  1010. $tblFKPackage = $joinedTablePeerBuilder->getStubPeerBuilder()->getPackage();
  1011. if (!$tblFK->isForReferenceOnly()) {
  1012. // we can't perform operations on tables that are
  1013. // not within the schema (i.e. that we have no map for, etc.)
  1014. $fkClassName = $tblFK->getPhpName();
  1015. // i'm not sure whether we can allow delete cascade for foreign keys
  1016. // within the same table? perhaps we can?
  1017. if ( $fk->getOnDelete() == ForeignKey::CASCADE && $tblFK->getName() != $table->getName()) {
  1018. // backwards on purpose
  1019. $columnNamesF = $fk->getLocalColumns();
  1020. $columnNamesL = $fk->getForeignColumns();
  1021. $script .= "
  1022. include_once '".$this->getFilePath($tblFKPackage, $tblFK->getPhpName())."';
  1023. // delete related $fkClassName objects
  1024. \$c = new Criteria();
  1025. ";
  1026. for($x=0,$xlen=count($columnNamesF); $x < $xlen; $x++) {
  1027. $columnFK = $tblFK->getColumn($columnNamesF[$x]);
  1028. $columnL = $table->getColumn($columnNamesL[$x]);
  1029. $script .= "
  1030. \$c->add(".$joinedTablePeerBuilder->getColumnConstant($columnFK) .", \$obj->get".$columnL->getPhpName()."());";
  1031. }
  1032. $script .= "
  1033. \$affectedRows += ".$joinedTablePeerBuilder->getPeerClassname()."::doDelete(\$c, \$con);";
  1034. } // if cascade && fkey table name != curr table name
  1035. } // if not for ref only
  1036. } // foreach foreign keys
  1037. $script .= "
  1038. }
  1039. return \$affectedRows;
  1040. }
  1041. ";
  1042. } // end addDoOnDeleteCascade
  1043. /**
  1044. * Adds the doOnDeleteSetNull() method, which provides ON DELETE SET NULL emulation.
  1045. * @param string &$script The script will be modified in this method.
  1046. */
  1047. protected function addDoOnDeleteSetNull(&$script)
  1048. {
  1049. $table = $this->getTable();
  1050. $script .= "
  1051. /**
  1052. * This is a method for emulating ON DELETE SET NULL DBs that don't support this
  1053. * feature (like MySQL or SQLite).
  1054. *
  1055. * This method is not very speedy because it must perform a query first to get
  1056. * the implicated records and then perform the deletes by calling those Peer classes.
  1057. *
  1058. * This method should be used within a transaction if possible.
  1059. *
  1060. * @param Criteria \$criteria
  1061. * @param Connection \$con
  1062. * @return void
  1063. */
  1064. protected static function doOnDeleteSetNull(Criteria \$criteria, Connection \$con)
  1065. {
  1066. // first find the objects that are implicated by the \$criteria
  1067. \$objects = ".$this->getPeerClassname()."::doSelect(\$criteria, \$con);
  1068. foreach(\$objects as \$obj) {
  1069. ";
  1070. // This logic is almost exactly the same as that in doOnDeleteCascade()
  1071. // it may make sense to refactor this, provided that thigns don't
  1072. // get too complicated.
  1073. foreach ($table->getReferrers() as $fk) {
  1074. // $fk is the foreign key in the other table, so localTableName will
  1075. // actually be the table name of other table
  1076. $tblFK = $fk->getTable();
  1077. $refTablePeerBuilder = OMBuilder::getNewPeerBuilder($tblFK);
  1078. if (!$tblFK->isForReferenceOnly()) {
  1079. // we can't perform operations on tables that are
  1080. // not within the schema (i.e. that we have no map for, etc.)
  1081. $fkClassName = $tblFK->getPhpName();
  1082. // i'm not sure whether we can allow delete setnull for foreign keys
  1083. // within the same table? perhaps we can?
  1084. if ( $fk->getOnDelete() == ForeignKey::SETNULL &&
  1085. $fk->getTable()->getName() != $table->getName()) {
  1086. // backwards on purpose
  1087. $columnNamesF = $fk->getLocalColumns();
  1088. $columnNamesL = $fk->getForeignColumns(); // should be same num as foreign
  1089. $script .= "
  1090. // set fkey col in related $fkClassName rows to NULL
  1091. \$selectCriteria = new Criteria(".$this->getPeerClassname()."::DATABASE_NAME);
  1092. \$updateValues = new Criteria(".$this->getPeerClassname()."::DATABASE_NAME);";
  1093. for ($x=0,$xlen=count($columnNamesF); $x < $xlen; $x++) {
  1094. $columnFK = $tblFK->getColumn($columnNamesF[$x]);
  1095. $columnL = $table->getColumn($columnNamesL[$x]);
  1096. $script .= "
  1097. \$selectCriteria->add(".$refTablePeerBuilder->getColumnConstant($columnFK).", \$obj->get".$columnL->getPhpName()."());
  1098. \$updateValues->add(".$refTablePeerBuilder->getColumnConstant($columnFK).", null);
  1099. ";
  1100. }
  1101. $script .= "
  1102. {$this->basePeerClassname}::doUpdate(\$selectCriteria, \$updateValues, \$con); // use BasePeer because generated Peer doUpdate() methods only update using pkey
  1103. ";
  1104. } // if setnull && fkey table name != curr table name
  1105. } // if not for ref only
  1106. } // foreach foreign keys
  1107. $script .= "
  1108. }
  1109. }
  1110. ";
  1111. }
  1112. /**
  1113. * Adds the doValidate() method.
  1114. * @param string &$script The script will be modified in this method.
  1115. */
  1116. protected function addDoValidate(&$script)
  1117. {
  1118. $table = $this->getTable();
  1119. $script .= "
  1120. /**
  1121. * Validates all modified columns of given ".$table->getPhpName()." object.
  1122. * If parameter \$columns is either a single column name or an array of column names
  1123. * than only those columns are validated.
  1124. *
  1125. * NOTICE: This does not apply to primary or foreign keys for now.
  1126. *
  1127. * @param ".$table->getPhpName()." \$obj The object to validate.
  1128. * @param mixed \$cols Column name or array of column names.
  1129. *
  1130. * @return mixed TRUE if all columns are valid or the error message of the first invalid column.
  1131. */
  1132. public static function doValidate(".$table->getPhpName()." \$obj, \$cols = null)
  1133. {
  1134. \$columns = array();
  1135. if (\$cols) {
  1136. \$dbMap = Propel::getDatabaseMap(".$this->getPeerClassname()."::DATABASE_NAME);
  1137. \$tableMap = \$dbMap->getTable(".$this->getPeerClassname()."::TABLE_NAME);
  1138. if (! is_array(\$cols)) {
  1139. \$cols = array(\$cols);
  1140. }
  1141. foreach(\$cols as \$colName) {
  1142. if (\$tableMap->containsColumn(\$colName)) {
  1143. \$get = 'get' . \$tableMap->getColumn(\$colName)->getPhpName();
  1144. \$columns[\$colName] = \$obj->\$get();
  1145. }
  1146. }
  1147. } else {
  1148. ";
  1149. foreach ($table->getValidators() as $val) {
  1150. $col = $val->getColumn();
  1151. if (!$col->isAutoIncrement()) {
  1152. $script .= "
  1153. if (\$obj->isNew() || \$obj->isColumnModified(".$this->getColumnConstant($col)."))
  1154. \$columns[".$this->getColumnConstant($col)."] = \$obj->get".$col->getPhpName()."();
  1155. ";
  1156. } // if
  1157. } // foreach
  1158. $script .= "
  1159. }
  1160. return {$this->basePeerClassname}::doValidate(".$this->getPeerClassname()."::DATABASE_NAME, ".$this->getPeerClassname()."::TABLE_NAME, \$columns);
  1161. }
  1162. ";
  1163. } // end addDoValidate()
  1164. /**
  1165. * Adds the retrieveByPK method for tables with single-column primary key.
  1166. * @param string &$script The script will be modified in this method.
  1167. */
  1168. protected function addRetrieveByPK_SinglePK(&$script)
  1169. {
  1170. $table = $this->getTable();
  1171. $script .= "
  1172. /**
  1173. * Retrieve a single object by pkey.
  1174. *
  1175. * @param mixed \$pk the primary key.
  1176. * @param Connection \$con the connection to use
  1177. * @return " . $table->getPhpName() . "
  1178. */
  1179. public static function ".$this->getRetrieveMethodName()."(\$pk, \$con = null)
  1180. {
  1181. if (\$con === null) {
  1182. \$con = Propel::getConnection(self::DATABASE_NAME);
  1183. }
  1184. \$criteria = new Criteria(".$this->getPeerClassname()."::DATABASE_NAME);
  1185. ";
  1186. if (count($table->getPrimaryKey()) === 1) {
  1187. $pkey = $table->getPrimaryKey();
  1188. $col = array_shift($pkey);
  1189. $script .= "
  1190. \$criteria->add(".$this->getColumnConstant($col).", \$pk);
  1191. ";
  1192. } else {
  1193. // primary key is composite; we therefore, expect
  1194. // the primary key passed to be an array of pkey
  1195. // values
  1196. $i=0;
  1197. foreach($table->getPrimaryKey() as $col) {
  1198. $script .= "
  1199. \$criteria->add(".$this->getColumnConstant($col).", \$pk[$i]);";
  1200. $i++;
  1201. }
  1202. } /* if count(table.PrimaryKeys) */
  1203. $script .= "
  1204. \$v = ".$this->getPeerClassname()."::doSelect(\$criteria, \$con);
  1205. return !empty(\$v) > 0 ? \$v[0] : null;
  1206. }
  1207. ";
  1208. }
  1209. /**
  1210. * Adds the retrieveByPKs method for tables with single-column primary key.
  1211. * @param string &$script The script will be modified in this method.
  1212. */
  1213. protected function addRetrieveByPKs_SinglePK(&$script)
  1214. {
  1215. $table = $this->getTable();
  1216. $script .= "
  1217. /**
  1218. * Retrieve multiple objects by pkey.
  1219. *
  1220. * @param array \$pks List of primary keys
  1221. * @param Connection \$con the connection to use
  1222. * @throws PropelException Any exceptions caught during processing will be
  1223. * rethrown wrapped into a PropelException.
  1224. */
  1225. public static function ".$this->getRetrieveMethodName()."s(\$pks, \$con = null)
  1226. {
  1227. if (\$con === null) {
  1228. \$con = Propel::getConnection(self::DATABASE_NAME);
  1229. }
  1230. \$objs = null;
  1231. if (empty(\$pks)) {
  1232. \$objs = array();
  1233. } else {
  1234. \$criteria = new Criteria();";
  1235. if (count($table->getPrimaryKey()) == 1) {
  1236. $k1 = $table->getPrimaryKey();
  1237. $script .= "
  1238. \$criteria->add(".$this->getColumnConstant($k1[0]).", \$pks, Criteria::IN);";
  1239. } else {
  1240. $script .= "
  1241. foreach(\$pks as \$pk) {";
  1242. $i = 0;
  1243. foreach($table->getPrimaryKey() as $col) {
  1244. $script .= "
  1245. \$c{$i} = \$criteria->getNewCriterion(".$this->getPeerClassname($col).", \$pk[$i], Criteria::EQUAL);";
  1246. $j = $i - 1;
  1247. if ($i > 0) {
  1248. $script .= "
  1249. \$c{$j}->addAnd(\$c{$i});";
  1250. } /* if $i > 0 */
  1251. $i++;
  1252. } /* foreach */
  1253. $script .= "
  1254. \$criteria->addOr(\$c0);
  1255. }";
  1256. } /* if count prim keys == 1 */
  1257. $script .= "
  1258. \$objs = ".$this->getPeerClassname()."::doSelect(\$criteria, \$con);
  1259. }
  1260. return \$objs;
  1261. }
  1262. ";
  1263. }
  1264. /**
  1265. * Adds the retrieveByPK method for tables with multi-column primary key.
  1266. * @param string &$script The script will be modified in this method.
  1267. */
  1268. protected function addRetrieveByPK_MultiPK(&$script)
  1269. {
  1270. $table = $this->getTable();
  1271. $script .= "
  1272. /**
  1273. * Retrieve object using using composite pkey values.
  1274. * ";
  1275. foreach ($table->getPrimaryKey() as $col) {
  1276. $clo = strtolower($col->getName());
  1277. $cptype = $col->getPhpNative();
  1278. $script .= "@param $cptype $".$clo."
  1279. ";
  1280. }
  1281. $script .= "
  1282. * @param Connection \$con
  1283. * @return ".$table->getPhpName()."
  1284. */
  1285. public static function ".$this->getRetrieveMethodName()."(";
  1286. $co = 0;
  1287. foreach ($table->getPrimaryKey() as $col) {
  1288. $clo = strtolower($col->getName());
  1289. $script .= ($co++ ? "," : "") . " $".$clo;
  1290. } /* foreach */
  1291. $script .= ", \$con = null) {
  1292. if (\$con === null) {
  1293. \$con = Propel::getConnection(self::DATABASE_NAME);
  1294. }
  1295. \$criteria = new Criteria();";
  1296. foreach ($table->getPrimaryKey() as $col) {
  1297. $clo = strtolower($col->getName());
  1298. $script .= "
  1299. \$criteria->add(".$this->getColumnConstant($col).", $".$clo.");";
  1300. }
  1301. $script .= "
  1302. \$v = ".$this->getPeerClassname()."::doSelect(\$criteria, \$con);
  1303. return !empty(\$v) ? \$v[0] : null;
  1304. }";
  1305. }
  1306. /**
  1307. * Adds the getTableMap() method which is a convenience method for apps to get DB metadata.
  1308. * @param string &$script The script will be modified in this method.
  1309. */
  1310. protected function addGetTableMap(&$script)
  1311. {
  1312. $script .= "
  1313. /**
  1314. * Returns the TableMap related to this peer.
  1315. * This method is not needed for general use but a specific application could have a need.
  1316. * @return TableMap
  1317. * @throws PropelException Any exceptions caught during processing will be
  1318. * rethrown wrapped into a PropelException.
  1319. */
  1320. public static function getTableMap()
  1321. {
  1322. return Propel::getDatabaseMap(self::DATABASE_NAME)->getTable(self::TABLE_NAME);
  1323. }
  1324. ";
  1325. }
  1326. } // PHP5BasicPeerBuilder