[ Index ]
 

Code source de Symfony 1.0.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/vendor/propel-generator/templates/om/php5/ -> Object.tpl (source)

   1  <?php
   2  
   3  // Template for creating an object class.

   4  //

   5  // This is based loosely on the Torque Object.vm Velocity template.

   6  //

   7  // $Id: Object.tpl,v 1.23 2005/03/17 01:16:42 hlellelid Exp $

   8  
   9   // helper classes

  10   include_once 'propel/engine/builder/om/PeerBuilder.php';
  11   include_once 'propel/engine/builder/om/ClassTools.php';
  12  
  13  echo '<' . '?' . 'php';
  14  
  15  $db = $table->getDatabase();
  16  if ($table->getPackage()) {
  17      $package = $table->getPackage();
  18  } else {
  19      $package = $targetPackage;
  20  }
  21  
  22  $parentClass = ClassTools::getBaseClass($table);
  23  ?>
  24  
  25  require_once '<?php echo ClassTools::getFilePath($parentClass) ?>';
  26  
  27  <?php
  28  $interface = ClassTools::getInterface($table);
  29  
  30  if (!empty($interface)) {
  31  ?>
  32  require_once '<?php echo ClassTools::getFilePath($interface) ?>';
  33  
  34  <?php
  35  }
  36  
  37  if (!$table->isAlias()) {
  38  
  39      // If any columns in table are BLOB or CLOB then we need to make

  40      // sure those classes are included so we can do things like

  41      // if ($v instanceof Lob) etc.

  42  
  43      $includes_lobs = false;
  44      foreach ($table->getColumns() as $col) {
  45          if ($col->isLob()) {
  46              $includes_lobs = true;
  47              break;
  48          }
  49      }
  50  
  51      if($includes_lobs) { ?>
  52  include_once 'creole/util/Clob.php';
  53  include_once 'creole/util/Blob.php';
  54  <?php
  55      }
  56  }
  57  ?>
  58  
  59  include_once 'propel/util/Criteria.php';
  60  <?php
  61      foreach ($table->getForeignKeys() as $fk) {
  62          $tblFK = $table->getDatabase()->getTable($fk->getForeignTableName());
  63          $className = $tblFK->getPhpName();
  64          if ($tblFK->getInterface()) {
  65              $className = $tblFK->getInterface();
  66          }
  67  
  68          $tblFKPackage = ($tblFK->getPackage() ? $tblFK->getPackage() : $package);
  69          $tblFKPackagePath = strtr($tblFKPackage, '.', '/');
  70          if ($tblFKPackagePath != "") {
  71              $tblFKPackagePath .= '/';
  72          } ?>
  73  
  74  // (on-demand) include_once '<?php echo ClassTools::getFilePath($tblFKPackage, $className) ?>';

  75  // (on-demand) include_once '<?php echo ClassTools::getFilePath($tblFKPackage, $tblFK->getPhpName() . 'Peer') ?>';

  76  <?php } ?>
  77  
  78  include_once '<?php echo ClassTools::getFilePath($package, $table->getPhpName() . 'Peer') ?>';
  79  
  80  /**

  81   * Base class that represents a row from the '<?php echo $table->getName() ?>' table.

  82   *

  83   * <?php echo $table->getDescription() ?> 

  84   *

  85  <?php if ($addTimeStamp) { ?>

  86   * This class was autogenerated by Propel on:

  87   *

  88   * [<?php echo $now ?>]

  89   *

  90  <?php } ?>

  91   * You should not use this class directly.  It should not even be

  92   * extended; all references should be to <?php echo $table->getPhpName() ?> class.

  93   *

  94   * @package <?php echo $package ?> 

  95   */
  96  abstract class <?php echo $basePrefix . $table->getPhpName() ?> extends <?php echo ClassTools::classname($parentClass) ?><?php if (!empty($interface)) { ?> implements <?php echo ClassTools::classname($interface) ?><?php } ?> {
  97  
  98      /**

  99       * The Peer class.

 100       * Instance provides a convenient way of calling static methods on a class

 101       * that calling code may not be able to identify.

 102       * @var <?php echo $table->getPhpName() ?>Peer

 103       */
 104      protected static $peer;
 105  <?php
 106      if (!$table->isAlias()) {
 107          foreach ($table->getColumns() as $col) {
 108              $cptype = $col->getPhpNative();
 109              $clo=strtolower($col->getName());
 110              $defVal = "";
 111              if (($val = $col->getPhpDefaultValue()) !== null) {
 112                  settype($val, $cptype);
 113                  $defaultValue = var_export($val, true);
 114                  $defVal = " = " . $defaultValue;
 115              }
 116  ?>
 117  
 118      /**

 119       * The value for the <?php echo $clo ?> field.

 120       * @var <?php echo $cptype ?> 

 121       */
 122      protected $<?php echo $clo . $defVal ?>;
 123  <?php         if ($col->isLazyLoad()) { ?>
 124  
 125      /**

 126       * Whether the lazy-loaded <?php echo $clo ?> value has been loaded from database.

 127       * This is necessary to avoid repeated lookups if <?php echo $clo ?> column is NULL.

 128       * @var boolean

 129       */
 130      protected $<?php echo $clo ?>_isLoaded = false;
 131  <?php
 132              }
 133          }
 134  
 135  foreach ($table->getColumns() as $col) {
 136  
 137      $cfc=$col->getPhpName();
 138      $clo=strtolower($col->getName());
 139      $cptype = $col->getPhpNative();
 140  
 141      $defaultValue = null;
 142      if (($val = $col->getPhpDefaultValue()) !== null) {
 143          settype($val, $cptype);
 144          $defaultValue = var_export($val, true);
 145      }
 146  
 147      if ($col->getType() === PropelTypes::DATE || $col->getType() === PropelTypes::TIME || $col->getType() === PropelTypes::TIMESTAMP) {
 148          // these default values are based on the Creole defaults

 149          // the date and time default formats are locale-sensitive

 150          if ($col->getType() === PropelTypes::DATE) {
 151              $defaultfmt = '%x';
 152          } elseif ($col->getType() === PropelTypes::TIME) {
 153              $defaultfmt = '%X';
 154          } elseif ($col->getType() === PropelTypes::TIMESTAMP) {
 155              $defaultfmt = 'Y-m-d H:i:s';
 156          }
 157  ?>
 158  
 159      /**

 160       * Get the [optionally formatted] `<?php echo $clo ?>` column value.

 161       * <?php echo $col->getDescription() ?> 

 162       * @param string $format The date/time format string (either date()-style or strftime()-style).

 163       *                            If format is NULL, then the integer unix timestamp will be returned.

 164       * @return mixed Formatted date/time value as string or integer unix timestamp (if format is NULL).

 165       * @throws PropelException - if unable to convert the date/time to timestamp.

 166       */
 167  	public function get<?php echo $cfc ?>($format = '<?php echo $defaultfmt ?>'<?php if ($col->isLazyLoad()) echo ', $con = null'; ?>)
 168      {
 169  <?php if ($col->isLazyLoad()) { ?>
 170          if (!$this-><?php echo $clo ?>_isLoaded && $this-><?php echo $clo ?> === null && !$this->isNew()) {
 171              $this->load<?php echo $cfc ?>($con);
 172          }
 173  <?php } ?>
 174          if ($this-><?php echo $clo ?> === null || $this-><?php echo $clo ?> === '') {
 175              return null;
 176          } elseif (!is_int($this-><?php echo $clo ?>)) {
 177              // a non-timestamp value was set externally, so we convert it

 178              $ts = strtotime($this-><?php echo $clo ?>);
 179              if ($ts === -1) {
 180                  throw new PropelException("Unable to parse value of <?php echo $clo ?> as date/time value: " . var_export($this-><?php echo $clo ?>, true));
 181              }
 182          } else {
 183              $ts = $this-><?php echo $clo ?>;
 184          }
 185          if ($format === null) {
 186              return $ts;
 187          } elseif (strpos($format, '%') !== false) {
 188              return strftime($format, $ts);
 189          } else {
 190              return date($format, $ts);
 191          }
 192      }
 193  <?php } else { ?>
 194      /**

 195       * Get the <?php echo $cfc ?> column value.

 196       * <?php echo $col->getDescription() ?> 

 197       * @return <?php echo $cptype ?> 

 198       */
 199  	public function get<?php echo $cfc ?>(<?php if ($col->isLazyLoad()) echo '$con = null'; ?>)
 200      {
 201  <?php if ($col->isLazyLoad()) { ?>
 202          if (!$this-><?php echo $clo ?>_isLoaded && $this-><?php echo $clo ?> === null && !$this->isNew()) {
 203              $this->load<?php echo $cfc ?>($con);
 204          }
 205  <?php } ?>
 206          return $this-><?php echo $clo ?>;
 207      }
 208  <?php } ?>
 209  
 210  <?php if ($col->isLazyLoad()) { ?>
 211      /**

 212       * Load the value for the [lazy-load] `<?php echo $clo ?>` column.

 213       *

 214       * This method performs an additional query to return the value for

 215       * the `<?php echo $clo ?>` column, since it is not populated by

 216       * the hydrate() method.

 217       *

 218       * @param Connection

 219       * @return void

 220       * @throws PropelException - any underlying error will be wrapped and re-thrown.

 221       */
 222  	protected function load<?php echo $cfc ?>($con = null)
 223      {
 224          $c = $this->buildPkeyCriteria();
 225          $c->addSelectColumn(<?php echo PeerBuilder::getColumnName($col, $table->getPhpName()) ?>);
 226          try {
 227              $rs = <?php echo $table->getPhpName()?>Peer::doSelectRS($c, $con);
 228              $rs->next();
 229  <?php
 230          $affix = CreoleTypes::getAffix(CreoleTypes::getCreoleCode($col->getType()));
 231           $clo = strtolower($col->getName());
 232           switch($col->getType()) {
 233  
 234               case PropelTypes::DATE:
 235               case PropelTypes::TIME:
 236               case PropelTypes::TIMESTAMP:
 237              ?>
 238              $this-><?php echo $clo ?> = $rs->get<?php echo $affix ?>(1, null);
 239  <?php
 240                  break;
 241              default:
 242                      ?>
 243              $this-><?php echo $clo?> = $rs->get<?php echo $affix ?>(1);
 244  <?php   } ?>
 245              $this-><?php echo $clo ?>_isLoaded = true;
 246          } catch (Exception $e) {
 247              throw new PropelException("Error loading value for `<?php echo $clo ?>` column on demand.", $e);
 248          }
 249      }
 250  
 251  <?php } ?>
 252  <?php
 253          if (!$table->isReadOnly()) {
 254  
 255  
 256              $throwsClause = "";
 257              if ($complexObjectModel) {
 258                  if ($col->isForeignKey()) {
 259                      $throwsClause = "@throws PropelException";
 260                  }
 261                  if (count($col->getReferrers()) > 0 ) {
 262                      if ($throwsClause == "") {
 263                          $throwsClause = "@throws PropelException";
 264                      }
 265                  }
 266              }
 267              if ($col->getType() === PropelTypes::DATE || $col->getType() === PropelTypes::TIME || $col->getType() === PropelTypes::TIMESTAMP) {
 268                  $throwsClause = "@throws PropelException - If passed [not-null] date/time is in an invalid format.";
 269              }
 270  
 271      ?>
 272  
 273      /**

 274       * Set the value of `<?php echo $clo ?>` column.

 275       * <?php echo $col->getDescription() ?> 

 276       * @param <?php echo ($col->getType() === PropelTypes::DATE || $col->getType() === PropelTypes::TIME || $col->getType() === PropelTypes::TIMESTAMP) ? 'mixed' : $cptype  ?> $v new value

 277       * @return void

 278       * <?php echo $throwsClause ?> 

 279       */
 280  	public function set<?php echo $cfc ?>($v)
 281      {
 282  <?php if ($col->isLazyLoad()) { ?>
 283          // explicitly set the is-loaded flag to true for this lazy load col;

 284          // it doesn't matter if the value is actually set or not (logic below) as

 285          // any attempt to set the value means that no db lookup should be performed

 286          // when the get<?php echo $cfc ?>() method is called.

 287          $this-><?php echo $clo ?>_isLoaded = true;
 288  <?php } ?>
 289  <?php
 290      if ($addSaveMethod) {
 291  
 292          if ($col->isLob()) {
 293              // Setting of LOB columns gets some special handling

 294  
 295              if ($col->getPropelType() === PropelTypes::BLOB || $col->getPropelType() === PropelTypes::LONGVARBINARY ) {
 296                  $lobClass = "Blob";
 297              } else {
 298                  $lobClass = "Clob";
 299              }
 300          ?>
 301          // if the passed in parameter is the *same* object that

 302          // is stored internally then we use the Lob->isModified()

 303          // method to know whether contents changed.

 304          if ($v instanceof Lob && $v === $this-><?php echo $clo ?>) {
 305              $changed = $v->isModified();
 306          } else {
 307              $changed = ($this-><?php echo $clo ?> !== $v);
 308          }
 309          if ($changed) {
 310              if ( !($v instanceof Lob) ) {
 311                  $obj = new <?php echo $lobClass ?>();
 312                  $obj->setContents($v);
 313              } else {
 314                  $obj = $v;
 315              }
 316              $this-><?php echo $clo ?> = $obj;
 317              $this->modifiedColumns[] = <?php echo PeerBuilder::getColumnName($col, $table->getPhpName()) ?>;
 318          }
 319  <?php   } elseif ($col->getType() === PropelTypes::DATE || $col->getType() === PropelTypes::TIME || $col->getType() === PropelTypes::TIMESTAMP) {
 320          // Setting a DATE/TIME value gets some special handling.

 321  ?>
 322          if ($v !== null && !is_int($v)) {
 323              $ts = strtotime($v);
 324              if ($ts === -1) {
 325                  throw new PropelException("Unable to parse date/time value for <?php echo $clo ?> from input: " . var_export($v, true));
 326              }
 327          } else {
 328              $ts = $v;
 329          }
 330          if ($this-><?php echo $clo ?> !== $ts<?php if ($defaultValue !== null) { ?> || $ts === <?php echo $defaultValue ?><?php } ?>) {
 331              $this-><?php echo $clo ?> = $ts;
 332              $this->modifiedColumns[] = <?php echo PeerBuilder::getColumnName($col, $table->getPhpName()) ?>;
 333          }
 334  <?php   } else {
 335          // NORMAL column

 336  ?>
 337          if ($this-><?php echo $clo ?> !== $v<?php if ($defaultValue !== null) { ?> || $v === <?php echo $defaultValue ?><?php } ?>) {
 338              $this-><?php echo $clo ?> = $v;
 339              $this->modifiedColumns[] = <?php echo PeerBuilder::getColumnName($col, $table->getPhpName()) ?>;
 340          }
 341          <?php }
 342      } else { ?>
 343          $this-><?php echo $clo ?> = $v;
 344  <?php
 345      } // if (addSaveMethod)

 346  
 347          if ($complexObjectModel) {
 348              if ($col->isForeignKey()) {
 349                  $tblFK = $table->getDatabase()->getTable($col->getRelatedTableName());
 350                  $colFK = $tblFK->getColumn($col->getRelatedColumnName());
 351                  if ($col->isMultipleFK() || $col->getRelatedTableName() == $table->getName()) {
 352                      $relCol = "";
 353                      foreach ($col->getForeignKey()->getLocalColumns() as $columnName) {
 354                          $column = $table->getColumn($columnName);
 355                          $relCol .= $column->getPhpName();
 356                      }
 357                      if ($relCol != "") {
 358                          $relCol = "RelatedBy".$relCol;
 359                      }
 360                      $varName = "a".$tblFK->getPhpName() . $relCol;
 361                  } else {
 362                      $varName = "a".$tblFK->getPhpName();
 363                  }
 364  
 365      ?>
 366  
 367          if ($this-><?php echo $varName ?> !== null && $this-><?php echo $varName ?>->get<?php echo $colFK->getPhpName() ?>() !== $v) {
 368              $this-><?php echo $varName ?> = null;
 369          }
 370      <?php     } /* if col is foreign key */
 371  
 372          foreach ($col->getReferrers() as $fk) {
 373              // used to be getLocalForeignMapping() which did not work.

 374              $flmap = $fk->getForeignLocalMapping();
 375              $fkColName = $flmap[$col->getName()];
 376              $tblFK = $fk->getTable();
 377              if ( $tblFK->getName() != $table->getName() ) {
 378                  $colFK = $tblFK->getColumn($fkColName);
 379                  if ($colFK->isMultipleFK()) {
 380                      $collName = "coll" . $tblFK->getPhpName() . "sRelatedBy" . $colFK->getPhpName();
 381                  } else {
 382                      $collName = "coll" . $tblFK->getPhpName() . "s";
 383                  }
 384      ?>
 385  
 386            // update associated <?php echo $tblFK->getPhpName() ?>

 387  
 388            if ($this-><?php echo $collName ?> !== null) {
 389                for ($i=0,$size=count($this-><?php echo $collName ?>); $i < $size; $i++) {
 390                    $this-><?php echo $collName ?>[$i]->set<?php echo $colFK->getPhpName()?>($v);
 391                }
 392            }
 393          <?php } /* if  $tblFk != $table */ ?>
 394        <?php } /* foreach referrers */ ?>
 395      <?php } /* if complex object model */ ?>
 396  
 397      }
 398  
 399  <?php
 400          } /* if !table->isReadOnly() */
 401      } /* foreach col */
 402  ?>
 403  
 404  
 405      /**

 406       * Hydrates (populates) the object variables with values from the database resultset.

 407       *

 408       * An offset (1-based "start column") is specified so that objects can be hydrated

 409       * with a subset of the columns in the resultset rows.  This is needed, for example,

 410       * for results of JOIN queries where the resultset row includes columns from two or

 411       * more tables.

 412       *

 413       * @param ResultSet $rs The ResultSet class with cursor advanced to desired record pos.

 414       * @param int $startcol 1-based offset column which indicates which restultset column to start with.

 415       * @return int next starting column

 416       * @throws PropelException  - Any caught Exception will be rewrapped as a PropelException.

 417       */
 418  	public function hydrate(ResultSet $rs, $startcol = 1)
 419      {
 420          try {
 421  <?php
 422              $n = 0;
 423              foreach($table->getColumns() as $col) {
 424                  if(!$col->isLazyLoad()) {
 425                      $affix = CreoleTypes::getAffix(CreoleTypes::getCreoleCode($col->getType()));
 426                      $clo = strtolower($col->getName());
 427                      switch($col->getType()) {
 428  
 429                          case PropelTypes::DATE:
 430                          case PropelTypes::TIME:
 431                          case PropelTypes::TIMESTAMP:
 432                      ?>
 433              $this-><?php echo $clo ?> = $rs->get<?php echo $affix ?>($startcol + <?php echo $n ?>, null);
 434  <?php
 435                              break;
 436                          default:
 437                      ?>
 438              $this-><?php echo $clo?> = $rs->get<?php echo $affix ?>($startcol + <?php echo $n ?>);
 439  <?php
 440                      }
 441                      $n++;
 442                  } // if col->isLazyLoad()

 443              } /* foreach */
 444          ?>
 445  <?php       if ($addSaveMethod) { ?>
 446              $this->resetModified();
 447  <?php       } ?>
 448              $this->setNew(false);
 449  
 450        return $startcol + <?php echo $n; ?>;
 451  
 452          } catch (Exception $e) {
 453              throw new PropelException("Error populating <?php echo $table->getPhpName()?> object", $e);
 454          }
 455  
 456      }
 457  
 458      /**

 459       * Builds a Criteria object containing the primary key for this object.

 460       *

 461       * Unlike buildCriteria() this method includes the primary key values regardless

 462       * of whether or not they have been modified.

 463       *

 464       * @return Criteria The Criteria object containing value(s) for primary key(s).

 465       */
 466  	public function buildPkeyCriteria()
 467      {
 468          $criteria = new Criteria(<?php echo $table->getPhpName()?>Peer::DATABASE_NAME);
 469  <?php
 470                  foreach ($table->getColumns() as $col) {
 471                      $clo = strtolower($col->getName());
 472                      if ($col->isPrimaryKey()) { ?>
 473          $criteria->add(<?php echo PeerBuilder::getColumnName($col, $table->getPhpName()) ?>, $this-><?php echo $clo ?>);
 474  <?php
 475                      }
 476                  }
 477              ?>
 478          return $criteria;
 479      }
 480  
 481      /**

 482       * Build a Criteria object containing the values of all modified columns in this object.

 483       *

 484       * @return Criteria The Criteria object containing all modified values.

 485       */
 486  	public function buildCriteria()
 487      {
 488          $criteria = new Criteria(<?php echo $table->getPhpName()?>Peer::DATABASE_NAME);
 489  <?php
 490                  foreach ($table->getColumns() as $col) {
 491                      $clo = strtolower($col->getName());
 492                      ?>
 493          if ($this->isColumnModified(<?php echo PeerBuilder::getColumnName($col, $table->getPhpName()) ?>)) $criteria->add(<?php echo PeerBuilder::getColumnName($col, $table->getPhpName()) ?>, $this-><?php echo $clo ?>);
 494  <?php
 495                  }
 496              ?>
 497          return $criteria;
 498      }
 499  
 500  <?php } /* if !table->isAlias */ ?>
 501  
 502  <?php
 503  
 504   // association code

 505  
 506  if ($complexObjectModel) {
 507  
 508      $pVars = array();  // Array of object set method names for later reference.

 509      $aVars = array();  // Array of object field names for later reference.

 510      foreach ($table->getForeignKeys() as $fk) {
 511  
 512          $tblFK = $table->getDatabase()->getTable($fk->getForeignTableName());
 513          $className = $tblFK->getPhpName();
 514  
 515          $tblFKPackage = ($tblFK->getPackage() ? $tblFK->getPackage() : $package);
 516          $tblFKPackagePath = strtr($tblFKPackage, '.', '/');
 517          if ($tblFKPackagePath != "") {
 518              $tblFKPackagePath .= '/';
 519          }
 520  
 521          $relCol = "";
 522          foreach ($fk->getLocalColumns() as $columnName) {
 523              $column = $table->getColumn($columnName);
 524              if ($column->isMultipleFK() || $fk->getForeignTableName() == $table->getName()) {
 525                  $relCol .= $column->getPhpName();
 526              }
 527          }
 528  
 529          if ($relCol != "") {
 530              $relCol = "RelatedBy" . $relCol;
 531          }
 532  
 533          $pVarName = $className . $relCol;
 534          $varName = "a" . $pVarName;
 535          $retVal = ($pVars[] = $pVarName);
 536          $retVal = ($aVars[] = $varName);
 537  ?>
 538  
 539      /**

 540       * @var <?php echo $className ?> 

 541       */
 542      protected $<?php echo $varName ?>;
 543  
 544      /**

 545       * Declares an association between this object and a <?php echo $className ?> object

 546       *

 547       * @param <?php echo $className ?> $v

 548       * @return void

 549       * @throws PropelException

 550       */
 551  	public function set<?php echo $pVarName ?>($v)
 552      {
 553  <?php
 554          foreach ($fk->getLocalColumns() as $columnName) {
 555              $column = $table->getColumn($columnName);
 556              $lfmap = $fk->getLocalForeignMapping();
 557              $colFKName = $lfmap[$columnName];
 558              $colFK = $tblFK->getColumn($colFKName); ?>
 559  
 560          if ($v === null) {
 561              $this->set<?php echo $column->getPhpName() ?>(<?php var_export($column->getPhpDefaultValue()) ?>);
 562          } else {
 563              $this->set<?php echo $column->getPhpName() ?>($v->get<?php echo $colFK->getPhpName() ?>());
 564          }
 565  <?php
 566          } /* foreach local col */
 567  ?>
 568  
 569          $this-><?php echo $varName ?> = $v;
 570      }
 571  
 572  <?php
 573      $and = "";
 574      $comma = "";
 575      $conditional = "";
 576      $arglist = "";
 577      $argsize = 0;
 578      foreach ($fk->getLocalColumns() as $columnName) {
 579          $column = $table->getColumn($columnName);
 580          $cptype = $column->getPhpNative();
 581          $clo = strtolower($column->getName());
 582          if ($cptype == "integer" || $cptype == "float" || $cptype == "double") {
 583              $conditional .= $and . "\$this->". $clo ." > 0";
 584          } elseif($cptype == "string") {
 585              $conditional .= $and . "(\$this->" . $clo ." !== \"\" && \$this->".$clo." !== null)";
 586          } else {
 587              $conditional .= $and . "\$this->" . $clo ." !== null";
 588          }
 589          $arglist .= $comma . "\$this->" . $clo;
 590          $and = " && ";
 591          $comma = ", ";
 592          $argsize = $argsize + 1;
 593     }
 594  
 595     $pCollName = $table->getPhpName() . 's' . $relCol;
 596  ?>
 597  
 598      /**

 599       * Get the associated <?php echo $className ?> object

 600       *

 601       * @param Connection Optional Connection object.

 602       * @return <?php echo $className ?> The associated <?php echo $className ?> object.

 603       * @throws PropelException

 604       */
 605  	public function get<?php echo $pVarName ?>($con = null)
 606      {
 607          // include the Peer class

 608          include_once '<?php echo $tblFKPackagePath . $className ?>Peer.php';
 609  
 610          if ($this-><?php echo $varName ?> === null && (<?php echo $conditional ?>)) {
 611  <?php
 612          if ($tblFK->isAlias()) {
 613              if ($argsize > 1) {
 614  ?>
 615  
 616              $this-><?php echo $varName ?> = <?php echo $className ?>Peer::retrieve<?php echo $className ?>ByPK(<?php echo $arglist ?>, $con);
 617      <?php    } else { ?>
 618  
 619              $this-><?php echo $varName ?> = <?php echo $className ?>Peer::retrieve<?php echo $className ?>ByPK(<?php echo $arglist ?>, $con);
 620      <?php    }
 621          } else {
 622              if ($argsize > 1) { ?>
 623  
 624              $this-><?php echo $varName ?> = <?php echo $className ?>Peer::retrieveByPK(<?php echo $arglist ?>, $con);
 625      <?php    } else { ?>
 626  
 627              $this-><?php echo $varName ?> = <?php echo $className ?>Peer::retrieveByPK(<?php echo $arglist ?>, $con);
 628      <?php }
 629          } // if tblFK->isAlias ?>

 630  
 631              /* The following can be used instead of the line above to

 632                 guarantee the related object contains a reference

 633                 to this object, but this level of coupling

 634                 may be undesirable in many circumstances.

 635                 As it can lead to a db query with many results that may

 636                 never be used.

 637                 $obj = <?php echo $className ?>Peer::retrieveByPK(<?php echo $arglist ?>, $con);

 638                 $obj->add<?php echo $pCollName ?>($this);

 639               */
 640          }
 641          return $this-><?php echo $varName ?>;
 642      }
 643  
 644      /**

 645       * Provides convenient way to set a relationship based on a

 646       * key.  e.g.

 647       * <code>$bar->setFooKey($foo->getPrimaryKey())</code>

 648       *

 649  <?php if (count($fk->getLocalColumns()) > 1) { ?>

 650       * Note: It is important that the xml schema used to create this class

 651       * maintains consistency in the order of related columns between

 652       * <?php echo $table->getName() ?> and <?php echo $tblFK->getName() ?>.

 653       * If for some reason this is impossible, this method should be

 654       * overridden in <code><?php echo $table->getPhpName() ?></code>.

 655  <?php } ?>

 656       * @return void

 657       * @throws PropelException

 658       */
 659  	public function set<?php echo $pVarName ?>Key($key)
 660      {
 661  <?php if (count($fk->getLocalColumns()) > 1) {
 662          $i = 0;
 663          foreach ($fk->getLocalColumns() as $colName) {
 664              $col = $table->getColumn($colName);
 665              $fktype = $col->getPhpNative();
 666  ?>
 667  
 668              $this->set<?php echo $col->getPhpName() ?>( (<?php echo $fktype ?>) $key[<?php echo $i ?>] );
 669  <?php
 670              $i++;
 671          } /* foreach */
 672      } else {
 673          $lcols = $fk->getLocalColumns();
 674          $colName = $lcols[0];
 675          $col = $table->getColumn($colName);
 676          $fktype = $col->getPhpNative();
 677  ?>
 678  
 679          $this->set<?php echo $col->getPhpName() ?>( (<?php echo $fktype ?>) $key);
 680      <?php } ?>
 681  
 682      }
 683  <?php } /*  end of foreach loop over foreign keys */
 684  
 685   //

 686   // setup foreign key associations

 687   //

 688      foreach ($table->getReferrers() as $fk) {
 689          $tblFK = $fk->getTable();
 690          $tblFKPackage = ($tblFK->getPackage() ? $tblFK->getPackage() : $package);
 691          $tblFKPackagePath = strtr($tblFKPackage, '.', '/');
 692          if ($tblFKPackagePath != "") {
 693              $tblFKPackagePath .= '/';
 694          }
 695  
 696          $className = $tblFK->getPhpName();
 697          $relatedByCol = "";
 698          foreach ($fk->getLocalColumns() as $columnName) {
 699              $column = $tblFK->getColumn($columnName);
 700              if ($column->isMultipleFK() || $tblFK->getName() == $table->getName()) {
 701                  // if there are seeral foreign keys that point to the same table

 702                  // then we need to generate methods like getAuthorRelatedByColName()

 703                  // instead of just getAuthor().  Currently we are doing the same

 704                  // for self-referential foreign keys, to avoid confusion.

 705                  $relatedByCol .= $column->getPhpName();
 706              }
 707          }
 708  
 709          if ($relatedByCol == "") {
 710              $suffix = "";
 711              $relCol = $className . "s";
 712              $relColMs = $className;
 713          } else {
 714              $suffix = "RelatedBy" . $relatedByCol;
 715              $relCol= $className . "sRelatedBy" . $relatedByCol;
 716              $relColMs= $className . "RelatedBy" . $relatedByCol;
 717          }
 718          $collName = "coll" . $relCol;
 719  ?>
 720  
 721      /**

 722       * Collection to store aggregation of <?php echo $collName ?> 

 723       * @var array

 724       */
 725      protected $<?php echo $collName ?>;
 726  
 727      /**

 728       * Temporary storage of <?php echo $collName ?> to save a possible db hit in

 729       * the event objects are add to the collection, but the

 730       * complete collection is never requested.

 731       * @return void

 732       */
 733  	public function init<?php echo $relCol ?>()
 734      {
 735          if ($this-><?php echo $collName ?> === null) {
 736              $this-><?php echo $collName ?> = array();
 737          }
 738      }
 739  
 740      /**

 741       * Method called to associate a <?php echo $tblFK->getPhpName() ?> object to this object

 742       * through the <?php echo $className ?> foreign key attribute

 743       *

 744       * @param <?php echo $className ?> $l $className

 745       * @return void

 746       * @throws PropelException

 747       */
 748  	public function add<?php echo $relColMs ?>(<?php echo $className ?> $l)
 749      {
 750          $this-><?php echo $collName ?>[] = $l;
 751          $l->set<?php echo $table->getPhpName() . $suffix ?>($this);
 752      }
 753  
 754      /**

 755       * The criteria used to select the current contents of <?php echo $collName ?>.

 756       * @var Criteria

 757       */
 758      private $last<?php echo $relCol ?>Criteria = null;
 759  
 760      /**

 761       * Returns the number of related <?php echo $relCol ?> 

 762       *

 763       * @param Criteria $criteria

 764       * @param boolean $distinct

 765       * @param Connection $con

 766       * @throws PropelException

 767       */
 768  	public function count<?php echo $relCol ?>($criteria = null, $distinct = false, $con = null)
 769      {
 770          // include the Peer class

 771          include_once '<?php echo $tblFKPackagePath . $className ?>Peer.php';
 772          if ($criteria === null) {
 773              $criteria = new Criteria();
 774          }
 775  <?php
 776      foreach ($fk->getForeignColumns() as $columnName) {
 777          $column = $table->getColumn($columnName);
 778          // used to be getLocalForeignMapping() but that didn't seem to work

 779          // (maybe a problem in translation of HashTable code to PHP).

 780          $flmap = $fk->getForeignLocalMapping();
 781          $colFKName = $flmap[$columnName];
 782          $colFK = $tblFK->getColumn($colFKName);
 783  ?>
 784          $criteria->add(<?php echo PeerBuilder::getColumnName($colFK, $className) ?>, $this->get<?php echo $column->getPhpName() ?>() );
 785  <?php
 786      } // end foreach ($fk->getForeignColumns()

 787  ?>
 788          return <?php echo $className ?>Peer::doCount($criteria, $distinct, $con);
 789      }
 790  
 791      /**

 792       * If this collection has already been initialized with

 793       * an identical criteria, it returns the collection.

 794       * Otherwise if this <?php echo $table->getPhpName() ?> has previously

 795       * been saved, it will retrieve related <?php echo $relCol ?> from storage.

 796       * If this <?php echo $table->getPhpName() ?> is new, it will return

 797       * an empty collection or the current collection, the criteria

 798       * is ignored on a new object.

 799       *

 800       * @param Connection $con

 801       * @param Criteria $criteria

 802       * @throws PropelException

 803       */
 804  	public function get<?php echo $relCol ?>($criteria = null, $con = null)
 805      {
 806          // include the Peer class

 807          include_once '<?php echo $tblFKPackagePath . $className ?>Peer.php';
 808          if ($criteria === null) {
 809              $criteria = new Criteria();
 810          }
 811  
 812          if ($this-><?php echo $collName ?> === null) {
 813              if ($this->isNew()) {
 814                 $this-><?php echo $collName ?> = array();
 815              } else {
 816  <?php
 817      foreach ($fk->getForeignColumns() as $columnName) {
 818          $column = $table->getColumn($columnName);
 819          // used to be getLocalForeignMapping() but that didn't seem to work

 820          // (maybe a problem in translation of HashTable code to PHP).

 821          $flmap = $fk->getForeignLocalMapping();
 822          $colFKName = $flmap[$columnName];
 823          $colFK = $tblFK->getColumn($colFKName);
 824  ?>
 825  
 826                  $criteria->add(<?php echo PeerBuilder::getColumnName($colFK, $className) ?>, $this->get<?php echo $column->getPhpName() ?>() );
 827  <?php
 828      } // end foreach ($fk->getForeignColumns()

 829  ?>
 830  
 831                  $this-><?php echo $collName ?> = <?php echo $className ?>Peer::doSelect($criteria, $con);
 832              }
 833          } else {
 834              // criteria has no effect for a new object

 835              if (!$this->isNew()) {
 836                  // the following code is to determine if a new query is

 837                  // called for.  If the criteria is the same as the last

 838                  // one, just return the collection.

 839  <?php
 840      foreach ($fk->getForeignColumns() as $columnName) {
 841          $column = $table->getColumn($columnName);
 842          $flmap = $fk->getForeignLocalMapping();
 843          $colFKName = $flmap[$columnName];
 844          $colFK = $tblFK->getColumn($colFKName);
 845  ?>
 846  
 847                  $criteria->add(<?php echo PeerBuilder::getColumnName($colFK, $className) ?>, $this->get<?php echo $column->getPhpName() ?>());
 848  <?php
 849      } // foreach ($fk->getForeignColumns()

 850  ?>
 851  
 852                  if (!isset($this->last<?php echo $relCol ?>Criteria) || !$this->last<?php echo $relCol ?>Criteria->equals($criteria)) {
 853                      $this-><?php echo $collName ?> = <?php echo $className ?>Peer::doSelect($criteria, $con);
 854                  }
 855              }
 856          }
 857          $this->last<?php echo $relCol ?>Criteria = $criteria;
 858  
 859          return $this-><?php echo $collName ?>;
 860      }
 861  
 862  <?php
 863      $countFK = 0;
 864      foreach ($tblFK->getForeignKeys() as $dummyFK) {
 865          $countFK = $countFK + 1;
 866      }
 867  
 868  // ------------------------------------------------------------

 869  //

 870  
 871      if ($countFK >= 1) {
 872          $lastTable = "";
 873          foreach ($tblFK->getForeignKeys() as $fk2) {
 874              // Add join methods if the fk2 table is not this table or

 875              // the fk2 table references this table multiple times.

 876  
 877              $doJoinGet = true;
 878              if ( $fk2->getForeignTableName() == $table->getName() ) {
 879                  $doJoinGet = false;
 880              }
 881  
 882              foreach ($fk2->getLocalColumns() as $columnName) {
 883                  $column = $tblFK->getColumn($columnName);
 884                  if ($column->isMultipleFK()) {
 885                      $doJoinGet = true;
 886                  }
 887              }
 888  
 889              $tblFK2 = $table->getDatabase()->getTable($fk2->getForeignTableName());
 890              $doJoinGet = !$tblFK2->isForReferenceOnly();
 891              $relatedByCol2 = "";
 892              foreach ($fk2->getLocalColumns() as $columnName) {
 893                  $column = $tblFK->getColumn($columnName);
 894                  if ($column->isMultipleFK()) {
 895                      $relatedByCol2 .= $column->getPhpName();
 896                  }
 897              }
 898  
 899              $fkClassName = $tblFK2->getPhpName();
 900  
 901              // do not generate code for self-referencing fk's, it would be

 902              // good to do, but it is just not implemented yet.

 903              if ($className == $fkClassName) {
 904                  // $doJoinGet = false;  -- SELF REFERENCING FKs UNDER TESTING

 905              }
 906  
 907              if ($relatedByCol2 == "") {
 908                  $relCol2 = $fkClassName;
 909              } else {
 910                  $relCol2 = $fkClassName . "RelatedBy". $relatedByCol2;
 911              }
 912  
 913              if ( $relatedByCol == "") {
 914                  // nothing?

 915              } else {
 916                  if ( $relatedByCol == $relatedByCol2 ) {
 917                      $doJoinGet = false;
 918                  }
 919              }
 920  
 921              if ($doJoinGet) {
 922  ?>
 923  
 924      /**

 925       * If this collection has already been initialized with

 926       * an identical criteria, it returns the collection.

 927       * Otherwise if this <?php echo $table->getPhpName() ?> is new, it will return

 928       * an empty collection; or if this <?php echo $table->getPhpName() ?> has previously

 929       * been saved, it will retrieve related <?php echo $relCol ?> from storage.

 930       *

 931       * This method is protected by default in order to keep the public

 932       * api reasonable.  You can provide public methods for those you

 933       * actually need in <?php echo $table->getPhpName() ?>.

 934       */
 935  	public function get<?php echo $relCol ?>Join<?php echo $relCol2 ?>($criteria = null, $con = null)
 936      {
 937          // include the Peer class

 938          include_once '<?php echo $tblFKPackagePath . $className ?>Peer.php';
 939          if ($criteria === null) {
 940              $criteria = new Criteria();
 941          }
 942  
 943          if ($this-><?php echo $collName ?> === null) {
 944              if ($this->isNew()) {
 945                 $this-><?php echo $collName ?> = array();
 946              } else {
 947  <?php
 948          foreach ($fk->getForeignColumns() as $columnName) {
 949              $column = $table->getColumn($columnName);
 950              $flMap = $fk->getForeignLocalMapping();
 951              $colFKName = $flMap[$columnName];
 952              $colFK = $tblFK->getColumn($colFKName);
 953  ?>
 954                  $criteria->add(<?php echo PeerBuilder::getColumnName($colFK, $className) ?>, $this->get<?php echo $column->getPhpName() ?>());
 955  <?php
 956          } // end foreach ($fk->getForeignColumns()

 957  ?>
 958                  $this-><?php echo $collName ?> = <?php echo $className ?>Peer::doSelectJoin<?php echo $relCol2 ?>($criteria, $con);
 959              }
 960          } else {
 961              // the following code is to determine if a new query is

 962              // called for.  If the criteria is the same as the last

 963              // one, just return the collection.

 964  <?php
 965              foreach ($fk->getForeignColumns() as $columnName) {
 966                  $column = $table->getColumn($columnName);
 967                  $flMap = $fk->getForeignLocalMapping();
 968                  $colFKName = $flMap[$columnName];
 969                  $colFK = $tblFK->getColumn($colFKName);
 970  ?>
 971              $criteria->add(<?php echo PeerBuilder::getColumnName($colFK, $className) ?>, $this->get<?php echo $column->getPhpName() ?>());
 972  <?php
 973              } /* end foreach ($fk->getForeignColumns() */
 974  ?>
 975              if (!isset($this->last<?php echo $relCol ?>Criteria) || !$this->last<?php echo $relCol ?>Criteria->equals($criteria)) {
 976                  $this-><?php echo $collName ?> = <?php echo $className ?>Peer::doSelectJoin<?php echo $relCol2 ?>($criteria, $con);
 977              }
 978          }
 979          $this->last<?php echo $relCol ?>Criteria = $criteria;
 980  
 981          return $this-><?php echo $collName ?>;
 982      }
 983  <?php
 984      } /* end if($doJoinGet) */
 985  
 986      } /* end foreach ($tblFK->getForeignKeys() as $fk2) { */
 987      } /* end if countFK >= 1 */
 988  
 989      } /*ends foreach over table->getReferrers() */
 990  
 991  } /* the if(complexObjectModel) */
 992  
 993  //

 994  // add GenericAccessors or GenericMutators?

 995  //

 996  if (!$table->isAlias() && ($addGenericAccessors || ($addGenericMutators && !$table->isReadOnly())))
 997  {
 998  ?>
 999  
1000      /**

1001       * phpname type

1002       * e.g. 'AuthorId'

1003       */
1004      const TYPE_PHPNAME = 'phpName';
1005  
1006      /**

1007       * column (peer) name type

1008       * e.g. 'book.AUTHOR_ID'

1009       */
1010      const TYPE_COLNAME = 'colName';
1011  
1012      /**

1013       * column fieldname type

1014       * e.g. 'author_id'

1015       */
1016      const TYPE_FIELDNAME = 'fieldName';
1017  
1018      /**

1019       * num type

1020       * simply the numerical array index, e.g. 4

1021       */
1022      const TYPE_NUM = 'num';
1023  <?php
1024      $tableColumns = $table->getColumns();
1025      $tablePhpname = $table->getPhpName();
1026  ?>
1027  
1028      /**

1029       * holds an array of fieldnames

1030       *

1031       * first dimension keys are the type constants

1032       * e.g. self::$fieldNames[self::TYPE_PHPNAME][0] = 'Id'

1033       */
1034      private static $fieldNames = array (
1035          <?php echo $basePrefix . $table->getPhpName() ?>::TYPE_PHPNAME => array (<?php foreach ($tableColumns as $col) { ?>'<?php echo $col->getPhpName(); ?>', <?php } ?>),
1036          <?php echo $basePrefix . $table->getPhpName() ?>::TYPE_COLNAME => array (<?php foreach ($tableColumns as $col) { ?><?php echo PeerBuilder::getColumnName($col, $tablePhpname); ?>, <?php } ?>),
1037          <?php echo $basePrefix . $table->getPhpName() ?>::TYPE_FIELDNAME => array (<?php foreach ($tableColumns as $col) { ?>'<?php echo $col->getName(); ?>', <?php } ?>),
1038          <?php echo $basePrefix . $table->getPhpName() ?>::TYPE_NUM => array (<?php foreach ($tableColumns as $num => $col) { echo $num; ?>, <?php } ?>)
1039      );
1040  
1041      /**

1042       * holds an array of keys for quick access to the fieldnames array

1043       *

1044       * first dimension keys are the type constants

1045       * e.g. self::$fieldNames[self::TYPE_PHPNAME]['Id'] = 0

1046       */
1047      private static $fieldKeys = array (
1048          <?php echo $basePrefix . $table->getPhpName() ?>::TYPE_PHPNAME => array (<?php foreach ($tableColumns as $num => $col) { ?>'<?php echo $col->getPhpName(); ?>' => <?php echo $num; ?>, <?php } ?>),
1049          <?php echo $basePrefix . $table->getPhpName() ?>::TYPE_COLNAME => array (<?php foreach ($tableColumns as $num => $col) { ?><?php echo PeerBuilder::getColumnName($col, $tablePhpname); ?> => <?php echo $num; ?>, <?php } ?>),
1050          <?php echo $basePrefix . $table->getPhpName() ?>::TYPE_FIELDNAME => array (<?php foreach ($tableColumns as $num => $col) { ?>'<?php echo $col->getName(); ?>' => <?php echo $num; ?>, <?php } ?>),
1051          <?php echo $basePrefix . $table->getPhpName() ?>::TYPE_NUM => array (<?php foreach ($tableColumns as $num => $col) { echo $num; ?>, <?php } ?>)
1052      );
1053  
1054      /**

1055       * Returns an array of of field names.

1056       *

1057       * @param  string $type The type of fieldnames to return:

1058       *                      One of the class type constants TYPE_PHPNAME,

1059       *                      TYPE_COLNAME, TYPE_FIELDNAME, TYPE_NUM

1060       * @return array A list of field names

1061       */
1062  
1063  	static public function getFieldNames($type = self::TYPE_FIELDNAME)
1064      {
1065          if (!isset(self::$fieldNames[$type])) {
1066              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.');
1067          }
1068          return self::$fieldNames[$type];
1069      }
1070  
1071      /**

1072       * Translates a fieldname to another type

1073       *

1074       * @param string $name field name

1075       * @param string $fromType One of the class type constants TYPE_PHPNAME,

1076       *                         TYPE_COLNAME, TYPE_FIELDNAME, TYPE_NUM

1077       * @param string $toType   One of the class type constants

1078       * @return string translated name of the field.

1079       */
1080  	static public function translateFieldName($name, $fromType, $toType)
1081      {
1082          $toNames = self::getFieldNames($toType);
1083          $key = self::$fieldKeys[$fromType][$name];
1084          if ($key === false) {
1085              throw new PropelException("'$name' could not be found in the field names of type '$fromType'. These are: " . print_r($fromNames, true));
1086          }
1087          return $toNames[$key];
1088      }
1089  <?php if ($addGenericAccessors) { ?>
1090  
1091      /**

1092       * Retrieves a field from the object by name passed in as a string.

1093       *

1094       * @param string $name name

1095       * @param string $type The type of fieldname the $name is of:

1096       *                     one of the class type constants TYPE_PHPNAME,

1097       *                     TYPE_COLNAME, TYPE_FIELDNAME, TYPE_NUM

1098       * @return mixed Value of field.

1099       */
1100  	public function getByName($name, $type = self::TYPE_COLNAME)
1101      {
1102          $names = self::getFieldNames($type);
1103          $pos = self::translateFieldName($name, $type, self::TYPE_NUM);
1104          return $this->getByPosition($pos);
1105      }
1106  
1107      /**

1108       * Retrieves a field from the object by Position as specified in the xml schema.

1109       * Zero-based.

1110       *

1111       * @param int $pos position in xml schema

1112       * @return mixed Value of field at $pos

1113       */
1114  	public function getByPosition($pos)
1115      {
1116          switch($pos) {
1117  <?php
1118          $i = 0;
1119          foreach ($table->getColumns() as $col) {
1120              $cfc = $col->getPhpName();
1121              $cptype = $col->getPhpNative();// not safe to use it because some methods may return objects (Blob)

1122  ?>
1123              case <?php echo $i ?>:
1124                  return $this->get<?php echo $cfc ?>();
1125                  break;
1126  <?php
1127              $i++;
1128          } /* foreach */
1129  ?>
1130              default:
1131                  return null;
1132          } // switch()

1133      }
1134  
1135      /**

1136       * Exports the object as an array.

1137       *

1138       * You can specify the key type of the array by passing one of the class

1139       * type constants.

1140       *

1141       * @param string $keyType One of the class type constants TYPE_PHPNAME,

1142       *                        TYPE_COLNAME, TYPE_FIELDNAME, TYPE_NUM

1143       * @return an associative array containing the field names (as keys) and field values

1144       */
1145  	public function toArray($keyType = self::TYPE_PHPNAME)
1146      {
1147          $keys = self::getFieldNames($keyType);
1148          $result = array(
1149  <?php
1150          foreach ($table->getColumns() as $num => $col) {
1151  ?>
1152              $keys[<?php echo $num; ?>] => $this->get<?php echo $col->getPhpName(); ?>(),
1153  <?php
1154          } /* foreach */
1155  ?>
1156          );
1157          return $result;
1158      }
1159  <?php } /* ends the if($addGenericAccessors) */ ?>
1160  
1161  
1162  <?php if ($addGenericMutators && !$table->isReadOnly()) { ?>
1163  
1164      /**

1165       * Sets a field from the object by name passed in as a string.

1166       *

1167       * @param string $name peer name

1168       * @param mixed $value field value

1169       * @param string $type The type of fieldname the $name is of:

1170       *                     one of the class type constants TYPE_PHPNAME,

1171       *                     TYPE_COLNAME, TYPE_FIELDNAME, TYPE_NUM

1172       * @return void

1173       */
1174  	public function setByName($name, $value, $type = self::TYPE_COLNAME)
1175      {
1176          $names = $this->getFieldnames($type);
1177          $pos = array_search($name, $names);
1178          return $this->setByPosition($pos, $value);
1179      }
1180  
1181      /**

1182       * Sets a field from the object by Position as specified in the xml schema.

1183       * Zero-based.

1184       *

1185       * @param int $pos position in xml schema

1186       * @param mixed $value field value

1187       * @return void

1188       */
1189  	public function setByPosition($pos, $value)
1190      {
1191          switch($pos) {
1192  <?php
1193          $i = 0;
1194          foreach ($table->getColumns() as $col) {
1195              $cfc = $col->getPhpName();
1196              $cptype = $col->getPhpNative();
1197  ?>
1198              case <?php echo $i ?>:
1199                  $this->set<?php echo $cfc ?>($value);
1200                  break;
1201  <?php
1202              $i++;
1203          } /* foreach */
1204  ?>
1205          } // switch()

1206      }
1207  
1208      /**

1209       * Populates the object using an array.

1210       *

1211       * This method is just an alias for populateFromArray()

1212       *

1213       * @param array  $arr     An array to populate the object from.

1214       * @param string $keyType The type of keys the array uses:

1215       *                        one of the class type constants TYPE_PHPNAME,

1216       *                        TYPE_COLNAME, TYPE_FIELDNAME, TYPE_NUM

1217       * @return void

1218       */
1219  	public function fromArray($arr, $keyType = self::TYPE_COLNAME)
1220      {
1221          return $this->populateFromArray($arr, $keyType);
1222      }
1223  
1224      /**

1225       * Populates the object using an array.

1226       *

1227       * This is particularly useful when populating an object from one of the

1228       * request arrays (e.g. $_POST).  This method goes through the column

1229       * names, checking to see whether a matching key exists in populated

1230       * array. If so the setByName() method is called for that column.

1231       *

1232       * You can specify the key type of the array by additionally passing one

1233       * of the class type constants TYPE_PHPNAME, TYPE_COLNAME, TYPE_FIELDNAME,

1234       * TYPE_NUM. The default key type is the (peer) column name (e.g.

1235       * 'book.AUTHOR_ID')

1236       *

1237       * @param array  $arr     An array to populate the object from.

1238       * @param string $keyType The type of keys the array uses.

1239       * @return void

1240       */
1241  	public function populateFromArray($arr, $keyType = self::TYPE_COLNAME)
1242      {
1243          $keys = self::getFieldNames($keyType);
1244  <?php
1245          foreach ($table->getColumns() as $num => $col) {
1246              $cfc = $col->getPhpName();
1247                $cptype = $col->getPhpNative();
1248  ?>
1249          if (array_key_exists($keys[<?php echo $num ?>], $arr)) $this->set<?php echo $cfc ?>($arr[$keys[<?php echo $num ?>]]);
1250  <?php
1251          } /* foreach */
1252  ?>
1253      }
1254  
1255  <?php } /* ends the if($addGenericMutators) */
1256      } /* ends the if($addGenericAccessors || $addGenericMutators) */
1257  ?>
1258  
1259  <?php if (!$table->isAlias() && isset($addSaveMethod) && $addSaveMethod && !$table->isReadOnly()) { ?>
1260  
1261      /**

1262       * Removes this object from datastore and sets delete attribute.

1263       *

1264       * @param Connection $con

1265       * @return void

1266       * @throws PropelException

1267       * @see BaseObject::setDeleted()

1268       * @see BaseObject::isDeleted()

1269       */
1270  	public function delete($con = null)
1271      {
1272          if ($this->isDeleted()) {
1273              throw new PropelException("This object has already been deleted.");
1274          }
1275  
1276          if ($con === null)
1277              $con = Propel::getConnection(<?php echo $table->getPhpName()?>Peer::DATABASE_NAME);
1278  
1279          try {
1280              $con->begin();
1281              <?php echo $table->getPhpName() ?>Peer::doDelete($this, $con);
1282              $this->setDeleted(true);
1283              $con->commit();
1284          } catch (PropelException $e) {
1285              $con->rollback();
1286              throw $e;
1287          }
1288      }
1289  
1290  <?php
1291  if ($complexObjectModel) {
1292  ?>
1293  
1294      /**

1295       * flag to prevent endless save loop, if this object is referenced

1296       * by another object which falls in this transaction.

1297       * @var boolean

1298       */
1299      private $alreadyInSave = false;
1300  
1301      /**

1302       * Stores the object in the database.  If the object is new,

1303       * it inserts it; otherwise an update is performed.  This method

1304       * wraps the doSave() worker method in a transaction.

1305       *

1306       * @param Connection $con

1307       * @return void

1308       * @throws PropelException

1309       */
1310  	public function save($con = null)
1311      {
1312          if ($this->isDeleted()) {
1313              throw new PropelException("You cannot save an object that has been deleted.");
1314          }
1315  
1316          if ($con === null)
1317              $con = Propel::getConnection(<?php echo $table->getPhpName()?>Peer::DATABASE_NAME);
1318  
1319          try {
1320              $con->begin();
1321              $this->doSave($con);
1322              $con->commit();
1323          } catch (PropelException $e) {
1324              $con->rollback();
1325              throw $e;
1326          }
1327      }
1328  <?php
1329  } /* if ($complexObjectModel) */
1330  ?>
1331  
1332      /**

1333       * Stores the object in the database.  If the object is new,

1334       * it inserts it; otherwise an update is performed.

1335       *

1336       * @param Connection $con

1337       * @return void

1338       * @throws PropelException

1339       */
1340  <?php
1341  if($complexObjectModel) {
1342  ?>
1343  		protected function doSave($con)<?php
1344  } else {
1345  ?>
1346  		public function save($con = null)<?php
1347  }
1348  ?>
1349      {
1350  
1351  <?php
1352  if(!$complexObjectModel) {
1353  ?>        if ($this->isDeleted()) {
1354              throw new PropelException("You cannot save an object that has been deleted.");
1355          }
1356  
1357          if ($con === null) {
1358              $con = Propel::getConnection(<?php echo $basePrefix . $table->getPhpName() ?>Peer::DATABASE_NAME);
1359          }
1360  <?php
1361  }
1362  
1363  if ($complexObjectModel) {
1364  ?>
1365  
1366          if (!$this->alreadyInSave) {
1367              $this->alreadyInSave = true;
1368  <?php
1369      if (!empty($pVars)) {
1370  ?>
1371  
1372              // We call the save method on the following object(s) if they

1373              // were passed to this object by their coresponding set

1374              // method.  This object relates to these object(s) by a

1375              // foreign key reference.

1376  <?php
1377          for($i=0,$_i=count($aVars); $i < $_i; $i++) {
1378              $aVarName = $aVars[$i];
1379  ?>
1380              if ($this-><?php echo $aVarName ?> !== null) {
1381                  if ($this-><?php echo $aVarName ?>->isModified()) $this-><?php echo $aVarName ?>->save($con);
1382                  $this->set<?php echo $pVars[$i] ?>($this-><?php echo $aVarName ?>);
1383              }
1384  <?php
1385          } /* foreach */
1386      } /* if count(pVars) != 0 */
1387  } /* if complexObjectMode */
1388  ?>
1389  
1390              // If this object has been modified, then save it to the database.

1391              if ($this->isModified()) {
1392                  if ($this->isNew()) {
1393                      $pk = <?php echo $table->getPhpName() ?>Peer::doInsert($this, $con);
1394  <?php
1395      if ($table->getIdMethod() != "none") {
1396  
1397          if (count($pks = $table->getPrimaryKey())) {
1398              foreach ($pks as $pk) {
1399                  if ($pk->isAutoIncrement()) {
1400  ?>
1401                      $this->set<?php echo $pk->getPhpName();?>( $pk );  //[IMV] update autoincrement primary key

1402  <?php
1403                  }
1404              }
1405          }
1406      }
1407  ?>
1408                      $this->setNew(false);
1409                  } else {
1410                      <?php echo $table->getPhpName() ?>Peer::doUpdate($this, $con);
1411                  }
1412                  $this->resetModified(); // [HL] After being saved an object is no longer 'modified'

1413              }
1414  <?php
1415      if ($complexObjectModel) {
1416          foreach ($table->getReferrers() as $fk) {
1417              $tblFK = $fk->getTable();
1418              if ( $tblFK->getName() != $table->getName() ) {
1419                  $className = $tblFK->getPhpName();
1420                  $relCol = "";
1421                  foreach ($fk->getLocalColumns() as $columnName) {
1422                      $column = $tblFK->getColumn($columnName);
1423                      if ($column->isMultipleFK()) {
1424                          $relCol .= $column->getPhpName();
1425                      }
1426                  }
1427  
1428                  if ($relCol == "") {
1429                      $relCol = $className . "s";
1430                  } else {
1431                      $relCol = $className . "sRelatedBy" . $relCol;
1432                  }
1433  
1434                  $collName = "coll" . $relCol;
1435  ?>
1436              if ($this-><?php echo $collName ?> !== null) {
1437                  for ($i=0,$size=count($this-><?php echo $collName ?>); $i < $size; $i++) {
1438                      $this-><?php echo $collName ?>[$i]->save($con);
1439                  }
1440                }
1441  <?php
1442              } /* if tableFK !+ table */
1443          } /* foreach getReferrers() */
1444      } /* if complexObjectModel */
1445  ?>
1446  
1447  <?php
1448      if ($complexObjectModel) {
1449  ?>
1450              $this->alreadyInSave = false;
1451          }
1452  <?php
1453      } /* if ($complexObjectModel) */
1454  ?>
1455      }
1456  <?php
1457  } /* if !table->isAlias && isset .... */
1458  ?>
1459  
1460  <?php
1461  if (!$table->isAlias() && !$table->isReadOnly()) {
1462  ?>
1463      /**

1464       * Validates the objects modified field values.

1465  <?php

1466      if ($complexObjectModel) {

1467  ?>

1468       * This includes all objects related to this table.

1469  <?php

1470      } /* if ($complexObjectModel) */
1471  ?>
1472       *
1473       * If $columns is either a column name or an array of column names
1474       * only those columns are validated.
1475       *
1476       * @param mixed $columns Column name or an array of column names.
1477       *
1478       * @return mixed <code>true</code> if all columns pass validation
1479       *              or an array of <code>ValidationFailed</code> objects for columns that fail.
1480       */
1481  	public function validate($columns = null)
1482      {
1483        if ($columns)
1484        {
1485          return <?php echo $table->getPhpName()?>Peer::doValidate($this, $columns);
1486        }
1487  
1488  <?php
1489      if (!$complexObjectModel) {
1490  ?>
1491          return <?php echo $table->getPhpName()?>Peer::doValidate($this);
1492  <?php
1493      } else {
1494  ?>
1495          return $this->doValidate();
1496  <?php
1497      } /* if (!$complexObjectModel) */
1498  ?>
1499      }
1500  
1501  <?php
1502      if ($complexObjectModel) {
1503  ?>
1504      /**

1505       * flag to prevent endless validation loop, if this object is referenced

1506       * by another object which falls in this transaction.

1507       * @var boolean

1508       */
1509      protected $alreadyInValidation = false;
1510  
1511      /**

1512       * This function performs the validation work for complex object models.

1513       *

1514       * In addition to checking the current object, all related objects will

1515       * also be validated.  If all pass then <code>true</code> is returned; otherwise

1516       * an aggreagated array of ValidationFailed objects will be returned.

1517       *

1518       * @return mixed <code>true</code> if all validations pass; array of <code>ValidationFailed</code> objets otherwise.

1519       */
1520  	protected function doValidate()
1521      {
1522          if (! $this->alreadyInValidation) {
1523              $this->alreadyInValidation = true;
1524              $retval = null;
1525  
1526              $failureMap = array();
1527  <?php
1528          if (count($pVars) != 0) {
1529  ?>
1530  
1531              // We call the validate method on the following object(s) if they

1532              // were passed to this object by their coresponding set

1533              // method.  This object relates to these object(s) by a

1534              // foreign key reference.

1535  <?php
1536              for($i=0,$_i=count($aVars); $i < $_i; $i++) {
1537                  $aVarName = $aVars[$i];
1538  ?>
1539              if ($this-><?php echo $aVarName ?> !== null) {
1540                  if (($retval = $this-><?php echo $aVarName ?>->validate()) !== true) {
1541                      $failureMap = array_merge($failureMap, $retval);
1542                  }
1543              }
1544  <?php
1545              } /* for() */
1546          } /* if count(pVars) */
1547  ?>
1548  
1549              if (($retval = <?php echo $table->getPhpName()?>Peer::doValidate($this)) !== true) {
1550                  $failureMap = array_merge($failureMap, $retval);
1551              }
1552  
1553  <?php
1554          foreach ($table->getReferrers() as $fk) {
1555              $tblFK = $fk->getTable();
1556              if ( $tblFK->getName() != $table->getName() ) {
1557                  $className = $tblFK->getPhpName();
1558                  $relCol = "";
1559                  foreach ($fk->getLocalColumns() as $columnName) {
1560                      $column = $tblFK->getColumn($columnName);
1561                      if ($column->isMultipleFK()) {
1562                          $relCol .= $column->getPhpName();
1563                      }
1564                  }
1565  
1566                  if ($relCol == "") {
1567                      $relCol = $className . "s";
1568                  } else {
1569                      $relCol = $className . "sRelatedBy" . $relCol;
1570                  }
1571  
1572                  $collName = "coll" . $relCol;
1573  ?>
1574              if ($this-><?php echo $collName ?> !== null) {
1575                  for ($i=0,$size=count($this-><?php echo $collName ?>); $i < $size; $i++) {
1576                      if (($retval = $this-><?php echo $collName ?>[$i]->validate()) !== true) {
1577                          $failureMap = array_merge($failureMap, $retval);
1578                      }
1579                  }
1580              }
1581  <?php
1582              } /* if tableFK !+ table */
1583          } /* foreach getReferrers() */
1584  ?>
1585  
1586              $this->alreadyInValidation = false;
1587          }
1588  
1589          return (!empty($failureMap) ? $failureMap : true);
1590      }
1591  
1592  <?php
1593      } /* complexObjectModel */
1594  } /* ! isAlias */
1595  ?>
1596  
1597  <?php
1598  
1599  // PrimaryKey methods

1600  if (!$table->isAlias()) {
1601  
1602      if (!$table->isReadOnly()) {
1603  
1604          $throwsClause = "@throws PropelException";
1605  
1606          if (count($table->getPrimaryKey()) == 1) {
1607              $pkeys = $table->getPrimaryKey();
1608              $col = $pkeys[0];
1609              $clo=strtolower($col->getName());
1610              $cptype= $col->getPhpNative();
1611  ?>
1612      /**

1613       * Set the PrimaryKey.

1614       *

1615       * @param mixed <?php echo $clo ?> Primary key.

1616       * @return void

1617       * <?php echo $throwsClause ?> 

1618       */
1619  	public function setPrimaryKey($key)
1620      {
1621          $this->set<?php echo $col->getPhpName() ?>($key);
1622      }
1623  
1624  <?php
1625          } elseif (count($table->getPrimaryKey()) > 1) { /* if(count($table->getPrimaryKey()) == 1) */
1626  ?>
1627  
1628      private $pks = array();
1629  
1630      /**

1631       * Set the PrimaryKey.

1632       *

1633       * @param array $keys The elements of the composite key (order must match the order in XML file).

1634       * @return void

1635       * @throws PropelException

1636       */
1637  	public function setPrimaryKey($keys)
1638      {
1639  <?php
1640              $i = 0;
1641              foreach ($table->getPrimaryKey() as $pk) {
1642                  $pktype = $pk->getPhpNative();
1643  ?>
1644  
1645          $this->set<?php echo $pk->getPhpName() ?>($keys[<?php echo $i ?>]);
1646  <?php
1647                  $i++;
1648              } /* foreach ($table->getPrimaryKey() */
1649  ?>
1650  
1651      }
1652  
1653  <?php
1654          } else { /* if(count($table->getPrimaryKey()) == 1) */
1655  ?>
1656  
1657      /**

1658       * Dummy primary key setter.

1659       * 

1660       * This function only exists to preserve backwards compatibility.  It is no longer

1661       * needed or required by the Persistent interface.  It will be removed in next BC-breaking

1662       * release of Propel.

1663       *

1664       * @deprecated

1665       */
1666  	 public function setPrimaryKey($pk)
1667       {
1668           // do nothing, because this object doesn't have any primary keys

1669       }
1670  <?php
1671          } /* if(count($table->getPrimaryKey()) == 1) */
1672  
1673      } /* !table->isReadOnly() */
1674  ?>
1675  
1676      /**

1677       * Returns an id that differentiates this object from others

1678       * of its class.

1679       * @return <?php if (count($table->getPrimaryKey()) === 0) { echo "null"; } elseif (count($table->getPrimaryKey()) > 1) { echo "array"; } else { $pkeys = $table->getPrimaryKey(); echo $pkeys[0]->getPhpType(); } ?> 

1680       */
1681  	public function getPrimaryKey()
1682      {
1683  <?php
1684      if (count($table->getPrimaryKey()) == 1) {
1685  ?>
1686  
1687          return $this->get<?php $pkeys = $table->getPrimaryKey(); echo $pkeys[0]->getPhpName()?>();
1688  <?php
1689      } elseif (count($table->getPrimaryKey()) > 1) { /* if (count($table->getPrimaryKey()) == 1) { */
1690          $i = 0;
1691          foreach ($table->getPrimaryKey() as $pk) {
1692  ?>
1693          $this->pks[<?php echo $i ?>] = $this->get<?php echo $pk->getPhpName() ?>();
1694  <?php
1695              $i++;
1696          } /* foreach */
1697  ?>
1698          return $this->pks;
1699  <?php
1700      } else { /*if (count($table->getPrimaryKey()) == 1) { */
1701  ?>
1702  
1703          return null;
1704  <?php
1705      } /* if (count($table->getPrimaryKey()) == 1) { */
1706  ?>
1707  
1708      }
1709  
1710  <?php
1711      if (!$table->isAbstract()) {
1712  ?>
1713  
1714      /**

1715       * Makes a copy of this object that will be inserted as a new row in table when saved.

1716       * It creates a new object filling in the simple attributes, but skipping any primary

1717       * keys that are defined for the table.

1718       * <?php if ($complexObjectModel) { ?>

1719       * If desired, this method can also make copies of all associated (fkey referrers)

1720       * objects.

1721       *

1722       * @param boolean $deepCopy Whether to also copy all rows that refer (by fkey) to the current row.

1723       * <?php } ?>

1724       * @return <?php echo $table->getPhpName() ?> Clone of current object.

1725       * @throws PropelException

1726       */
1727  	public function copy(<?php if ($complexObjectModel) { ?>$deepCopy = false<?php } ?>)
1728      {
1729          $clazz = get_class($this);
1730          $copyObj = new $clazz();
1731  <?php
1732  
1733          $pkcols = array();
1734          foreach ($table->getColumns() as $pkcol) {
1735              if ($pkcol->isPrimaryKey()) {
1736                  $pkcols[] = $pkcol->getName();
1737              }
1738          }
1739  
1740          foreach ($table->getColumns() as $col) {
1741  
1742              if (!in_array($col->getName(), $pkcols)) {
1743  ?>
1744          $copyObj->set<?php echo $col->getPhpName()?>($this-><?php echo strtolower($col->getName()) ?>);
1745  <?php
1746          }
1747      }
1748  
1749          if ($complexObjectModel) {
1750  
1751              // Avoid useless code by checking to see if there are any referrers

1752              // to this table:

1753              if (count($table->getReferrers()) > 0) {
1754  ?>
1755  
1756          if ($deepCopy) {
1757              // important: setNew(false) because this affects the behavior of

1758              // the getter/setter methods for fkey referrer objects.

1759              $copyObj->setNew(false);
1760  <?php
1761  
1762                  foreach ($table->getReferrers() as $fk) {
1763                      $tblFK = $fk->getTable();
1764                      if ( $tblFK->getName() != $table->getName() ) {
1765                          $className = $tblFK->getPhpName();
1766                          $relCol = "";
1767                          foreach ($fk->getLocalColumns() as $columnName) {
1768                              $column = $tblFK->getColumn($columnName);
1769                              if ($column->isMultipleFK()) {
1770                                  $relCol .= $column->getPhpName();
1771                              }
1772                          }
1773  
1774                          if ($relCol == "") {
1775                              $pCollName = $className . "s";
1776                              $pCollNameNoS = $className;
1777                          } else {
1778                              $pCollName = $className . "sRelatedBy".$relCol;
1779                              $pCollNameNoS = $className . "RelatedBy".$relCol;
1780                          }
1781  ?>
1782  
1783              foreach($this->get<?php echo $pCollName ?>() as $relObj) {
1784                  $copyObj->add<?php echo $pCollNameNoS ?>($relObj->copy());
1785              }
1786  <?php
1787                      } /* if tblFK != table */
1788                  } /* foreach */
1789  ?>
1790          } // if ($deepCopy)

1791  <?php
1792              } /* if (count referrers > 0 ) */
1793  ?>
1794  
1795          $copyObj->setNew(true);
1796  
1797  <?php
1798          } /* if complex object model */
1799  
1800  // this is a little redundant, but it's clearer than re-using

1801  // the $pkcols array  we created above

1802          foreach ($table->getColumns() as $col) {
1803              if ($col->isPrimaryKey()) {
1804                      $coldefval = $col->getPhpDefaultValue();
1805  
1806                      // This seems to work pretty well for getting

1807                      // the right value (including NULL)

1808                      $coldefval = var_export($coldefval, true);
1809  ?>
1810          $copyObj->set<?php echo $col->getPhpName()?>(<?php echo $coldefval ?>); // this is a pkey column, so set to default value

1811  <?php
1812              } // if col->isPrimaryKey

1813          } // foreach

1814  ?>
1815  
1816          return $copyObj;
1817      }
1818  
1819  <?php
1820      } /* if (!$table->isAbstract()) */
1821  ?>
1822  
1823      /**

1824       * returns a peer instance associated with this om.  Since Peer classes

1825       * are not to have any instance attributes, this method returns the

1826       * same instance for all member of this class. The method could therefore

1827       * be static, but this would prevent one from overriding the behavior.

1828       * @return <?php echo $table->getPhpName() ?>Peer

1829       */
1830  	public function getPeer()
1831      {
1832          if (self::$peer === null) {
1833              self::$peer = new <?php echo $table->getPhpName() ?>Peer();
1834          }
1835          return self::$peer;
1836      }
1837  <?php
1838  } /* if !table->isAlias */
1839  ?>
1840  
1841  }


Généré le : Fri Mar 16 22:42:14 2007 par Balluche grâce à PHPXref 0.7