| [ Index ] |
|
Code source de Symfony 1.0.0 |
1 <?php 2 3 /* 4 * $Id: PHP5BasicObjectBuilder.php 157 2005-08-10 19:16:22Z 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 tree node Object class for user object model (OM). 27 * 28 * This class produces the base tree node object class (e.g. BaseMyTable) which contains all 29 * the custom-built accessor and setter methods. 30 * 31 * This class replaces the Node.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 PHP5NodeBuilder 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->getStubNodeBuilder()->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 $script .= " 64 require_once '".$this->getStubNodePeerBuilder()->getClassFilePath()."'; 65 "; 66 } // addIncludes() 67 68 /** 69 * Adds class phpdoc comment and openning of class. 70 * @param string &$script The script will be modified in this method. 71 */ 72 protected function addClassOpen(&$script) 73 { 74 75 $table = $this->getTable(); 76 $tableName = $table->getName(); 77 $tableDesc = $table->getDescription(); 78 79 $script .= " 80 /** 81 * Base class that represents a row from the '$tableName' table. 82 * 83 * $tableDesc 84 *"; 85 if ($this->getBuildProperty('addTimeStamp')) { 86 $now = strftime('%c'); 87 $script .= " 88 * This class was autogenerated by Propel on: 89 * 90 * $now 91 *"; 92 } 93 $script .= " 94 * @package ".$this->getPackage()." 95 */ 96 abstract class ".$this->getClassname()." implements IteratorAggregate { 97 "; 98 } 99 100 /** 101 * Specifies the methods that are added as part of the basic OM class. 102 * This can be overridden by subclasses that wish to add more methods. 103 * @see ObjectBuilder::addClassBody() 104 */ 105 protected function addClassBody(&$script) 106 { 107 $table = $this->getTable(); 108 109 $this->addAttributes($script); 110 111 $this->addConstructor($script); 112 113 $this->addCallOverload($script); 114 $this->addSetIteratorOptions($script); 115 $this->addGetIterator($script); 116 117 $this->addGetNodeObj($script); 118 $this->addGetNodePath($script); 119 $this->addGetNodeIndex($script); 120 $this->addGetNodeLevel($script); 121 122 $this->addHasChildNode($script); 123 $this->addGetChildNodeAt($script); 124 $this->addGetFirstChildNode($script); 125 $this->addGetLastChildNode($script); 126 $this->addGetSiblingNode($script); 127 128 $this->addGetParentNode($script); 129 $this->addGetAncestors($script); 130 $this->addIsRootNode($script); 131 132 $this->addSetNew($script); 133 $this->addSetDeleted($script); 134 $this->addAddChildNode($script); 135 $this->addMoveChildNode($script); 136 $this->addSave($script); 137 138 $this->addDelete($script); 139 $this->addEquals($script); 140 141 $this->addAttachParentNode($script); 142 $this->addAttachChildNode($script); 143 $this->addDetachParentNode($script); 144 $this->addDetachChildNode($script); 145 $this->addShiftChildNodes($script); 146 $this->addInsertNewChildNode($script); 147 148 $this->addAdjustStatus($script); 149 $this->addAdjustNodePath($script); 150 151 } 152 153 /** 154 * Closes class. 155 * @param string &$script The script will be modified in this method. 156 */ 157 protected function addClassClose(&$script) 158 { 159 $script .= " 160 } // " . $this->getClassname() . " 161 "; 162 } 163 164 165 /** 166 * Adds class attributes. 167 * @param string &$script The script will be modified in this method. 168 */ 169 protected function addAttributes(&$script) 170 { 171 $script .= " 172 /** 173 * @var ".$this->getStubObjectBuilder()->getClassname()." object wrapped by this node. 174 */ 175 protected \$obj = null; 176 177 /** 178 * The parent node for this node. 179 * @var ".$this->getStubNodeBuilder()->getClassname()." 180 */ 181 protected \$parentNode = null; 182 183 /** 184 * Array of child nodes for this node. Nodes indexes are one-based. 185 * @var array 186 */ 187 protected \$childNodes = array(); 188 "; 189 } 190 191 /** 192 * Adds the constructor. 193 * @param string &$script The script will be modified in this method. 194 */ 195 protected function addConstructor(&$script) 196 { 197 $script .= " 198 /** 199 * Constructor. 200 * 201 * @param ".$this->getStubObjectBuilder()->getClassname()." \$obj Object wrapped by this node. 202 */ 203 public function __construct(\$obj = null) 204 { 205 if (\$obj !== null) { 206 \$this->obj = \$obj; 207 } else { 208 \$setNodePath = 'set' . ".$this->getStubNodePeerBuilder()->getClassname()."::NPATH_PHPNAME; 209 \$this->obj = new ".$this->getStubObjectBuilder()->getClassname()."(); 210 \$this->obj->\$setNodePath('0'); 211 } 212 } 213 "; 214 } 215 216 217 218 protected function addCallOverload(&$script) 219 { 220 $script .= " 221 /** 222 * Convenience overload for wrapped object methods. 223 * 224 * @param string Method name to call on wrapped object. 225 * @param mixed Parameter accepted by wrapped object set method. 226 * @return mixed Return value of wrapped object method. 227 * @throws PropelException Fails if method is not defined for wrapped object. 228 */ 229 public function __call(\$name, \$parms) 230 { 231 if (method_exists(\$this->obj, \$name)) 232 return call_user_func_array(array(\$this->obj, \$name), \$parms); 233 else 234 throw new PropelException('get method not defined: \$name'); 235 } 236 "; 237 } 238 239 protected function addSetIteratorOptions(&$script) 240 { 241 $script .= " 242 243 /** 244 * Sets the default options for iterators created from this object. 245 * The options are specified in map format. The following options 246 * are supported by all iterators. Some iterators may support other 247 * options: 248 * 249 * \"querydb\" - True if nodes should be retrieved from database. 250 * \"con\" - Connection to use if retrieving from database. 251 * 252 * @param string Type of iterator to use (\"pre\", \"post\", \"level\"). 253 * @param array Map of option name => value. 254 * @return void 255 * @todo Implement other iterator types (i.e. post-order, level, etc.) 256 */ 257 public function setIteratorOptions(\$type, \$opts) 258 { 259 \$this->itType = \$type; 260 \$this->itOpts = \$opts; 261 } 262 "; 263 } 264 265 protected function addGetIterator(&$script) 266 { 267 $script .= " 268 /** 269 * Returns a pre-order iterator for this node and its children. 270 * 271 * @param string Type of iterator to use (\"pre\", \"post\", \"level\") 272 * @param array Map of option name => value. 273 * @return NodeIterator 274 */ 275 public function getIterator(\$type = null, \$opts = null) 276 { 277 if (\$type === null) 278 \$type = (isset(\$this->itType) ? \$this->itType : 'Pre'); 279 280 if (\$opts === null) 281 \$opts = (isset(\$this->itOpts) ? \$this->itOpts : array()); 282 283 \$itclass = ucfirst(strtolower(\$type)) . 'OrderNodeIterator'; 284 285 require_once('propel/om/' . \$itclass . '.php'); 286 return new \$itclass(\$this, \$opts); 287 } 288 "; 289 } 290 291 protected function addGetNodeObj(&$script) 292 { 293 $script .= " 294 /** 295 * Returns the object wrapped by this class. 296 * @return ".$this->getStubObjectBuilder()->getClassname()." 297 */ 298 public function getNodeObj() 299 { 300 return \$this->obj; 301 } 302 "; 303 } 304 305 protected function addGetNodePath(&$script) 306 { 307 $script .= " 308 /** 309 * Convenience method for retrieving nodepath. 310 * @return string 311 */ 312 public function getNodePath() 313 { 314 \$getNodePath = 'get' . ".$this->getStubNodePeerBuilder()->getClassname()."::NPATH_PHPNAME; 315 return \$this->obj->\$getNodePath(); 316 } 317 "; 318 } 319 320 protected function addGetNodeIndex(&$script) 321 { 322 $script .= " 323 /** 324 * Returns one-based node index among siblings. 325 * @return int 326 */ 327 public function getNodeIndex() 328 { 329 \$npath =& \$this->getNodePath(); 330 \$sep = strrpos(\$npath, ".$this->getStubNodePeerBuilder()->getClassname()."::NPATH_SEP); 331 return (int) (\$sep !== false ? substr(\$npath, \$sep+1) : \$npath); 332 } 333 "; 334 } 335 336 protected function addGetNodeLevel(&$script) 337 { 338 $script .= " 339 /** 340 * Returns one-based node level within tree (root node is level 1). 341 * @return int 342 */ 343 public function getNodeLevel() 344 { 345 return (substr_count(\$this->getNodePath(), ".$this->getStubNodePeerBuilder()->getClassname()."::NPATH_SEP) + 1); 346 } 347 "; 348 } 349 350 protected function addHasChildNode(&$script) 351 { 352 $script .= " 353 /** 354 * Returns true if specified node is a child of this node. If recurse is 355 * true, checks if specified node is a descendant of this node. 356 * 357 * @param ".$this->getStubNodeBuilder()->getClassname()." Node to look for. 358 * @param boolean True if strict comparison should be used. 359 * @param boolean True if all descendants should be checked. 360 * @return boolean 361 */ 362 public function hasChildNode(\$node, \$strict = false, \$recurse = false) 363 { 364 foreach (\$this->childNodes as \$childNode) 365 { 366 if (\$childNode->equals(\$node, \$strict)) 367 return true; 368 369 if (\$recurse && \$childNode->hasChildNode(\$node, \$recurse)) 370 return true; 371 } 372 373 return false; 374 } 375 "; 376 } 377 378 protected function addGetChildNodeAt(&$script) 379 { 380 $script .= " 381 /** 382 * Returns child node at one-based index. Retrieves from database if not 383 * loaded yet. 384 * 385 * @param int One-based child node index. 386 * @param boolean True if child should be retrieved from database. 387 * @param Connection Connection to use if retrieving from database. 388 * @return ".$this->getStubNodeBuilder()->getClassname()." 389 */ 390 public function getChildNodeAt(\$i, \$querydb = false, \$con = null) 391 { 392 if (\$querydb && 393 !\$this->obj->isNew() && 394 !\$this->obj->isDeleted() && 395 !isset(\$this->childNodes[\$i])) 396 { 397 \$criteria = new Criteria(".$this->getStubPeerBuilder()->getClassname()."::DATABASE_NAME); 398 \$criteria->add(".$this->getStubNodePeerBuilder()->getClassname()."::NPATH_COLNAME, \$this->getNodePath() . ".$this->getStubNodePeerBuilder()->getClassname()."::NPATH_SEP . \$i, Criteria::EQUAL); 399 400 if (\$childObj = ".$this->getStubPeerBuilder()->getClassname()."::doSelectOne(\$criteria, \$con)) 401 \$this->attachChildNode(new ".$this->getStubNodeBuilder()->getClassname()."(\$childObj)); 402 } 403 404 return (isset(\$this->childNodes[\$i]) ? \$this->childNodes[\$i] : null); 405 } 406 "; 407 } 408 409 protected function addGetFirstChildNode(&$script) 410 { 411 $script .= " 412 /** 413 * Returns first child node (if any). Retrieves from database if not loaded yet. 414 * 415 * @param boolean True if child should be retrieved from database. 416 * @param Connection Connection to use if retrieving from database. 417 * @return ".$this->getStubNodeBuilder()->getClassname()." 418 */ 419 public function getFirstChildNode(\$querydb = false, \$con = null) 420 { 421 return \$this->getChildNodeAt(1, \$querydb, \$con); 422 } 423 "; 424 } 425 426 protected function addGetLastChildNode(&$script) 427 { 428 $peerClassname = $this->getStubPeerBuilder()->getClassname(); 429 $nodePeerClassname = $this->getStubNodePeerBuilder()->getClassname(); 430 431 $script .= " 432 /** 433 * Returns last child node (if any). 434 * 435 * @param boolean True if child should be retrieved from database. 436 * @param Connection Connection to use if retrieving from database. 437 */ 438 public function getLastChildNode(\$querydb = false, \$con = null) 439 { 440 \$lastNode = null; 441 442 if (\$this->obj->isNew() || \$this->obj->isDeleted()) 443 { 444 end(\$this->childNodes); 445 \$lastNode = (count(\$this->childNodes) ? current(\$this->childNodes) : null); 446 } 447 else if (\$querydb) 448 { 449 \$db = Propel::getDb($peerClassname::DATABASE_NAME); 450 \$criteria = new Criteria($peerClassname::DATABASE_NAME); 451 \$criteria->add($nodePeerClassname::NPATH_COLNAME, \$this->getNodePath() . $nodePeerClassname::NPATH_SEP . '%', Criteria::LIKE); 452 \$criteria->addAnd($nodePeerClassname::NPATH_COLNAME, \$this->getNodePath() . $nodePeerClassname::NPATH_SEP . '%' . $nodePeerClassname::NPATH_SEP . '%', Criteria::NOT_LIKE); 453 \$criteria->addAsColumn('npathlen', \$db->strLength($nodePeerClassname::NPATH_COLNAME)); 454 \$criteria->addDescendingOrderByColumn('npathlen'); 455 \$criteria->addDescendingOrderByColumn($nodePeerClassname::NPATH_COLNAME); 456 457 \$lastObj = $peerClassname::doSelectOne(\$criteria, \$con); 458 459 if (\$lastObj !== null) 460 { 461 \$lastNode = new ".$this->getStubNodeBuilder()->getClassname()."(\$lastObj); 462 463 end(\$this->childNodes); 464 \$endNode = (count(\$this->childNodes) ? current(\$this->childNodes) : null); 465 466 if (\$endNode) 467 { 468 if (\$endNode->getNodePath() > \$lastNode->getNodePath()) 469 throw new PropelException('Cached child node inconsistent with database.'); 470 else if (\$endNode->getNodePath() == \$lastNode->getNodePath()) 471 \$lastNode = \$endNode; 472 else 473 \$this->attachChildNode(\$lastNode); 474 } 475 else 476 { 477 \$this->attachChildNode(\$lastNode); 478 } 479 } 480 } 481 482 return \$lastNode; 483 } 484 "; 485 } 486 487 protected function addGetSiblingNode(&$script) 488 { 489 $script .= " 490 /** 491 * Returns next (or previous) sibling node or null. Retrieves from database if 492 * not loaded yet. 493 * 494 * @param boolean True if previous sibling should be returned. 495 * @param boolean True if sibling should be retrieved from database. 496 * @param Connection Connection to use if retrieving from database. 497 * @return ".$this->getStubNodeBuilder()->getClassname()." 498 */ 499 public function getSiblingNode(\$prev = false, \$querydb = false, \$con = null) 500 { 501 \$nidx = \$this->getNodeIndex(); 502 503 if (\$this->isRootNode()) 504 { 505 return null; 506 } 507 else if (\$prev) 508 { 509 if (\$nidx > 1 && (\$parentNode = \$this->getParentNode(\$querydb, \$con))) 510 return \$parentNode->getChildNodeAt(\$nidx-1, \$querydb, \$con); 511 else 512 return null; 513 } 514 else 515 { 516 if (\$parentNode = \$this->getParentNode(\$querydb, \$con)) 517 return \$parentNode->getChildNodeAt(\$nidx+1, \$querydb, \$con); 518 else 519 return null; 520 } 521 } 522 "; 523 } 524 525 protected function addGetParentNode(&$script) 526 { 527 $peerClassname = $this->getStubPeerBuilder()->getClassname(); 528 $nodePeerClassname = $this->getStubNodePeerBuilder()->getClassname(); 529 530 $script .= " 531 /** 532 * Returns parent node. Loads from database if not cached yet. 533 * 534 * @param boolean True if parent should be retrieved from database. 535 * @param Connection Connection to use if retrieving from database. 536 * @return ".$this->getStubNodeBuilder()->getClassname()." 537 */ 538 public function getParentNode(\$querydb = true, \$con = null) 539 { 540 if (\$querydb && 541 \$this->parentNode === null && 542 !\$this->isRootNode() && 543 !\$this->obj->isNew() && 544 !\$this->obj->isDeleted()) 545 { 546 \$npath =& \$this->getNodePath(); 547 \$sep = strrpos(\$npath, $nodePeerClassname::NPATH_SEP); 548 \$ppath = substr(\$npath, 0, \$sep); 549 550 \$criteria = new Criteria($peerClassname::DATABASE_NAME); 551 \$criteria->add($nodePeerClassname::NPATH_COLNAME, \$ppath, Criteria::EQUAL); 552 553 if (\$parentObj = $peerClassname::doSelectOne(\$criteria, \$con)) 554 { 555 \$parentNode = new ".$this->getStubNodeBuilder()->getClassname()."(\$parentObj); 556 \$parentNode->attachChildNode(\$this); 557 } 558 } 559 560 return \$this->parentNode; 561 } 562 "; 563 } 564 565 protected function addGetAncestors(&$script) 566 { 567 $script .= " 568 /** 569 * Returns an array of all ancestor nodes, starting with the root node 570 * first. 571 * 572 * @param boolean True if ancestors should be retrieved from database. 573 * @param Connection Connection to use if retrieving from database. 574 * @return array 575 */ 576 public function getAncestors(\$querydb = false, \$con = null) 577 { 578 \$ancestors = array(); 579 \$parentNode = \$this; 580 581 while (\$parentNode = \$parentNode->getParentNode(\$querydb, \$con)) 582 array_unshift(\$ancestors, \$parentNode); 583 584 return \$ancestors; 585 } 586 "; 587 } 588 589 protected function addIsRootNode(&$script) 590 { 591 $script .= " 592 /** 593 * Returns true if node is the root node of the tree. 594 * @return boolean 595 */ 596 public function isRootNode() 597 { 598 return (\$this->getNodePath() === '1'); 599 } 600 "; 601 } 602 603 protected function addSetNew(&$script) 604 { 605 $script .= " 606 /** 607 * Changes the state of the object and its descendants to 'new'. 608 * Also changes the node path to '0' to indicate that it is not a 609 * stored node. 610 * 611 * @param boolean 612 * @return void 613 */ 614 public function setNew(\$b) 615 { 616 \$this->adjustStatus('new', \$b); 617 \$this->adjustNodePath(\$this->getNodePath(), '0'); 618 } 619 "; 620 } 621 622 protected function addSetDeleted(&$script) 623 { 624 $script .= " 625 /** 626 * Changes the state of the object and its descendants to 'deleted'. 627 * 628 * @param boolean 629 * @return void 630 */ 631 public function setDeleted(\$b) 632 { 633 \$this->adjustStatus('deleted', \$b); 634 } 635 "; 636 } 637 638 protected function addAddChildNode(&$script) 639 { 640 $peerClassname = $this->getStubPeerBuilder()->getClassname(); 641 $nodePeerClassname = $this->getStubNodePeerBuilder()->getClassname(); 642 643 $script .= " 644 /** 645 * Adds the specified node (and its children) as a child to this node. If a 646 * valid \$beforeNode is specified, the node will be inserted in front of 647 * \$beforeNode. If \$beforeNode is not specified the node will be appended to 648 * the end of the child nodes. 649 * 650 * @param ".$this->getStubNodeBuilder()->getClassname()." Node to add. 651 * @param ".$this->getStubNodeBuilder()->getClassname()." Node to insert before. 652 * @param Connection Connection to use. 653 */ 654 public function addChildNode(\$node, \$beforeNode = null, \$con = null) 655 { 656 if (\$this->obj->isNew() && !\$node->obj->isNew()) 657 throw new PropelException('Cannot add stored nodes to a new node.'); 658 659 if (\$this->obj->isDeleted() || \$node->obj->isDeleted()) 660 throw new PropelException('Cannot add children in a deleted state.'); 661 662 if (\$this->hasChildNode(\$node)) 663 throw new PropelException('Node is already a child of this node.'); 664 665 if (\$beforeNode && !\$this->hasChildNode(\$beforeNode)) 666 throw new PropelException('Invalid beforeNode.'); 667 668 if (\$con === null) 669 \$con = Propel::getConnection($peerClassname::DATABASE_NAME); 670 671 try { 672 673 if (!\$this->obj->isNew()) \$con->begin(); 674 675 if (\$beforeNode) 676 { 677 // Inserting before a node. 678 \$childIdx = \$beforeNode->getNodeIndex(); 679 \$this->shiftChildNodes(1, \$beforeNode->getNodeIndex(), \$con); 680 } 681 else 682 { 683 // Appending child node. 684 if (\$lastNode = \$this->getLastChildNode(true, \$con)) 685 \$childIdx = \$lastNode->getNodeIndex()+1; 686 else 687 \$childIdx = 1; 688 } 689 690 // Add the child (and its children) at the specified index. 691 692 if (!\$this->obj->isNew() && \$node->obj->isNew()) 693 { 694 \$this->insertNewChildNode(\$node, \$childIdx, \$con); 695 } 696 else 697 { 698 // \$this->isNew() && \$node->isNew() || 699 // !\$this->isNew() && !node->isNew() 700 701 \$srcPath = \$node->getNodePath(); 702 \$dstPath = \$this->getNodePath() . $nodePeerClassname::NPATH_SEP . \$childIdx; 703 704 if (!\$node->obj->isNew()) 705 { 706 $nodePeerClassname::moveNodeSubTree(\$srcPath, \$dstPath, \$con); 707 \$parentNode = \$node->getParentNode(true, \$con); 708 } 709 else 710 { 711 \$parentNode = \$node->getParentNode(); 712 } 713 714 if (\$parentNode) 715 { 716 \$parentNode->detachChildNode(\$node); 717 \$parentNode->shiftChildNodes(-1, \$node->getNodeIndex()+1, \$con); 718 } 719 720 \$node->adjustNodePath(\$srcPath, \$dstPath); 721 } 722 723 if (!\$this->obj->isNew()) \$con->commit(); 724 725 \$this->attachChildNode(\$node); 726 727 } catch (SQLException \$e) { 728 if (!\$this->obj->isNew()) \$con->rollback(); 729 throw new PropelException(\$e); 730 } 731 } 732 "; 733 } 734 735 protected function addMoveChildNode(&$script) 736 { 737 $script .= " 738 /** 739 * Moves the specified child node in the specified direction. 740 * 741 * @param ".$this->getStubNodeBuilder()->getClassname()." Node to move. 742 * @param int Number of spaces to move among siblings (may be negative). 743 * @param Connection Connection to use. 744 * @throws PropelException 745 */ 746 public function moveChildNode(\$node, \$direction, \$con = null) 747 { 748 throw new PropelException('moveChildNode() not implemented yet.'); 749 } 750 "; 751 } 752 753 754 protected function addSave(&$script) 755 { 756 757 $nodePeerClassname = $this->getStubNodePeerBuilder()->getClassname(); 758 $script .= " 759 /** 760 * Saves modified object data to the datastore. 761 * 762 * @param boolean If true, descendants will be saved as well. 763 * @param Connection Connection to use. 764 */ 765 public function save(\$recurse = false, \$con = null) 766 { 767 if (\$this->obj->isDeleted()) 768 throw new PropelException('Cannot save deleted node.'); 769 770 if (substr(\$this->getNodePath(), 0, 1) == '0') 771 throw new PropelException('Cannot save unattached node.'); 772 773 if (\$this->obj->isColumnModified($nodePeerClassname::NPATH_COLNAME)) 774 throw new PropelException('Cannot save manually modified node path.'); 775 776 \$this->obj->save(\$con); 777 778 if (\$recurse) 779 { 780 foreach (\$this->childNodes as \$childNode) 781 \$childNode->save(\$recurse, \$con); 782 } 783 } 784 "; 785 } 786 787 788 protected function addDelete(&$script) 789 { 790 $nodePeerClassname = $this->getStubNodePeerBuilder()->getClassname(); 791 $script .= " 792 /** 793 * Removes this object and all descendants from datastore. 794 * 795 * @param Connection Connection to use. 796 * @return void 797 * @throws PropelException 798 */ 799 public function delete(\$con = null) 800 { 801 if (\$this->obj->isDeleted()) 802 throw new PropelException('This node has already been deleted.'); 803 804 if (!\$this->obj->isNew()) 805 { 806 $nodePeerClassname::deleteNodeSubTree(\$this->getNodePath(), \$con); 807 } 808 809 if (\$parentNode = \$this->getParentNode(true, \$con)) 810 { 811 \$parentNode->detachChildNode(\$this); 812 \$parentNode->shiftChildNodes(-1, \$this->getNodeIndex()+1, \$con); 813 } 814 815 \$this->setDeleted(true); 816 } 817 "; 818 } 819 820 protected function addEquals(&$script) 821 { 822 $nodeClassname = $this->getStubNodeBuilder()->getClassname(); 823 $script .= " 824 /** 825 * Compares the object wrapped by this node with that of another node. Use 826 * this instead of equality operators to prevent recursive dependency 827 * errors. 828 * 829 * @param $nodeClassname Node to compare. 830 * @param boolean True if strict comparison should be used. 831 * @return boolean 832 */ 833 public function equals(\$node, \$strict = false) 834 { 835 if (\$strict) { 836 return (\$this->obj === \$node->obj); 837 } else { 838 return (\$this->obj == \$node->obj); 839 } 840 } 841 "; 842 } 843 844 protected function addAttachParentNode(&$script) 845 { 846 $nodeClassname = $this->getStubNodeBuilder()->getClassname(); 847 $script .= " 848 /** 849 * This method is used internally when constructing the tree structure 850 * from the database. To set the parent of a node, you should call 851 * addChildNode() on the parent. 852 * 853 * @param $nodeClassname Parent node to attach. 854 * @return void 855 * @throws PropelException 856 */ 857 public function attachParentNode(\$node) 858 { 859 if (!\$node->hasChildNode(\$this, true)) 860 throw new PropelException('Failed to attach parent node for non-child.'); 861 862 \$this->parentNode = \$node; 863 } 864 "; 865 } 866 867 868 protected function addAttachChildNode(&$script) 869 { 870 $nodeClassname = $this->getStubNodeBuilder()->getClassname(); 871 $nodePeerClassname = $this->getStubNodePeerBuilder()->getClassname(); 872 $script .= " 873 /** 874 * This method is used internally when constructing the tree structure 875 * from the database. To add a child to a node you should call the 876 * addChildNode() method instead. 877 * 878 * @param $nodeClassname Child node to attach. 879 * @return void 880 * @throws PropelException 881 */ 882 public function attachChildNode(\$node) 883 { 884 if (\$this->hasChildNode(\$node)) 885 throw new PropelException('Failed to attach child node. Node already exists.'); 886 887 if (\$this->obj->isDeleted() || \$node->obj->isDeleted()) 888 throw new PropelException('Failed to attach node in deleted state.'); 889 890 if (\$this->obj->isNew() && !\$node->obj->isNew()) 891 throw new PropelException('Failed to attach non-new child to new node.'); 892 893 if (!\$this->obj->isNew() && \$node->obj->isNew()) 894 throw new PropelException('Failed to attach new child to non-new node.'); 895 896 if (\$this->getNodePath() . $nodePeerClassname::NPATH_SEP . \$node->getNodeIndex() != \$node->getNodePath()) 897 throw new PropelException('Failed to attach child node. Node path mismatch.'); 898 899 \$this->childNodes[\$node->getNodeIndex()] = \$node; 900 ksort(\$this->childNodes); 901 902 \$node->attachParentNode(\$this); 903 } 904 "; 905 } 906 907 protected function addDetachParentNode(&$script) 908 { 909 $nodeClassname = $this->getStubNodeBuilder()->getClassname(); 910 $script .= " 911 /** 912 * This method is used internally when deleting nodes. It is used to break 913 * the link to this node's parent. 914 * @param $nodeClassname Parent node to detach from. 915 * @return void 916 * @throws PropelException 917 */ 918 public function detachParentNode(\$node) 919 { 920 if (!\$node->hasChildNode(\$this, true)) 921 throw new PropelException('Failed to detach parent node from non-child.'); 922 923 unset(\$node->childNodes[\$this->getNodeIndex()]); 924 \$this->parentNode = null; 925 } 926 "; 927 } 928 929 protected function addDetachChildNode(&$script) 930 { 931 $script .= " 932 /** 933 * This method is used internally when deleting nodes. It is used to break 934 * the link to this between this node and the specified child. 935 * @param ".$this->getStubNodeBuilder()->getClassname()." Child node to detach. 936 * @return void 937 * @throws PropelException 938 */ 939 public function detachChildNode(\$node) 940 { 941 if (!\$this->hasChildNode(\$node, true)) 942 throw new PropelException('Failed to detach non-existent child node.'); 943 944 unset(\$this->childNodes[\$node->getNodeIndex()]); 945 \$node->parentNode = null; 946 } 947 "; 948 } 949 950 protected function addShiftChildNodes(&$script) 951 { 952 $peerClassname = $this->getStubPeerBuilder()->getClassname(); 953 $nodePeerClassname = $this->getStubNodePeerBuilder()->getClassname(); 954 955 $script .= " 956 /** 957 * Shifts child nodes in the specified direction and offset index. This 958 * method assumes that there is already space available in the 959 * direction/offset indicated. 960 * 961 * @param int Direction/# spaces to shift. 1=leftshift, 1=rightshift 962 * @param int Node index to start shift at. 963 * @param Connection The connection to be used. 964 * @return void 965 * @throws PropelException 966 */ 967 protected function shiftChildNodes(\$direction, \$offsetIdx, \$con) 968 { 969 if (\$this->obj->isDeleted()) 970 throw new PropelException('Cannot shift nodes for deleted object'); 971 972 \$lastNode = \$this->getLastChildNode(true, \$con); 973 \$lastIdx = (\$lastNode !== null ? \$lastNode->getNodeIndex() : 0); 974 975 if (\$lastNode === null || \$offsetIdx > \$lastIdx) 976 return; 977 978 if (\$con === null) 979 \$con = Propel::getConnection($peerClassname::DATABASE_NAME); 980 981 if (!\$this->obj->isNew()) 982 { 983 // Shift nodes in database. 984 985 try { 986 987 \$con->begin(); 988 989 \$n = \$lastIdx - \$offsetIdx + 1; 990 \$i = \$direction < 1 ? \$offsetIdx : \$lastIdx; 991 992 while (\$n--) 993 { 994 \$srcPath = \$this->getNodePath() . $nodePeerClassname::NPATH_SEP . \$i; // 1.2.2 995 \$dstPath = \$this->getNodePath() . $nodePeerClassname::NPATH_SEP . (\$i+\$direction); // 1.2.3 996 997 $nodePeerClassname::moveNodeSubTree(\$srcPath, \$dstPath, \$con); 998 999 \$i -= \$direction; 1000 } 1001 1002 \$con->commit(); 1003 1004 } catch (SQLException \$e) { 1005 \$con->rollback(); 1006 throw new PropelException(\$e); 1007 } 1008 } 1009 1010 // Shift the in-memory objects. 1011 1012 \$n = \$lastIdx - \$offsetIdx + 1; 1013 \$i = \$direction < 1 ? \$offsetIdx : \$lastIdx; 1014 1015 while (\$n--) 1016 { 1017 if (isset(\$this->childNodes[\$i])) 1018 { 1019 \$srcPath = \$this->getNodePath() . $nodePeerClassname::NPATH_SEP . \$i; // 1.2.2 1020 \$dstPath = \$this->getNodePath() . $nodePeerClassname::NPATH_SEP . (\$i+\$direction); // 1.2.3 1021 1022 \$this->childNodes[\$i+\$direction] = \$this->childNodes[\$i]; 1023 \$this->childNodes[\$i+\$direction]->adjustNodePath(\$srcPath, \$dstPath); 1024 1025 unset(\$this->childNodes[\$i]); 1026 } 1027 1028 \$i -= \$direction; 1029 } 1030 1031 ksort(\$this->childNodes); 1032 } 1033 "; 1034 } 1035 1036 protected function addInsertNewChildNode(&$script) 1037 { 1038 $peerClassname = $this->getStubPeerBuilder()->getClassname(); 1039 $nodePeerClassname = $this->getStubNodePeerBuilder()->getClassname(); 1040 $nodeClassname = $this->getStubNodePeerBuilder()->getClassname(); 1041 1042 $script .= " 1043 /** 1044 * Inserts the node and its children at the specified childIdx. 1045 * 1046 * @param $nodeClassname Node to insert. 1047 * @param int One-based child index to insert at. 1048 * @param Connection Connection to use. 1049 * @param void 1050 */ 1051 protected function insertNewChildNode(\$node, \$childIdx, \$con) 1052 { 1053 if (!\$node->obj->isNew()) 1054 throw new PropelException('Failed to insert non-new node.'); 1055 1056 \$setNodePath = 'set' . $nodePeerClassname::NPATH_PHPNAME; 1057 1058 \$node->obj->\$setNodePath(\$this->getNodePath() . $nodePeerClassname::NPATH_SEP . \$childIdx); 1059 \$node->obj->save(\$con); 1060 1061 \$i = 1; 1062 foreach (\$node->childNodes as \$childNode) 1063 \$node->insertNewChildNode(\$childNode, \$i++, \$con); 1064 } 1065 "; 1066 } 1067 1068 protected function addAdjustStatus(&$script) 1069 { 1070 $script .= " 1071 /** 1072 * Adjust new/deleted status of node and all children. 1073 * 1074 * @param string Status to change ('New' or 'Deleted') 1075 * @param boolean Value for status. 1076 * @return void 1077 */ 1078 protected function adjustStatus(\$status, \$b) 1079 { 1080 \$setStatus = 'set' . \$status; 1081 1082 \$this->obj->\$setStatus(\$b); 1083 1084 foreach (\$this->childNodes as \$childNode) 1085 \$childNode->obj->\$setStatus(\$b); 1086 } 1087 "; 1088 } 1089 1090 protected function addAdjustNodePath(&$script) 1091 { 1092 $nodePeerClassname = $this->getStubNodePeerBuilder()->getClassname(); 1093 $script .= " 1094 /** 1095 * Adjust path of node and all children. This is used internally when 1096 * inserting/moving nodes. 1097 * 1098 * @param string Section of old path to change. 1099 * @param string New section to replace old path with. 1100 * @return void 1101 */ 1102 protected function adjustNodePath(\$oldBasePath, \$newBasePath) 1103 { 1104 \$setNodePath = 'set' . $nodePeerClassname::NPATH_PHPNAME; 1105 1106 \$this->obj->\$setNodePath(\$newBasePath . substr(\$this->getNodePath(), strlen(\$oldBasePath))); 1107 \$this->obj->resetModified($nodePeerClassname::NPATH_COLNAME); 1108 1109 foreach (\$this->childNodes as \$childNode) 1110 \$childNode->adjustNodePath(\$oldBasePath, \$newBasePath); 1111 } 1112 "; 1113 } 1114 1115 } // PHP5NodeObjectBuilder
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 |