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