Development

HowToHandleHierarchicalDataWithPropel (diff)

You must first sign up to be able to contribute.

Changes between Version 6 and Version 7 of HowToHandleHierarchicalDataWithPropel

Show
Ignore:
Author:
l2k (IP: 88.64.10.230)
Timestamp:
05/12/07 21:20:16 (11 years ago)
Comment:

replace by reference to sfPropelActAsNestedSetBehaviorPlugin

Legend:

Unmodified
Added
Removed
Modified
  • HowToHandleHierarchicalDataWithPropel

    v6 v7  
    1 '''WARNING'''[[BR]] 
    2 This code may be moved to a pear package later. Let's just be sure there aren't bugs in it anymore first.[[BR]] 
    3 The following source code is NOT ready for production usages. Use it at your own risk.[[BR]] 
    4 Method names may change in the future and won't after the first stable release (I hope so).[[BR]] 
     1The content on this page has been superseded by the [wiki:sfPropelActAsNestedSetBehaviorPlugin sfPropelActAsNestedSetBehaviorPlugin] 
    52 
    6 = sfPropelTree plugin = 
    7  
    8 Create in file with the following path : '''/lib/plugins/sfPropelTree/sfPropelTree.class.php'''[[BR]] 
    9 Paste the content from http://www.igreg.info/sfPropelTree/sfPropelTree.class.php.txt [[BR]] 
    10 Syntax highlighted http://www.igreg.info/sfPropelTree/sfPropelTree.class.phps 
    11  
    12 '''Clear the cache''' for symfony to find this new plugin. 
    13  
    14 {{{ 
    15 symfony cc 
    16 }}} 
    17  
    18 = schema.xml = 
    19  
    20 Now, add two columns to the table that will store the tree.[[BR]] 
    21 You cannot change the names of theses two columns. 
    22  
    23 {{{ 
    24 <table ....> 
    25     ... 
    26     <column name="lft" type="integer" required="true" /> 
    27     <column name="rgt" type="integer" required="true" /> 
    28     ... 
    29 </table> 
    30 }}} 
    31  
    32 Then rebuild DB structure 
    33  
    34 {{{ 
    35 symfony propel-build-sql 
    36 symfony propel-build-model 
    37 }}} 
    38  
    39 And don't forget to apply table modification to mysql. 
    40  
    41 = MyTable.class.php = 
    42  
    43 Add this methods to your class. This will keep the class clean and will make ''shortcuts'' to the real functions in sfPropelTree. 
    44  
    45 {{{ 
    46 <?php 
    47  
    48 class MyTable extends BaseMyTable { 
    49   // sfPropelTree Functions. Do not modify unless you know what you are doing 
    50   function save($con = null)      { sfPropelTree::saveNode($this); parent::save($con); } 
    51   function delete($con = null)    { sfPropelTree::deleteNode($this); parent::delete($con); } 
    52   function deleteChilds()         { return sfPropelTree::deleteChilds($this); } 
    53   function addChild($child)       { return sfPropelTree::addChildBottom($this, $child); } 
    54   function addChildTop($child)    { return sfPropelTree::addChildTop($this, $child); } 
    55   function addChildBottom($child) { return sfPropelTree::addChildBottom($this, $child); } 
    56   function addLeft($child)        { return sfPropelTree::addBefore($this, $child); } 
    57   function addBefore($child)      { return sfPropelTree::addBefore($this, $child); } 
    58   function addRight($child)       { return sfPropelTree::addAfter($this, $child); } 
    59   function addAfter($child)       { return sfPropelTree::addAfter($this, $child); } 
    60   function moveBefore($refNode)   { return sfPropelTree::moveBefore($refNode, $this); } 
    61   function moveAfter($refNode)    { return sfPropelTree::moveAfter($refNode, $this); } 
    62   function hasChilds()            { return sfPropelTree::hasChilds($this); } 
    63   function getChilds()            { return sfPropelTree::getChilds($this); } 
    64   function getFirstChild()        { return sfPropelTree::getFirstChild($this); } 
    65   function getLastChild()         { return sfPropelTree::getLastChild($this); } 
    66   function getParent()            { return sfPropelTree::getParent($this); } 
    67   function getRoot()              { return sfPropelTree::getRoot($this); } 
    68   function getPath()              { return sfPropelTree::getPath($this); } 
    69   function getLeft()              { return sfPropelTree::getLeft($this); } 
    70   function getRight()             { return sfPropelTree::getRight($this); } 
    71 
    72  
    73 ?> 
    74 }}} 
    75  
    76 = MyTablePeer.class.php = 
    77  
    78 Do the same, but for MyTablePeer.class.php this time. 
    79  
    80 {{{ 
    81 <?php 
    82  
    83 class MyTablePeer extends BaseMyTablePeer { 
    84   // sfPropelTree Functions. Do not modify unless you know what you are doing 
    85   static function getIdField ()   { return self::ID; } 
    86   static function getLftField ()  { return self::LFT; } 
    87   static function getRgtField ()  { return self::RGT; } 
    88   static function getTable()      { return self::TABLE_NAME; } 
    89   static function getRoot()       { return sfPropelTree::getRoot(__CLASS__); } 
    90   static function print_r()       { return sfPropelTree::print_r(__CLASS__); } // debug function 
    91 
    92  
    93 ?>  
    94 }}} 
    95  
    96 = Examples of use = 
    97  
    98 In the following examples, I have setup a table named '''Page'''.[[BR]] 
    99 Each page can contains subpages. 
    100  
    101 == propeltreedemo_schema.xml == 
    102  
    103 {{{ 
    104 <?xml version="1.0" encoding="UTF-8"?> 
    105 <database name="propel" defaultIdMethod="native" noxsd="true"> 
    106   <table name="page" phpName="Page"> 
    107     <column name="id" type="integer" required="true" primaryKey="true" autoincrement="true" /> 
    108     <column name="title" type="varchar" size="255" required="true" /> 
    109     <column name="lft" type="integer" required="true" /> 
    110     <column name="rgt" type="integer" required="true" /> 
    111   </table> 
    112 </database> 
    113 }}} 
    114  
    115 Tree structure for each example 
    116 {{{ 
    117   root page 
    118     page 1 
    119     page 2 
    120     page 3 
    121     page 5 
    122     page 8 
    123 }}} 
    124  
    125  
    126  
    127 == add a root node == 
    128  
    129 {{{ 
    130 $page = new Page(); 
    131 $page->setTitle('Page 9'); 
    132 $page->save(); 
    133 }}} 
    134  
    135 results: 
    136  
    137 {{{ 
    138   root page 
    139     page 1 
    140     page 2 
    141     page 3 
    142     page 5 
    143     page 8 
    144     page 9 < 
    145 }}} 
    146  
    147 == add a child to 'Page 1' == 
    148  
    149 {{{ 
    150 $c = new Criteria(); 
    151 $c->add(PagePeer::TITLE, 'page 1'); 
    152 $parent = PagePeer::doSelectOne($c); 
    153  
    154 $child = new Page(); 
    155 $child->setTitle('Page 1.1'); 
    156 $parent->addChild($child); // saves $child in database. No need to call $child->save() after 
    157 // addChild is an equivalent to addChildBottom 
    158 // you could have typed : 
    159 // $parent->addChildBottom($child) 
    160 }}} 
    161  
    162 results : 
    163  
    164 {{{ 
    165   root page 
    166     page 1 
    167         page 1.1 < 
    168     page 2 
    169     page 3 
    170     page 5 
    171     page 8 
    172 }}} 
    173  
    174 == add a child before the other ones == 
    175  
    176 We're using the result of the previous example for this one. 
    177  
    178 {{{ 
    179 $c = new Criteria(); 
    180 $c->add(PagePeer::TITLE, 'page 1'); 
    181 $parent = PagePeer::doSelectOne($c); 
    182  
    183 $child = new Page(); 
    184 $child->setTitle('Page 1.0'); 
    185 $parent->addChildTop($child); // saves $child in database. No need to call $child->save() after 
    186 }}} 
    187  
    188 results: 
    189  
    190 {{{ 
    191   root page 
    192     page 1 
    193         page 1.0 < 
    194         page 1.1 
    195     page 2 
    196     page 3 
    197     page 5 
    198     page 8 
    199 }}} 
    200  
    201 == add a node after another one == 
    202  
    203 {{{ 
    204 $c = new Criteria(); 
    205 $c->add(PagePeer::TITLE, 'page 3'); 
    206 $parent = PagePeer::doSelectOne($c); 
    207  
    208 $child = new Page(); 
    209 $child->setTitle('page 4'); 
    210  
    211 if ($parent && $child) 
    212   $parent->addAfter($child); // saves $child in database. No need to call $child->save() after 
    213 }}} 
    214  
    215 results : 
    216 {{{ 
    217   root page 
    218     page 1 
    219     page 2 
    220     page 3 
    221     page 4 < 
    222     page 5 
    223     page 8 
    224 }}} 
     3[/trac/wiki/HowToHandleHierarchicalDataWithPropel?version=6 click here to see the original content]