PageRenderTime 65ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://bitbucket.org/pandaos/kaltura
PHP | 1392 lines | 772 code | 136 blank | 484 comment | 79 complexity | 405a0a419915d8047802d182c27cdd58 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: PHP5BasicObjectBuilder.php 298 2005-12-09 13:47:25Z hans $
  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/ObjectBuilder.php';
  22. /**
  23. * Generates a PHP5 base Object class for user object model (OM).
  24. *
  25. * This class produces the base object class (e.g. BaseMyTable) which contains all
  26. * the custom-built accessor and setter methods.
  27. *
  28. * This class replaces the Object.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 PHP5BasicObjectBuilder extends ObjectBuilder {
  35. /**
  36. * Gets the package for the [base] object classes.
  37. * @return string
  38. */
  39. public function getPackage()
  40. {
  41. return parent::getPackage() . ".om";
  42. }
  43. /**
  44. * Returns the name of the current class being built.
  45. * @return string
  46. */
  47. public function getClassname()
  48. {
  49. return $this->getBuildProperty('basePrefix') . $this->getStubObjectBuilder()->getClassname();
  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. {
  57. $table = $this->getTable();
  58. $package = $this->getPackage();
  59. $parentClass = $this->getBaseClass();
  60. $interface = $this->getInterface();
  61. $script .= "
  62. require_once '".$this->getFilePath($parentClass)."';
  63. ";
  64. if (!empty($interface)) {
  65. $script .= "
  66. require_once '".$this->getFilePath($interface)."';
  67. ";
  68. }
  69. if (!$table->isAlias()) {
  70. // If any columns in table are BLOB or CLOB then we need to make
  71. // sure those classes are included so we can do things like
  72. // if ($v instanceof Lob) etc.
  73. $includes_lobs = false;
  74. foreach ($table->getColumns() as $col) {
  75. if ($col->isLob()) {
  76. $includes_lobs = true;
  77. break;
  78. }
  79. }
  80. if($includes_lobs) {
  81. $script .= "
  82. include_once 'creole/util/Clob.php';
  83. include_once 'creole/util/Blob.php';
  84. ";
  85. }
  86. } // if table is not alias
  87. $script .= "
  88. include_once 'propel/util/Criteria.php';
  89. ";
  90. $script .= "
  91. include_once '".$this->getStubPeerBuilder()->getClassFilePath()."';
  92. ";
  93. } // addIncludes()
  94. /**
  95. * Adds class phpdoc comment and openning of class.
  96. * @param string &$script The script will be modified in this method.
  97. */
  98. protected function addClassOpen(&$script)
  99. {
  100. $table = $this->getTable();
  101. $tableName = $table->getName();
  102. $tableDesc = $table->getDescription();
  103. $interface = $this->getInterface();
  104. $script .= "
  105. /**
  106. * Base class that represents a row from the '$tableName' table.
  107. *
  108. * $tableDesc
  109. *";
  110. if ($this->getBuildProperty('addTimeStamp')) {
  111. $now = strftime('%c');
  112. $script .= "
  113. * This class was autogenerated by Propel on:
  114. *
  115. * $now
  116. *";
  117. }
  118. $script .= "
  119. * @package ".$this->getPackage()."
  120. */
  121. abstract class ".$this->getClassname()." extends ".ClassTools::classname($this->getBaseClass())." ";
  122. $interface = ClassTools::getInterface($table);
  123. if ($interface) {
  124. $script .= " implements " . ClassTools::classname($interface);
  125. }
  126. $script .= " {
  127. ";
  128. }
  129. /**
  130. * Specifies the methods that are added as part of the basic OM class.
  131. * This can be overridden by subclasses that wish to add more methods.
  132. * @see ObjectBuilder::addClassBody()
  133. */
  134. protected function addClassBody(&$script)
  135. {
  136. $table = $this->getTable();
  137. if (!$table->isAlias()) {
  138. $this->addConstants($script);
  139. $this->addAttributes($script);
  140. }
  141. $this->addColumnAccessorMethods($script);
  142. $this->addColumnMutatorMethods($script);
  143. $this->addHydrate($script);
  144. $this->addManipulationMethods($script);
  145. $this->addValidationMethods($script);
  146. if ($this->isAddGenericAccessors()) {
  147. $this->addGetByName($script);
  148. $this->addGetByPosition($script);
  149. $this->addToArray($script);
  150. }
  151. if ($this->isAddGenericMutators()) {
  152. $this->addSetByName($script);
  153. $this->addSetByPosition($script);
  154. $this->addFromArray($script);
  155. }
  156. $this->addBuildCriteria($script);
  157. $this->addBuildPkeyCriteria($script);
  158. $this->addGetPrimaryKey($script);
  159. $this->addSetPrimaryKey($script);
  160. $this->addCopy($script);
  161. if (!$table->isAlias()) {
  162. $this->addGetPeer($script);
  163. }
  164. }
  165. /**
  166. * Closes class.
  167. * @param string &$script The script will be modified in this method.
  168. */
  169. protected function addClassClose(&$script)
  170. {
  171. $script .= "
  172. } // " . $this->getClassname() . "
  173. ";
  174. }
  175. /**
  176. * Adds any constants to the class.
  177. * @param string &$script The script will be modified in this method.
  178. */
  179. protected function addConstants(&$script)
  180. {
  181. // nothing to do here any more
  182. // fieldnameTypeConstants have been moved to class BasePeer [sv]
  183. }
  184. /**
  185. * Adds class attributes.
  186. * @param string &$script The script will be modified in this method.
  187. */
  188. protected function addAttributes(&$script)
  189. {
  190. $script .= "
  191. /**
  192. * The Peer class.
  193. * Instance provides a convenient way of calling static methods on a class
  194. * that calling code may not be able to identify.
  195. * @var ".$this->getPeerClassname()."
  196. */
  197. protected static \$peer;
  198. ";
  199. if (!$this->getTable()->isAlias()) {
  200. $this->addColumnAttributes($script);
  201. }
  202. }
  203. /**
  204. * Adds variables that store column values.
  205. * @param string &$script The script will be modified in this method.
  206. * @see addColumnNameConstants()
  207. */
  208. protected function addColumnAttributes(&$script) {
  209. $table = $this->getTable();
  210. foreach ($table->getColumns() as $col) {
  211. $cptype = $col->getPhpNative();
  212. $clo=strtolower($col->getName());
  213. $defVal = "";
  214. if (($val = $col->getPhpDefaultValue()) !== null) {
  215. settype($val, $cptype);
  216. $defaultValue = var_export($val, true);
  217. $defVal = " = " . $defaultValue;
  218. }
  219. $script .= "
  220. /**
  221. * The value for the $clo field.
  222. * @var $cptype
  223. */
  224. protected \$" . $clo . $defVal . ";
  225. ";
  226. if ($col->isLazyLoad()) {
  227. $script .= "
  228. /**
  229. * Whether the lazy-loaded $clo value has been loaded from database.
  230. * This is necessary to avoid repeated lookups if $clo column is NULL in the db.
  231. * @var boolean
  232. */
  233. protected \$".$clo."_isLoaded = false;
  234. ";
  235. }
  236. } // foreach col
  237. } // addColumnAttributes()
  238. /**
  239. * Adds the getPeer() method.
  240. * This is a convenient, non introspective way of getting the Peer class for a particular object.
  241. * @param string &$script The script will be modified in this method.
  242. */
  243. protected function addGetPeer(&$script)
  244. {
  245. $script .= "
  246. /**
  247. * Returns a peer instance associated with this om.
  248. *
  249. * Since Peer classes are not to have any instance attributes, this method returns the
  250. * same instance for all member of this class. The method could therefore
  251. * be static, but this would prevent one from overriding the behavior.
  252. *
  253. * @return ".$this->getPeerClassname()."
  254. */
  255. public function getPeer()
  256. {
  257. if (self::\$peer === null) {
  258. self::\$peer = new ".$this->getPeerClassname()."();
  259. }
  260. return self::\$peer;
  261. }
  262. ";
  263. }
  264. // --------------------------------------------------------------
  265. //
  266. // A C C E S S O R M E T H O D S
  267. //
  268. // --------------------------------------------------------------
  269. /**
  270. * Adds a date/time/timestamp getter method.
  271. * @param string &$script The script will be modified in this method.
  272. * @param Column $col The current column.
  273. * @see parent::addColumnAccessors()
  274. */
  275. protected function addTemporalAccessor(&$script, $col)
  276. {
  277. $cfc=$col->getPhpName();
  278. $clo=strtolower($col->getName());
  279. // these default values are based on the Creole defaults
  280. // the date and time default formats are locale-sensitive
  281. if ($col->getType() === PropelTypes::DATE) {
  282. $defaultfmt = $this->getBuildProperty('defaultDateFormat');
  283. } elseif ($col->getType() === PropelTypes::TIME) {
  284. $defaultfmt = $this->getBuildProperty('defaultTimeFormat');
  285. } elseif ($col->getType() === PropelTypes::TIMESTAMP) {
  286. $defaultfmt = $this->getBuildProperty('defaultTimeStampFormat');
  287. }
  288. // if the default format property was an empty string, then we'll set it
  289. // to NULL, which will return the "native" integer timestamp
  290. if (empty($defaultfmt)) { $defaultfmt = null; }
  291. $script .= "
  292. /**
  293. * Get the [optionally formatted] [$clo] column value.
  294. * ".$col->getDescription()."
  295. * @param string \$format The date/time format string (either date()-style or strftime()-style).
  296. * If format is NULL, then the integer unix timestamp will be returned.
  297. * @return mixed Formatted date/time value as string or integer unix timestamp (if format is NULL).
  298. * @throws PropelException - if unable to convert the date/time to timestamp.
  299. */
  300. public function get$cfc(\$format = ".var_export($defaultfmt, true)."";
  301. if ($col->isLazyLoad()) $script .= ", \$con = null";
  302. $script .= ")
  303. {
  304. ";
  305. if ($col->isLazyLoad()) {
  306. $script .= "
  307. if (!\$this->".$clo."_isLoaded && \$this->$clo === null && !\$this->isNew()) {
  308. \$this->load$cfc(\$con);
  309. }
  310. ";
  311. }
  312. $script .= "
  313. if (\$this->$clo === null || \$this->$clo === '') {
  314. return null;
  315. } elseif (!is_int(\$this->$clo)) {
  316. // a non-timestamp value was set externally, so we convert it
  317. \$ts = strtotime(\$this->$clo);
  318. if (\$ts === -1 || \$ts === false) { // in PHP 5.1 return value changes to FALSE
  319. throw new PropelException(\"Unable to parse value of [$clo] as date/time value: \" . var_export(\$this->$clo, true));
  320. }
  321. } else {
  322. \$ts = \$this->$clo;
  323. }
  324. if (\$format === null) {
  325. return \$ts;
  326. } elseif (strpos(\$format, '%') !== false) {
  327. return strftime(\$format, \$ts);
  328. } else {
  329. return date(\$format, \$ts);
  330. }
  331. }
  332. ";
  333. } // addTemporalAccessor
  334. /**
  335. * Adds a normal (non-temporal) getter method.
  336. * @param string &$script The script will be modified in this method.
  337. * @param Column $col The current column.
  338. * @see parent::addColumnAccessors()
  339. */
  340. protected function addGenericAccessor(&$script, $col)
  341. {
  342. $cfc=$col->getPhpName();
  343. $clo=strtolower($col->getName());
  344. $script .= "
  345. /**
  346. * Get the [$clo] column value.
  347. * ".$col->getDescription()."
  348. * @return ".$col->getPhpNative()."
  349. */
  350. public function get$cfc(";
  351. if ($col->isLazyLoad()) $script .= "\$con = null";
  352. $script .= ")
  353. {
  354. ";
  355. if ($col->isLazyLoad()) {
  356. $script .= "
  357. if (!\$this->".$clo."_isLoaded && \$this->$clo === null && !\$this->isNew()) {
  358. \$this->load$cfc(\$con);
  359. }
  360. ";
  361. }
  362. $script .= "
  363. return \$this->$clo;
  364. }
  365. ";
  366. }
  367. /**
  368. * Adds the lazy loader method.
  369. * @param string &$script The script will be modified in this method.
  370. * @param Column $col The current column.
  371. * @see parent::addColumnAccessors()
  372. */
  373. protected function addLazyLoader(&$script, $col)
  374. {
  375. $cfc=$col->getPhpName();
  376. $clo=strtolower($col->getName());
  377. $script .= "
  378. /**
  379. * Load the value for the lazy-loaded [$clo] column.
  380. *
  381. * This method performs an additional query to return the value for
  382. * the [$clo] column, since it is not populated by
  383. * the hydrate() method.
  384. *
  385. * @param \$con Connection
  386. * @return void
  387. * @throws PropelException - any underlying error will be wrapped and re-thrown.
  388. */
  389. protected function load$cfc(\$con = null)
  390. {
  391. \$c = \$this->buildPkeyCriteria();
  392. \$c->addSelectColumn(".$this->getColumnConstant($col).");
  393. try {
  394. \$rs = ".$this->getPeerClassname()."::doSelectRS(\$c, \$con);
  395. \$rs->next();
  396. ";
  397. $affix = CreoleTypes::getAffix(CreoleTypes::getCreoleCode($col->getType()));
  398. $clo = strtolower($col->getName());
  399. switch($col->getType()) {
  400. case PropelTypes::DATE:
  401. case PropelTypes::TIME:
  402. case PropelTypes::TIMESTAMP:
  403. $script .= "
  404. \$this->$clo = \$rs->get$affix(1, null);
  405. ";
  406. break;
  407. default:
  408. $script .= "
  409. \$this->$clo = \$rs->get$affix(1);
  410. ";
  411. } // switch
  412. $script .= "
  413. \$this->".$clo."_isLoaded = true;
  414. } catch (Exception \$e) {
  415. throw new PropelException(\"Error loading value for [$clo] column on demand.\", \$e);
  416. }
  417. }
  418. ";
  419. } // addLazyLoader()
  420. // --------------------------------------------------------------
  421. //
  422. // M U T A T O R M E T H O D S
  423. //
  424. // --------------------------------------------------------------
  425. /**
  426. * Adds the open of the mutator (setter) method for a column.
  427. * @param string &$script The script will be modified in this method.
  428. * @param Column $col The current column.
  429. */
  430. protected function addMutatorOpen(&$script, Column $col)
  431. {
  432. $cfc=$col->getPhpName();
  433. $clo=strtolower($col->getName());
  434. $script .= "
  435. /**
  436. * Set the value of [$clo] column.
  437. * ".$col->getDescription()."
  438. * @param ".$col->getPhpNative()." \$v new value
  439. * @return void
  440. */
  441. public function set$cfc(\$v)
  442. {
  443. ";
  444. if ($col->isLazyLoad()) {
  445. $script .= "
  446. // explicitly set the is-loaded flag to true for this lazy load col;
  447. // it doesn't matter if the value is actually set or not (logic below) as
  448. // any attempt to set the value means that no db lookup should be performed
  449. // when the get$cfc() method is called.
  450. \$this->".$clo."_isLoaded = true;
  451. ";
  452. }
  453. }
  454. /**
  455. * Adds the close of the mutator (setter) method for a column.
  456. * This can be overridden (e.g. by PHP5ComplexObjectBuilder) if additional functionality is needed.
  457. * @param string &$script The script will be modified in this method.
  458. * @param Column $col The current column.
  459. */
  460. protected function addMutatorClose(&$script, Column $col)
  461. {
  462. $script .= "
  463. } // set".$col->getPhpName()."()
  464. ";
  465. }
  466. /**
  467. * Adds a setter for date/time/timestamp columns.
  468. * @param string &$script The script will be modified in this method.
  469. * @param Column $col The current column.
  470. * @see parent::addColumnMutators()
  471. */
  472. protected function addLobMutator(&$script, Column $col)
  473. {
  474. $this->addMutatorOpen($script, $col);
  475. $clo = strtolower($col->getName());
  476. // Setting of LOB columns gets some special handling
  477. if ($col->getPropelType() === PropelTypes::BLOB || $col->getPropelType() === PropelTypes::LONGVARBINARY ) {
  478. $lobClass = 'Blob';
  479. } else {
  480. $lobClass = 'Clob';
  481. }
  482. $script .= "
  483. // if the passed in parameter is the *same* object that
  484. // is stored internally then we use the Lob->isModified()
  485. // method to know whether contents changed.
  486. if (\$v instanceof Lob && \$v === \$this->$clo) {
  487. \$changed = \$v->isModified();
  488. } else {
  489. \$changed = (\$this->$clo !== \$v);
  490. }
  491. if (\$changed) {
  492. if ( !(\$v instanceof Lob) ) {
  493. \$obj = new $lobClass();
  494. \$obj->setContents(\$v);
  495. } else {
  496. \$obj = \$v;
  497. }
  498. \$this->$clo = \$obj;
  499. \$this->modifiedColumns[] = ".$this->getColumnConstant($col).";
  500. }
  501. ";
  502. $this->addMutatorClose($script, $col);
  503. } // addLobMutatorSnippet
  504. /**
  505. * Adds a setter method for date/time/timestamp columns.
  506. * @param string &$script The script will be modified in this method.
  507. * @param Column $col The current column.
  508. * @see parent::addColumnMutators()
  509. */
  510. protected function addTemporalMutator(&$script, Column $col)
  511. {
  512. $clo = strtolower($col->getName());
  513. $defaultValue = null;
  514. if (($val = $col->getPhpDefaultValue()) !== null) {
  515. settype($val, $col->getPhpNative());
  516. $defaultValue = var_export($val, true);
  517. }
  518. $this->addMutatorOpen($script, $col);
  519. $script .= "
  520. if (\$v !== null && !is_int(\$v)) {
  521. \$ts = strtotime(\$v);
  522. if (\$ts === -1 || \$ts === false) { // in PHP 5.1 return value changes to FALSE
  523. throw new PropelException(\"Unable to parse date/time value for [$clo] from input: \" . var_export(\$v, true));
  524. }
  525. } else {
  526. \$ts = \$v;
  527. }
  528. if (\$this->$clo !== \$ts";
  529. if ($defaultValue !== null) {
  530. $script .= " || \$ts === $defaultValue";
  531. }
  532. $script .= ") {
  533. \$this->$clo = \$ts;
  534. \$this->modifiedColumns[] = ".$this->getColumnConstant($col).";
  535. }
  536. ";
  537. $this->addMutatorClose($script, $col);
  538. }
  539. /**
  540. * Adds setter method for "normal" columns.
  541. * @param string &$script The script will be modified in this method.
  542. * @param Column $col The current column.
  543. * @see parent::addColumnMutators()
  544. */
  545. protected function addDefaultMutator(&$script, Column $col)
  546. {
  547. $clo = strtolower($col->getName());
  548. // FIXME: refactor this
  549. $defaultValue = null;
  550. if (($val = $col->getPhpDefaultValue()) !== null) {
  551. settype($val, $col->getPhpNative());
  552. $defaultValue = var_export($val, true);
  553. }
  554. $this->addMutatorOpen($script, $col);
  555. $script .= "
  556. if (\$this->$clo !== \$v";
  557. if ($defaultValue !== null) {
  558. $script .= " || \$v === $defaultValue";
  559. }
  560. $script .= ") {
  561. \$this->$clo = \$v;
  562. \$this->modifiedColumns[] = ".$this->getColumnConstant($col).";
  563. }
  564. ";
  565. $this->addMutatorClose($script, $col);
  566. }
  567. /**
  568. * Adds the hydrate() method, which sets attributes of the object based on a ResultSet.
  569. */
  570. protected function addHydrate(&$script)
  571. {
  572. $table = $this->getTable();
  573. $script .= "
  574. /**
  575. * Hydrates (populates) the object variables with values from the database resultset.
  576. *
  577. * An offset (1-based \"start column\") is specified so that objects can be hydrated
  578. * with a subset of the columns in the resultset rows. This is needed, for example,
  579. * for results of JOIN queries where the resultset row includes columns from two or
  580. * more tables.
  581. *
  582. * @param ResultSet \$rs The ResultSet class with cursor advanced to desired record pos.
  583. * @param int \$startcol 1-based offset column which indicates which restultset column to start with.
  584. * @return int next starting column
  585. * @throws PropelException - Any caught Exception will be rewrapped as a PropelException.
  586. */
  587. public function hydrate(ResultSet \$rs, \$startcol = 1)
  588. {
  589. try {
  590. ";
  591. $n = 0;
  592. foreach($table->getColumns() as $col) {
  593. if(!$col->isLazyLoad()) {
  594. $affix = CreoleTypes::getAffix(CreoleTypes::getCreoleCode($col->getType()));
  595. $clo = strtolower($col->getName());
  596. switch($col->getType()) {
  597. case PropelTypes::DATE:
  598. case PropelTypes::TIME:
  599. case PropelTypes::TIMESTAMP:
  600. $script .= "
  601. \$this->$clo = \$rs->get$affix(\$startcol + $n, null);
  602. ";
  603. break;
  604. default:
  605. $script .= "
  606. \$this->$clo = \$rs->get$affix(\$startcol + $n);
  607. ";
  608. }
  609. $n++;
  610. } // if col->isLazyLoad()
  611. } /* foreach */
  612. if ($this->getBuildProperty("addSaveMethod")) {
  613. $script .= "
  614. \$this->resetModified();
  615. ";
  616. }
  617. $script .= "
  618. \$this->setNew(false);
  619. // FIXME - using NUM_COLUMNS may be clearer.
  620. return \$startcol + $n; // $n = ".$this->getPeerClassname()."::NUM_COLUMNS - ".$this->getPeerClassname()."::NUM_LAZY_LOAD_COLUMNS).
  621. } catch (Exception \$e) {
  622. throw new PropelException(\"Error populating ".$table->getPhpName()." object\", \$e);
  623. }
  624. }
  625. ";
  626. } // addHydrate()
  627. /**
  628. *
  629. */
  630. protected function addBuildPkeyCriteria(&$script) {
  631. $script .= "
  632. /**
  633. * Builds a Criteria object containing the primary key for this object.
  634. *
  635. * Unlike buildCriteria() this method includes the primary key values regardless
  636. * of whether or not they have been modified.
  637. *
  638. * @return Criteria The Criteria object containing value(s) for primary key(s).
  639. */
  640. public function buildPkeyCriteria()
  641. {
  642. \$criteria = new Criteria(".$this->getPeerClassname()."::DATABASE_NAME);
  643. ";
  644. foreach ($this->getTable()->getColumns() as $col) {
  645. $clo = strtolower($col->getName());
  646. if ($col->isPrimaryKey()) {
  647. $script .= "
  648. \$criteria->add(".$this->getColumnConstant($col).", \$this->$clo);";
  649. }
  650. }
  651. $script .= "
  652. return \$criteria;
  653. }
  654. ";
  655. }
  656. /**
  657. *
  658. */
  659. protected function addBuildCriteria(&$script)
  660. {
  661. $script .= "
  662. /**
  663. * Build a Criteria object containing the values of all modified columns in this object.
  664. *
  665. * @return Criteria The Criteria object containing all modified values.
  666. */
  667. public function buildCriteria()
  668. {
  669. \$criteria = new Criteria(".$this->getPeerClassname()."::DATABASE_NAME);
  670. ";
  671. foreach ($this->getTable()->getColumns() as $col) {
  672. $clo = strtolower($col->getName());
  673. $script .= "
  674. if (\$this->isColumnModified(".$this->getColumnConstant($col).")) \$criteria->add(".$this->getColumnConstant($col).", \$this->$clo);";
  675. }
  676. $script .= "
  677. return \$criteria;
  678. }
  679. ";
  680. } // addBuildCriteria()
  681. protected function addToArray(&$script)
  682. {
  683. $script .= "
  684. /**
  685. * Exports the object as an array.
  686. *
  687. * You can specify the key type of the array by passing one of the class
  688. * type constants.
  689. *
  690. * @param string \$keyType One of the class type constants TYPE_PHPNAME,
  691. * TYPE_COLNAME, TYPE_FIELDNAME, TYPE_NUM
  692. * @return an associative array containing the field names (as keys) and field values
  693. */
  694. public function toArray(\$keyType = BasePeer::TYPE_PHPNAME)
  695. {
  696. \$keys = ".$this->getPeerClassname()."::getFieldNames(\$keyType);
  697. \$result = array(";
  698. foreach ($this->getTable()->getColumns() as $num => $col) {
  699. $script .= "
  700. \$keys[$num] => \$this->get".$col->getPhpName()."(),";
  701. }
  702. $script .= "
  703. );
  704. return \$result;
  705. }
  706. ";
  707. } // addToArray()
  708. protected function addGetByName(&$script)
  709. {
  710. $script .= "
  711. /**
  712. * Retrieves a field from the object by name passed in as a string.
  713. *
  714. * @param string \$name name
  715. * @param string \$type The type of fieldname the \$name is of:
  716. * one of the class type constants TYPE_PHPNAME,
  717. * TYPE_COLNAME, TYPE_FIELDNAME, TYPE_NUM
  718. * @return mixed Value of field.
  719. */
  720. public function getByName(\$name, \$type = BasePeer::TYPE_PHPNAME)
  721. {
  722. \$pos = ".$this->getPeerClassname()."::translateFieldName(\$name, \$type, BasePeer::TYPE_NUM);
  723. return \$this->getByPosition(\$pos);
  724. }
  725. ";
  726. }
  727. protected function addGetByPosition(&$script)
  728. {
  729. $table = $this->getTable();
  730. $script .= "
  731. /**
  732. * Retrieves a field from the object by Position as specified in the xml schema.
  733. * Zero-based.
  734. *
  735. * @param int \$pos position in xml schema
  736. * @return mixed Value of field at \$pos
  737. */
  738. public function getByPosition(\$pos)
  739. {
  740. switch(\$pos) {";
  741. $i = 0;
  742. foreach ($table->getColumns() as $col) {
  743. $cfc = $col->getPhpName();
  744. $cptype = $col->getPhpNative();// not safe to use it because some methods may return objects (Blob)
  745. $script .= "
  746. case $i:
  747. return \$this->get$cfc();
  748. break;";
  749. $i++;
  750. } /* foreach */
  751. $script .= "
  752. default:
  753. return null;
  754. break;
  755. } // switch()
  756. }
  757. ";
  758. }
  759. protected function addSetByName(&$script)
  760. {
  761. $table = $this->getTable();
  762. $script .= "
  763. /**
  764. * Sets a field from the object by name passed in as a string.
  765. *
  766. * @param string \$name peer name
  767. * @param mixed \$value field value
  768. * @param string \$type The type of fieldname the \$name is of:
  769. * one of the class type constants TYPE_PHPNAME,
  770. * TYPE_COLNAME, TYPE_FIELDNAME, TYPE_NUM
  771. * @return void
  772. */
  773. public function setByName(\$name, \$value, \$type = BasePeer::TYPE_PHPNAME)
  774. {
  775. \$pos = ".$this->getPeerClassname()."::translateFieldName(\$name, \$type, BasePeer::TYPE_NUM);
  776. return \$this->setByPosition(\$pos, \$value);
  777. }
  778. ";
  779. }
  780. protected function addSetByPosition(&$script)
  781. {
  782. $table = $this->getTable();
  783. $script .= "
  784. /**
  785. * Sets a field from the object by Position as specified in the xml schema.
  786. * Zero-based.
  787. *
  788. * @param int \$pos position in xml schema
  789. * @param mixed \$value field value
  790. * @return void
  791. */
  792. public function setByPosition(\$pos, \$value)
  793. {
  794. switch(\$pos) {";
  795. $i = 0;
  796. foreach ($table->getColumns() as $col) {
  797. $cfc = $col->getPhpName();
  798. $cptype = $col->getPhpNative();
  799. $script .= "
  800. case $i:
  801. \$this->set$cfc(\$value);
  802. break;";
  803. $i++;
  804. } /* foreach */
  805. $script .= "
  806. } // switch()
  807. }
  808. ";
  809. } // addSetByPosition()
  810. protected function addFromArray(&$script)
  811. {
  812. $table = $this->getTable();
  813. $script .= "
  814. /**
  815. * Populates the object using an array.
  816. *
  817. * This is particularly useful when populating an object from one of the
  818. * request arrays (e.g. \$_POST). This method goes through the column
  819. * names, checking to see whether a matching key exists in populated
  820. * array. If so the setByName() method is called for that column.
  821. *
  822. * You can specify the key type of the array by additionally passing one
  823. * of the class type constants TYPE_PHPNAME, TYPE_COLNAME, TYPE_FIELDNAME,
  824. * TYPE_NUM. The default key type is the column's phpname (e.g. 'authorId')
  825. *
  826. * @param array \$arr An array to populate the object from.
  827. * @param string \$keyType The type of keys the array uses.
  828. * @return void
  829. */
  830. public function fromArray(\$arr, \$keyType = BasePeer::TYPE_PHPNAME)
  831. {
  832. \$keys = ".$this->getPeerClassname()."::getFieldNames(\$keyType);
  833. ";
  834. foreach ($table->getColumns() as $num => $col) {
  835. $cfc = $col->getPhpName();
  836. $cptype = $col->getPhpNative();
  837. $script .= "
  838. if (array_key_exists(\$keys[$num], \$arr)) \$this->set$cfc(\$arr[\$keys[$num]]);";
  839. } /* foreach */
  840. $script .= "
  841. }
  842. ";
  843. } // addFromArray
  844. protected function addDelete(&$script)
  845. {
  846. $script .= "
  847. /**
  848. * Removes this object from datastore and sets delete attribute.
  849. *
  850. * @param Connection \$con
  851. * @return void
  852. * @throws PropelException
  853. * @see BaseObject::setDeleted()
  854. * @see BaseObject::isDeleted()
  855. */
  856. public function delete(\$con = null)
  857. {
  858. if (\$this->isDeleted()) {
  859. throw new PropelException(\"This object has already been deleted.\");
  860. }
  861. if (\$con === null) {
  862. \$con = Propel::getConnection(".$this->getPeerClassname()."::DATABASE_NAME);
  863. }
  864. try {
  865. \$con->begin();
  866. ".$this->getPeerClassname()."::doDelete(\$this, \$con);
  867. \$this->setDeleted(true);
  868. \$con->commit();
  869. } catch (PropelException \$e) {
  870. \$con->rollback();
  871. throw \$e;
  872. }
  873. }
  874. ";
  875. } // addDelete()
  876. /**
  877. * Adds the methods related to saving and deleting the object.
  878. * @param string &$script The script will be modified in this method.
  879. */
  880. protected function addManipulationMethods(&$script)
  881. {
  882. $this->addDelete($script);
  883. $this->addSave($script);
  884. }
  885. /**
  886. * Adds the methods related to validationg the object.
  887. * @param string &$script The script will be modified in this method.
  888. */
  889. protected function addValidationMethods(&$script)
  890. {
  891. $this->addValidationFailuresAttribute($script);
  892. $this->addGetValidationFailures($script);
  893. $this->addValidate($script);
  894. }
  895. /**
  896. * Adds the save() method.
  897. * @param string &$script The script will be modified in this method.
  898. */
  899. protected function addSave(&$script)
  900. {
  901. $table = $this->getTable();
  902. $script .= "
  903. /**
  904. * Stores the object in the database.
  905. *
  906. * If the object is new, it inserts it; otherwise an update is performed.
  907. *
  908. * @param Connection \$con
  909. * @return int The number of rows affected by this insert/update operation (for non-complex OM this will be at most 1).
  910. * @throws PropelException
  911. */
  912. public function save(\$con = null)
  913. {
  914. \$affectedRows = 0; // initialize var to track total num of affected rows
  915. // If this object has been modified, then save it to the database.
  916. if (\$this->isModified()) {
  917. if (\$this->isNew()) {
  918. \$pk = ".$this->getPeerClassname()."::doInsert(\$this, \$con);
  919. \$affectedRows += 1; // we are assuming that there is only 1 row per doInsert() which
  920. // should always be true here (even though technically
  921. // BasePeer::doInsert() can insert multiple rows).
  922. ";
  923. if ($table->getIdMethod() != "none") {
  924. if (count($pks = $table->getPrimaryKey())) {
  925. foreach ($pks as $pk) {
  926. if ($pk->isAutoIncrement()) {
  927. $script .= "
  928. \$this->set".$pk->getPhpName()."(\$pk); //[IMV] update autoincrement primary key
  929. ";
  930. }
  931. }
  932. }
  933. }
  934. $script .= "
  935. \$this->setNew(false);
  936. } else {
  937. \$affectedRows += ".$this->getPeerClassname()."::doUpdate(\$this, \$con);
  938. }
  939. \$this->resetModified(); // [HL] After being saved an object is no longer 'modified'
  940. } // if \$this->isModified()
  941. return \$affectedRows;
  942. } // save()
  943. ";
  944. } // addSave()
  945. /**
  946. * Adds the $validationFailures attribute to store ValidationFailed objects.
  947. * @param string &$script The script will be modified in this method.
  948. */
  949. protected function addValidationFailuresAttribute(&$script)
  950. {
  951. $script .= "
  952. /**
  953. * Array of ValidationFailed objects.
  954. * @var array ValidationFailed[]
  955. */
  956. protected \$validationFailures = array();
  957. ";
  958. }
  959. /**
  960. * Adds the validate() method.
  961. * @param string &$script The script will be modified in this method.
  962. */
  963. protected function addValidate(&$script)
  964. {
  965. $script .= "
  966. /**
  967. * Validates the objects modified field values.
  968. *
  969. * If \$columns is either a column name or an array of column names
  970. * only those columns are validated.
  971. *
  972. * @param mixed \$columns Column name or an array of column names.
  973. *
  974. * @return mixed <code>true</code> if all columns pass validation
  975. * or an array of <code>ValidationFailed</code> objects for columns that fail.
  976. */
  977. public function validate(\$columns = null)
  978. {
  979. if (\$columns) {
  980. return ".$this->getPeerClassname()."::doValidate(\$this, \$columns);
  981. }
  982. return ".$this->getPeerClassname()."::doValidate(\$this);
  983. }
  984. ";
  985. } // addValidate()
  986. /**
  987. * Adds the getValidationFailures() method.
  988. * @param string &$script The script will be modified in this method.
  989. */
  990. protected function addGetValidationFailures(&$script)
  991. {
  992. $script .= "
  993. /**
  994. * Gets any ValidationFailed objects that resulted from last call to validate().
  995. *
  996. *
  997. * @return array ValidationFailed[]
  998. * @see validate()
  999. */
  1000. public function getValidationFailures()
  1001. {
  1002. return \$this->validationFailures;
  1003. }
  1004. ";
  1005. } // addGetValidationFailures()
  1006. /**
  1007. * Adds the correct getPrimaryKey() method for this object.
  1008. * @param string &$script The script will be modified in this method.
  1009. */
  1010. protected function addGetPrimaryKey(&$script)
  1011. {
  1012. $pkeys = $this->getTable()->getPrimaryKey();
  1013. if (count($pkeys) == 1) {
  1014. $this->addGetPrimaryKey_SinglePK($script);
  1015. } elseif (count($pkeys) > 1) {
  1016. $this->addGetPrimaryKey_MultiPK($script);
  1017. } else {
  1018. // no primary key -- this is deprecated, since we don't *need* this method anymore
  1019. $this->addGetPrimaryKey_NoPK($script);
  1020. }
  1021. }
  1022. /**
  1023. * Adds the getPrimaryKey() method for tables that contain a single-column primary key.
  1024. * @param string &$script The script will be modified in this method.
  1025. */
  1026. protected function addGetPrimaryKey_SinglePK(&$script)
  1027. {
  1028. $table = $this->getTable();
  1029. $pkeys = $table->getPrimaryKey();
  1030. $cptype = $pkeys[0]->getPhpType();
  1031. $script .= "
  1032. /**
  1033. * Returns the primary key for this object (row).
  1034. * @return $cptype
  1035. */
  1036. public function getPrimaryKey()
  1037. {
  1038. return \$this->get".$pkeys[0]->getPhpName()."();
  1039. }
  1040. ";
  1041. } // addetPrimaryKey_SingleFK
  1042. /**
  1043. * Adds the setPrimaryKey() method for tables that contain a multi-column primary key.
  1044. * @param string &$script The script will be modified in this method.
  1045. */
  1046. protected function addGetPrimaryKey_MultiPK(&$script)
  1047. {
  1048. $script .= "
  1049. /**
  1050. * Returns the composite primary key for this object.
  1051. * The array elements will be in same order as specified in XML.
  1052. * @return array
  1053. */
  1054. public function getPrimaryKey()
  1055. {
  1056. \$pks = array();
  1057. ";
  1058. $i = 0;
  1059. foreach ($this->getTable()->getPrimaryKey() as $pk) {
  1060. $script .= "
  1061. \$pks[$i] = \$this->get".$pk->getPhpName()."();
  1062. ";
  1063. $i++;
  1064. } /* foreach */
  1065. $script .= "
  1066. return \$pks;
  1067. }
  1068. ";
  1069. } // addGetPrimaryKey_MultiFK()
  1070. /**
  1071. * Adds the getPrimaryKey() method for objects that have no primary key.
  1072. * This "feature" is dreprecated, since the getPrimaryKey() method is not required
  1073. * by the Persistent interface (or used by the templates). Hence, this method is also
  1074. * deprecated.
  1075. * @param string &$script The script will be modified in this method.
  1076. * @deprecated
  1077. */
  1078. protected function addGetPrimaryKey_NoPK(&$script)
  1079. {
  1080. $script .= "
  1081. /**
  1082. * Returns NULL since this table doesn't have a primary key.
  1083. * This method exists only for BC and is deprecated!
  1084. * @return null
  1085. */
  1086. public function getPrimaryKey()
  1087. {
  1088. return null;
  1089. }
  1090. ";
  1091. }
  1092. /**
  1093. * Adds the correct setPrimaryKey() method for this object.
  1094. * @param string &$script The script will be modified in this method.
  1095. */
  1096. protected function addSetPrimaryKey(&$script)
  1097. {
  1098. $pkeys = $this->getTable()->getPrimaryKey();
  1099. if (count($pkeys) == 1) {
  1100. $this->addSetPrimaryKey_SinglePK($script);
  1101. } elseif (count($pkeys) > 1) {
  1102. $this->addSetPrimaryKey_MultiPK($script);
  1103. } else {
  1104. // no primary key -- this is deprecated, since we don't *need* this method anymore
  1105. $this->addSetPrimaryKey_NoPK($script);
  1106. }
  1107. }
  1108. /**
  1109. * Adds the setPrimaryKey() method for tables that contain a single-column primary key.
  1110. * @param string &$script The script will be modified in this method.
  1111. */
  1112. protected function addSetPrimaryKey_SinglePK(&$script)
  1113. {
  1114. $pkeys = $this->getTable()->getPrimaryKey();
  1115. $col = $pkeys[0];
  1116. $clo=strtolower($col->getName());
  1117. $ctype = $col->getPhpNative();
  1118. $script .= "
  1119. /**
  1120. * Generic method to set the primary key ($clo column).
  1121. *
  1122. * @param $ctype \$key Primary key.
  1123. * @return void
  1124. */
  1125. public function setPrimaryKey(\$key)
  1126. {
  1127. \$this->set".$col->getPhpName()."(\$key);
  1128. }
  1129. ";
  1130. } // addSetPrimaryKey_SinglePK
  1131. /**
  1132. * Adds the setPrimaryKey() method for tables that contain a multi-columnprimary key.
  1133. * @param string &$script The script will be modified in this method.
  1134. */
  1135. protected function addSetPrimaryKey_MultiPK(&$script)
  1136. {
  1137. $script .="
  1138. /**
  1139. * Set the [composite] primary key.
  1140. *
  1141. * @param array \$keys The elements of the composite key (order must match the order in XML file).
  1142. * @return void
  1143. */
  1144. public function setPrimaryKey(\$keys)
  1145. {
  1146. ";
  1147. $i = 0;
  1148. foreach ($this->getTable()->getPrimaryKey() as $pk) {
  1149. $pktype = $pk->getPhpNative();
  1150. $script .= "
  1151. \$this->set".$pk->getPhpName()."(\$keys[$i]);
  1152. ";
  1153. $i++;
  1154. } /* foreach ($table->getPrimaryKey() */
  1155. $script .= "
  1156. }
  1157. ";
  1158. } // addSetPrimaryKey_MultiPK
  1159. /**
  1160. * Adds the setPrimaryKey() method for objects that have no primary key.
  1161. * This "feature" is dreprecated, since the setPrimaryKey() method is not required
  1162. * by the Persistent interface (or used by the templates). Hence, this method is also
  1163. * deprecated.
  1164. * @param string &$script The script will be modified in this method.
  1165. * @deprecated
  1166. */
  1167. protected function addSetPrimaryKey_NoPK(&$script)
  1168. {
  1169. $script .="
  1170. /**
  1171. * Dummy primary key setter.
  1172. *
  1173. * This function only exists to preserve backwards compatibility. It is no longer
  1174. * needed or required by the Persistent interface. It will be removed in next BC-breaking
  1175. * release of Propel.
  1176. *
  1177. * @deprecated
  1178. */
  1179. public function setPrimaryKey(\$pk)
  1180. {
  1181. // do nothing, because this object doesn't have any primary keys
  1182. }
  1183. ";
  1184. }
  1185. /**
  1186. * Adds the copy() method, which (in complex OM) includes the $deepCopy param for making copies of related objects.
  1187. * @param string &$script The script will be modified in this method.
  1188. */
  1189. protected function addCopy(&$script)
  1190. {
  1191. $this->addCopyInto($script);
  1192. $table = $this->getTable();
  1193. $script .= "
  1194. /**
  1195. * Makes a copy of this object that will be inserted as a new row in table when saved.
  1196. * It creates a new object filling in the simple attributes, but skipping any primary
  1197. * keys that are defined for the table.
  1198. *
  1199. * @return ".$table->getPhpName()." Clone of current object.
  1200. * @throws PropelException
  1201. */
  1202. public function copy()
  1203. {
  1204. // we use get_class(), because this might be a subclass
  1205. \$clazz = get_class(\$this);
  1206. \$copyObj = new \$clazz();
  1207. \$this->copyInto(\$copyObj);
  1208. return \$copyObj;
  1209. }
  1210. ";
  1211. } // addCopy()
  1212. /**
  1213. * Adds the copy() method.
  1214. */
  1215. protected function addCopyInto(&$script)
  1216. {
  1217. $table = $this->getTable();
  1218. $script .= "
  1219. /**
  1220. * Sets contents of passed object to values from current object.
  1221. *
  1222. * @param object \$copyObj An object of ".$table->getPhpName()." (or compatible) type.
  1223. * @return ".$table->getPhpName()." Clone of current object.
  1224. * @throws PropelException
  1225. */
  1226. public function copyInto(\$copyObj)
  1227. {
  1228. ";
  1229. $pkcols = array();
  1230. foreach ($table->getColumns() as $pkcol) {
  1231. if ($pkcol->isPrimaryKey()) {
  1232. $pkcols[] = $pkcol->getName();
  1233. }
  1234. }
  1235. foreach ($table->getColumns() as $col) {
  1236. if (!in_array($col->getName(), $pkcols)) {
  1237. $script .= "
  1238. \$copyObj->set<?php echo $col->getPhpName()?>(\$this-><?php echo strtolower($col->getName()) ?>);";
  1239. }
  1240. } // foreach
  1241. $script .= "
  1242. \$copyObj->setNew(true);";
  1243. foreach ($table->getColumns() as $col) {
  1244. if ($col->isPrimaryKey()) {
  1245. $coldefval = $col->getPhpDefaultValue();
  1246. $coldefval = var_export($coldefval, true);
  1247. $script .= "
  1248. \$copyObj->set".$col->getPhpName() ."($coldefval); // this is a pkey column, so set to default value";
  1249. } // if col->isPrimaryKey
  1250. } // foreach
  1251. $script .= "
  1252. return \$copyObj;
  1253. }
  1254. ";
  1255. } // addCopy()
  1256. } // PHP5BasicObjectBuilder