[ Index ] |
|
Code source de Symfony 1.0.0 |
1 <?php 2 3 // Template for creating base node Peer class on tree table. 4 // 5 // $Id: NodePeer.tpl,v 1.10 2005/02/13 12:23:52 micha Exp $ 6 7 require_once 'propel/engine/builder/om/ClassTools.php'; 8 require_once 'propel/engine/builder/om/PeerBuilder.php'; 9 10 $npath_colname = ''; 11 $npath_phpname = ''; 12 $npath_len = 0; 13 $npath_sep = ''; 14 15 foreach ($table->getColumns() as $col) { 16 if ($col->isNodeKey()) { 17 $npath_colname = $table->getName() . '.' . strtoupper($col->getName()); 18 $npath_phpname = $col->getPhpName(); 19 $npath_len = $col->getSize(); 20 $npath_sep = $col->getNodeKeySep(); 21 break; 22 } 23 } 24 25 $db = $table->getDatabase(); 26 if($table->getPackage()) { 27 $package = $table->getPackage(); 28 } else { 29 $package = $targetPackage; 30 } 31 32 $CLASS = $table->getPhpName() . 'NodePeer'; 33 echo '<' . '?' . 'php'; 34 35 ?> 36 37 38 require_once '<?php echo ClassTools::getFilePath($package, $table->getPhpName()) ?>'; 39 require_once '<?php echo ClassTools::getFilePath($package, $table->getPhpName() . 'Node') ?>'; 40 41 42 /** 43 * Base static class for performing query operations on the tree contained by the 44 * '<?php echo $table->getPhpName() ?>' table. 45 * 46 <?php if ($addTimeStamp) { ?> 47 * This class was autogenerated by Propel on: 48 * 49 * [<?php echo $now ?>] 50 * 51 <?php } ?> 52 * @package <?php echo $package ?> 53 */ 54 class <?php echo $basePrefix . $table->getPhpName() ?>NodePeer 55 { 56 function NPATH_COLNAME() { return '<?php echo $npath_colname ?>'; } 57 function NPATH_PHPNAME() { return '<?php echo $npath_phpname ?>'; } 58 function NPATH_SEP() { return '<?php echo $npath_sep ?>'; } 59 60 /** 61 * Temp function for CodeBase hacks that will go away. 62 */ 63 function isCodeBase($con = null) 64 { 65 Propel::assertParam($con, '<?php echo $CLASS; ?>', 'isCodeBase', 1); 66 $con =& Param::get($con); 67 68 if ($con === null) 69 $con =& Propel::getConnection(<?php echo $table->getPhpName() ?>Peer::DATABASE_NAME()); 70 71 return (get_class($con) == 'ODBCConnection' && 72 get_class($con->getAdapter()) == 'CodeBaseAdapter'); 73 } 74 75 /** 76 * Create a new Node at the top of tree. This method will destroy any 77 * existing root node (along with its children). 78 * 79 * Use at your own risk! 80 * 81 * @param <?php echo $table->getPhpName() ?> Object wrapped by new node. 82 * @param Connection Connection to use. 83 * @return <?php echo $table->getPhpName() ?>Node 84 * @throws PropelException 85 */ 86 function & createNewRootNode(&$obj, $con = null) 87 { 88 Propel::assertParam($con, '<?php echo $CLASS; ?>', 'createNewRootNode', 2); 89 $con =& Param::get($con); 90 91 if ($con === null) 92 $con =& Propel::getConnection(<?php echo $table->getPhpName() ?>Peer::DATABASE_NAME()); 93 94 if (Propel::isError($con)) { 95 return $con; 96 } 97 98 $e = $con->begin(); 99 if (Creole::isError($e)) { $con->rollback(); return new PropelException(PROPEL_ERROR_DB, $e); } 100 101 $e = <?php echo $table->getPhpName() ?>NodePeer::deleteNodeSubTree('1', Param::set($con)); 102 if (Propel::isError($e)) { $con->rollback(); return $e; } 103 104 $setNodePath = 'set' . <?php echo $table->getPhpName() ?>NodePeer::NPATH_PHPNAME(); 105 $obj->$setNodePath('1'); 106 107 $e = $obj->save(Param::set($con)); 108 if (Propel::isError($e)) { $con->rollback(); return $e; } 109 110 $e = $con->commit(); 111 if (Creole::isError($e)) { $con->rollback(); return new PropelException(PROPEL_ERROR_DB, $e); } 112 113 return new <?php echo $table->getPhpName() ?>Node(Param::set($obj)); 114 } 115 116 /** 117 * Inserts a new Node at the top of tree. Any existing root node (along with 118 * its children) will be made a child of the new root node. This is a 119 * safer alternative to createNewRootNode(). 120 * 121 * @param <?php echo $table->getPhpName() ?> Object wrapped by new node. 122 * @param Connection Connection to use. 123 * @return <?php echo $table->getPhpName() ?>Node 124 * @throws PropelException 125 */ 126 function & insertNewRootNode(&$obj, $con = null) 127 { 128 Propel::assertParam($con, '<?php echo $CLASS; ?>', 'insertNewRootNode', 2); 129 $con =& Param::get($con); 130 131 if ($con === null) 132 $con =& Propel::getConnection(<?php echo $table->getPhpName() ?>Peer::DATABASE_NAME()); 133 134 $e = $con->begin(); 135 if (Creole::isError($e)) { $con->rollback(); return new PropelException(PROPEL_ERROR_DB, $e); } 136 137 // Move root tree to an invalid node path. 138 $e = <?php echo $table->getPhpName() ?>NodePeer::moveNodeSubTree('1', '0', Param::set($con)); 139 if (Propel::isError($e)) { $con->rollback(); return $e; } 140 141 $setNodePath = 'set' . <?php echo $table->getPhpName() ?>NodePeer::NPATH_PHPNAME(); 142 143 // Insert the new root node. 144 $obj->$setNodePath('1'); 145 146 $e = $obj->save(Param::set($con)); 147 if (Propel::isError($e)) { $con->rollback(); return $e; } 148 149 // Move the old root tree as a child of the new root. 150 $e = <?php echo $table->getPhpName() ?>NodePeer::moveNodeSubTree('0', '1' . <?php echo $table->getPhpName() ?>NodePeer::NPATH_SEP() . '1', Param::set($con)); 151 if (Propel::isError($e)) { $con->rollback(); return $e; } 152 153 $e = $con->commit(); 154 if (Creole::isError($e)) { $con->rollback(); return new PropelException(PROPEL_ERROR_DB, $e); } 155 156 return new <?php echo $table->getPhpName() ?>Node(Param::set($obj)); 157 } 158 159 /** 160 * Retrieves an array of tree nodes based on specified criteria. Optionally 161 * includes all parent and/or child nodes of the matching nodes. 162 * 163 * @param Criteria Criteria to use. 164 * @param boolean True if ancestors should also be retrieved. 165 * @param boolean True if descendants should also be retrieved. 166 * @param Connection Connection to use. 167 * @return array Array of root nodes. 168 */ 169 function & retrieveNodes(&$criteria, $ancestors = false, $descendants = false, $con = null) 170 { 171 Propel::assertParam($con, '<?php echo $CLASS; ?>', 'retrieveNodes', 4); 172 $con =& Param::get($con); 173 $criteria =& <?php echo $table->getPhpName() ?>NodePeer::buildFamilyCriteria($criteria, $ancestors, $descendants); 174 $rs =& <?php echo $table->getPhpName() ?>Peer::doSelectRS($criteria, Param::set($con)); 175 return <?php echo $table->getPhpName() ?>NodePeer::populateNodes($rs, $criteria); 176 } 177 178 /** 179 * Retrieves a tree node based on a primary key. Optionally includes all 180 * parent and/or child nodes of the matching node. 181 * 182 * @param mixed <?php echo $table->getPhpName() ?> primary key (array for composite keys) 183 * @param boolean True if ancestors should also be retrieved. 184 * @param boolean True if descendants should also be retrieved. 185 * @param Connection Connection to use. 186 * @return <?php echo $table->getPhpName() ?>Node 187 */ 188 function & retrieveNodeByPK($pk, $ancestors = false, $descendants = false, $con = null) 189 { 190 Propel::assertParam($con, '<?php echo $CLASS; ?>', 'retrieveNodeByPK', 4); 191 $con =& Param::get($con); 192 return new PropelException(PROPEL_ERROR, 'retrieveNodeByPK() not implemented yet.'); 193 } 194 195 /** 196 * Retrieves a tree node based on a node path. Optionally includes all 197 * parent and/or child nodes of the matching node. 198 * 199 * @param string Node path to retrieve. 200 * @param boolean True if ancestors should also be retrieved. 201 * @param boolean True if descendants should also be retrieved. 202 * @param Connection Connection to use. 203 * @return <?php echo $table->getPhpName() ?>Node 204 */ 205 function & retrieveNodeByNP($np, $ancestors = false, $descendants = false, $con = null) 206 { 207 Propel::assertParam($con, '<?php echo $CLASS; ?>', 'retrieveNodeByNP', 4); 208 $con =& Param::get($con); 209 $criteria =& new Criteria(<?php echo $table->getPhpName() ?>Peer::DATABASE_NAME()); 210 $criteria->add(<?php echo $table->getPhpName() ?>NodePeer::NPATH_COLNAME(), $np, Criteria::EQUAL()); 211 $criteria =& <?php echo $table->getPhpName() ?>NodePeer::buildFamilyCriteria($criteria, $ancestors, $descendants); 212 $rs =& <?php echo $table->getPhpName() ?>Peer::doSelectRS($criteria, Param::set($con)); 213 $nodes =& <?php echo $table->getPhpName() ?>NodePeer::populateNodes($rs, $criteria); 214 return (count($nodes) == 1 ? $nodes[0] : null); 215 } 216 217 /** 218 * Retrieves the root node. 219 * 220 * @param string Node path to retrieve. 221 * @param boolean True if descendants should also be retrieved. 222 * @param Connection Connection to use. 223 * @return <?php echo $table->getPhpName() ?>Node 224 */ 225 function & retrieveRootNode($descendants = false, $con = null) 226 { 227 Propel::assertParam($con, '<?php echo $CLASS; ?>', 'retrieveRootNode', 2); 228 $con =& Param::get($con); 229 return <?php echo $table->getPhpName() ?>NodePeer::retrieveNodeByNP('1', false, $descendants, Param::set($con)); 230 } 231 232 /** 233 * Moves the node subtree at srcpath to the dstpath. This method is intended 234 * for internal use by the BaseNode object. Note that it does not check for 235 * preexisting nodes at the dstpath. It also does not update the node path 236 * of any Node objects that might currently be in memory. 237 * 238 * Use at your own risk! 239 * 240 * @param string Source node path to move (root of the src subtree). 241 * @param string Destination node path to move to (root of the dst subtree). 242 * @param Connection Connection to use. 243 * @return void 244 * @throws PropelException 245 * @todo This is currently broken for simulated "onCascadeDelete"s. 246 * @todo Need to abstract the SQL better. The CONCAT sql function doesn't 247 * seem to be standardized (i.e. mssql), so maybe it needs to be moved 248 * to DBAdapter. 249 */ 250 function moveNodeSubTree($srcPath, $dstPath, $con = null) 251 { 252 Propel::assertParam($con, '<?php echo $CLASS; ?>', 'moveNodeSubTree', 3); 253 $con =& Param::get($con); 254 255 if (substr($dstPath, 0, strlen($srcPath)) == $srcPath) 256 return new PropelException(PROPEL_ERROR, 'Cannot move a node subtree within itself.'); 257 258 if ($con === null) 259 $con =& Propel::getConnection(<?php echo $table->getPhpName() ?>Peer::DATABASE_NAME()); 260 261 /** 262 * Example: 263 * UPDATE table 264 * SET npath = CONCAT('1.3', SUBSTRING(npath, 6, 74)) 265 * WHERE npath = '1.2.2' OR npath LIKE '1.2.2.%' 266 */ 267 268 $npath = <?php echo $table->getPhpName() ?>NodePeer::NPATH_COLNAME(); 269 //the following dot isn`t mean`t a nodeKeySeperator 270 $setcol = substr($npath, strpos($npath, '.')+1); 271 $setcollen = <?php echo $npath_len ?>; 272 $db = Propel::getDb(<?php echo $table->getPhpName() ?>Peer::DATABASE_NAME()); 273 274 // <hack> 275 if (<?php echo $table->getPhpName() ?>NodePeer::isCodeBase(Param::set($con))) 276 { 277 // This is a hack to get CodeBase working. It will eventually be removed. 278 // It is a workaround for the following CodeBase bug: 279 // -Prepared statement parameters cannot be embedded in SQL functions (i.e. CONCAT) 280 $sql = "UPDATE " . <?php echo $table->getPhpName() ?>Peer::TABLE_NAME() . " " . 281 "SET $setcol=" . $db->concatString("'$dstPath'", $db->subString($npath, strlen($srcPath)+1, $setcollen)) . " " . 282 "WHERE $npath = '$srcPath' OR $npath LIKE '$srcPath" . <?php echo $table->getPhpName() ?>NodePeer::NPATH_SEP() . "%'"; 283 284 $con->executeUpdate($sql); 285 } 286 else 287 { 288 // </hack> 289 $sql = "UPDATE " . <?php echo $table->getPhpName() ?>Peer::TABLE_NAME() . " " . 290 "SET $setcol=" . $db->concatString('?', $db->subString($npath, '?', '?')) . " " . 291 "WHERE $npath = ? OR $npath LIKE ?"; 292 293 $stmt =& $con->prepareStatement($sql); 294 $stmt->setString(1, $dstPath); 295 $stmt->setInt(2, strlen($srcPath)+1); 296 $stmt->setInt(3, $setcollen); 297 $stmt->setString(4, $srcPath); 298 $stmt->setString(5, $srcPath . <?php echo $table->getPhpName() ?>NodePeer::NPATH_SEP() . '%'); 299 $stmt->executeUpdate(); 300 // <hack> 301 } 302 // </hack> 303 } 304 305 /** 306 * Deletes the node subtree at the specified node path from the database. 307 * 308 * @param string Node path to delete 309 * @param Connection Connection to use. 310 * @return void 311 * @throws PropelException 312 * @todo This is currently broken for simulated "onCascadeDelete"s. 313 */ 314 function deleteNodeSubTree($nodePath, $con = null) 315 { 316 Propel::assertParam($con, '<?php echo $CLASS; ?>', 'deleteNodeSubTree', 2); 317 $con =& Param::get($con); 318 319 if ($con === null) 320 $con =& Propel::getConnection(<?php echo $table->getPhpName() ?>Peer::DATABASE_NAME()); 321 322 /** 323 * DELETE FROM table 324 * WHERE npath = '1.2.2' OR npath LIKE '1.2.2.%' 325 */ 326 327 $criteria =& new Criteria(<?php echo $table->getPhpName() ?>Peer::DATABASE_NAME()); 328 $criteria->add(<?php echo $table->getPhpName() ?>NodePeer::NPATH_COLNAME(), $nodePath, Criteria::EQUAL()); 329 $criteria->addOr(<?php echo $table->getPhpName() ?>NodePeer::NPATH_COLNAME(), $nodePath . <?php echo $table->getPhpName() ?>NodePeer::NPATH_SEP() . '%', Criteria::LIKE()); 330 // For now, we call BasePeer directly since <?php echo $table->getPhpName() ?>Peer tries to 331 // do a cascade delete. 332 // <?php echo $table->getPhpName() ?>Peer::doDelete($criteria, Param::set($con)); 333 BasePeer::doDelete($criteria, $con); 334 } 335 336 /** 337 * Builds the criteria needed to retrieve node ancestors and/or descendants. 338 * 339 * @param Criteria Criteria to start with 340 * @param boolean True if ancestors should be retrieved. 341 * @param boolean True if descendants should be retrieved. 342 * @return Criteria 343 */ 344 function & buildFamilyCriteria(&$criteria, $ancestors = false, $descendants = false) 345 { 346 /* 347 Example SQL to retrieve nodepath '1.2.3' with both ancestors and descendants: 348 349 SELECT L.NPATH, L.LABEL, test.NPATH, UCASE(L.NPATH) 350 FROM test L, test 351 WHERE test.NPATH='1.2.3' AND 352 (L.NPATH=SUBSTRING(test.NPATH, 1, LENGTH(L.NPATH)) OR 353 test.NPATH=SUBSTRING(L.NPATH, 1, LENGTH(test.NPATH))) 354 ORDER BY UCASE(L.NPATH) ASC 355 */ 356 357 if ($criteria === null) 358 $criteria =& new Criteria(<?php echo $table->getPhpName() ?>::DATABASE_NAME()); 359 360 if (!$criteria->getSelectColumns()) 361 <?php echo $table->getPhpName() ?>Peer::addSelectColumns($criteria); 362 363 $db =& Propel::getDb($criteria->getDbName()); 364 365 if (($ancestors || $descendants) && $criteria->size()) 366 { 367 // If we are retrieving ancestors/descendants, we need to do a 368 // self-join to locate them. The exception to this is if no search 369 // criteria is specified. In this case we're retrieving all nodes 370 // anyway, so there is no need to do a self-join. 371 372 // The left-side of the self-join will contain the columns we'll 373 // use to build node objects (target node records along with their 374 // ancestors and/or descendants). The right-side of the join will 375 // contain the target node records specified by the initial criteria. 376 // These are used to match the appropriate ancestor/descendant on 377 // the left. 378 379 // Specify an alias for the left-side table to use. 380 $criteria->addAlias('L', <?php echo $table->getPhpName() ?>Peer::TABLE_NAME()); 381 382 // Make sure we have select columns to begin with. 383 if (!$criteria->getSelectColumns()) 384 <?php echo $table->getPhpName() ?>Peer::addSelectColumns($criteria); 385 386 // Replace any existing columns for the right-side table with the 387 // left-side alias. 388 $selectColumns = $criteria->getSelectColumns(); 389 $criteria->clearSelectColumns(); 390 foreach ($selectColumns as $colName) 391 $criteria->addSelectColumn(str_replace(<?php echo $table->getPhpName() ?>Peer::TABLE_NAME(), 'L', $colName)); 392 393 $a = null; 394 $d = null; 395 396 $npathL = <?php echo $table->getPhpName() ?>Peer::alias('L', <?php echo $table->getPhpName() ?>NodePeer::NPATH_COLNAME()); 397 $npathR = <?php echo $table->getPhpName() ?>NodePeer::NPATH_COLNAME(); 398 $npath_len = <?php echo $npath_len ?>; 399 400 if ($ancestors) 401 { 402 // For ancestors, match left-side node paths which are contained 403 // by right-side node paths. 404 $a =& $criteria->getNewCriterion($npathL, 405 "$npathL=" . $db->subString($npathR, 1, $db->strLength($npathL), $npath_len), 406 Criteria::CUSTOM()); 407 } 408 409 if ($descendants) 410 { 411 // For descendants, match left-side node paths which contain 412 // right-side node paths. 413 $d =& $criteria->getNewCriterion($npathR, 414 "$npathR=" . $db->subString($npathL, 1, $db->strLength($npathR), $npath_len), 415 Criteria::CUSTOM()); 416 } 417 418 if ($a) 419 { 420 if ($d) $a->addOr($d); 421 $criteria->addAnd($a); 422 } 423 else if ($d) 424 { 425 $criteria->addAnd($d); 426 } 427 428 // Add the target node path column. This is used by populateNodes(). 429 $criteria->addSelectColumn($npathR); 430 431 // Sort by node path to speed up tree construction in populateNodes() 432 $criteria->addAsColumn('npathlen', $db->strLength($npathL)); 433 $criteria->addAscendingOrderByColumn('npathlen'); 434 $criteria->addAscendingOrderByColumn($npathL); 435 } 436 else 437 { 438 // Add the target node path column. This is used by populateNodes(). 439 $criteria->addSelectColumn(<?php echo $table->getPhpName() ?>NodePeer::NPATH_COLNAME()); 440 441 // Sort by node path to speed up tree construction in populateNodes() 442 $criteria->addAsColumn('npathlen', $db->strLength(<?php echo $table->getPhpName() ?>NodePeer::NPATH_COLNAME())); 443 $criteria->addAscendingOrderByColumn('npathlen'); 444 $criteria->addAscendingOrderByColumn(<?php echo $table->getPhpName() ?>NodePeer::NPATH_COLNAME()); 445 } 446 447 return $criteria; 448 } 449 450 /** 451 * This method reconstructs as much of the tree structure as possible from 452 * the given array of objects. Depending on how you execute your query, it 453 * is possible for the ResultSet to contain multiple tree fragments (i.e. 454 * subtrees). The array returned by this method will contain one entry 455 * for each subtree root node it finds. The remaining subtree nodes are 456 * accessible from the <?php echo $table->getPhpName() ?>Node methods of the 457 * subtree root nodes. 458 * 459 * @param array Array of <?php echo $table->getPhpName() ?>Node objects 460 * @return array Array of <?php echo $table->getPhpName() ?>Node objects 461 */ 462 function & buildTree(&$nodes) 463 { 464 // Subtree root nodes to return 465 $rootNodes = array(); 466 $copy = $nodes; 467 468 // Build the tree relations 469 foreach ($nodes as $key1 => $node) 470 { 471 //strrpos fix 472 $sep = 0; 473 while(false !== ($last = strpos($node->getNodePath(), <?php echo $table->getPhpName() ?>NodePeer::NPATH_SEP(), $sep))) { 474 $sep = $last + strlen(<?php echo $table->getPhpName() ?>NodePeer::NPATH_SEP()); 475 } 476 $parentPath = ($sep != 0 ? substr($node->getNodePath(), 0, $sep - strlen(<?php echo $table->getPhpName() ?>NodePeer::NPATH_SEP())) : ''); 477 $parentNode =& Propel::null(); 478 479 // Scan other nodes for parent. 480 foreach ($copy as $key2 => $pnode) 481 { 482 if ($pnode->getNodePath() == $parentPath) 483 { 484 $parentNode =& $nodes[$key2]; 485 break; 486 } 487 } 488 489 // If parent was found, attach as child, otherwise its a subtree root 490 if ($parentNode) 491 $parentNode->attachChildNode($nodes[$key1]); 492 else 493 $rootNodes[] =& $nodes[$key1]; 494 } 495 496 return $rootNodes; 497 } 498 499 /** 500 * Populates the <?php echo $table->getPhpName() ?> objects from the 501 * specified ResultSet, wraps them in <?php echo $table->getPhpName() ?>Node 502 * objects and build the appropriate node relationships. 503 * The array returned by this method will only include the initial targets 504 * of the query, even if ancestors/descendants were also requested. 505 * The ancestors/descendants will be cached in memory and are accessible via 506 * the getNode() methods. 507 * 508 * @param ResultSet 509 * @param Criteria 510 * @return array Array of <?php $table->getPhpName() ?>Node objects. 511 */ 512 function & populateNodes(&$rs, &$criteria) 513 { 514 $nodes = array(); 515 $targets = array(); 516 $values = array(); 517 $targetfld = count($criteria->getSelectColumns()); 518 519 <?php if (!$table->getChildrenColumn()) { ?> 520 // set the class once to avoid overhead in the loop 521 $cls = <?php echo $table->getPhpName() ?>Peer::getOMClass(); 522 if (Propel::isError($cls =& Propel::import($cls))) { 523 return $cls; 524 } 525 <?php } ?> 526 527 // populate the object(s) 528 while($rs->next()) 529 { 530 if (!isset($nodes[$rs->getString(1)])) 531 { 532 <?php if ($table->getChildrenColumn()) { ?> 533 // class must be set each time from the record row 534 $cls =& Propel::import(<?php echo $table->getPhpName() ?>Peer::getOMClass($rs, 1)); 535 if (Propel::isError($cls)) { 536 return $cls; 537 } 538 <?php } ?> 539 $obj =& new $cls(); 540 541 if (Propel::isError($e =& $obj->hydrate($rs))) { 542 return $e; 543 } 544 545 $nodes[$rs->getString(1)] =& new <?php echo $table->getPhpName() ?>Node(Param::set($obj)); 546 } 547 $node =& $nodes[$rs->getString(1)]; 548 549 if ($node->getNodePath() == $rs->getString($targetfld)) 550 $targets[$node->getNodePath()] =& $node; 551 } 552 553 <?php echo $table->getPhpName() ?>NodePeer::buildTree($nodes); 554 555 foreach($targets as $key => $value) 556 $values[] =& $targets[$key]; 557 558 return $values; 559 } 560 561 } 562 563 ?>
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 |