Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [Symfony] implementacja NestedTree
Forum PHP.pl > Forum > PHP > Frameworki
jareeny
Więc moje pytanie brzmi: jak zaimplementować drzewo w Symfony, na już istniejącej bazie danych? Trzeba w niej robić jakieś poprawki? Znalazłem w googlach parę artykułów, ale po angielsku i nie rozumiem za bardzo choć bardzo się starałem :/

Używam Symfony 1.2 i Propel'a 1.3

bardzo bym prosił o jakiś prosty przykładzik... smile.gif
prohol
1. dodaj do tabeli trzy pola: lft, rgt oraz scope i pozakladaj na nie indeksy
2. jak uzywasz propela to jest funkcja cli to zgrania struktury istniejacej juz bazy i pozmieniaj pola tabeli kategori tak jak to jest napisane na stronie

http://propel.phpdb.org/trac/wiki/Users/Do.../Tree/NestedSet

(kolumny lft, rgt oraz scope oraz treeMode odpowiedni)

  1. <table name="menu" idMethod="native" treeMode="NestedSet">
  2.    <column name="id" type="INTEGER" required="true" autoIncrement="true" primaryKey="true"/>
  3.    <column name="lft" type="INTEGER" required="true" default="0" nestedSetLeftKey="true"/>
  4.    <column name="rgt" type="INTEGER" required="true" default="0" nestedSetRightKey="true"/>
  5.    <column name="scope" type="INTEGER" required="true" default="0" treeScopeKey="true"/>
  6.    <column name="text" type="VARCHAR" size="128" required="true" default=""/>
  7.    <column name="link" type="VARCHAR" size="255" required="true" default=""/>
  8.    <index name="lft">
  9.      <index-column name="lft"/>
  10.    </index>
  11.    <index name="rgt">
  12.      <index-column name="rgt"/>
  13.    </index>
  14.    <index name="scope">
  15.      <index-column name="scope"/>
  16.    </index>
  17.  </table>

3. wygeneruj sobie model, formularze i co ci tam potrzeba
4. Przykladowe klasy komponentu do wyswietlenia drzewka kategorii (domysle tylko 1 poziom zagniezdzenie a po kliknieciu pokazanie dzieci itd)

  1. <?php
  2. class categoriesComponents extends sfComponents {
  3.  public $cat_tree=array();
  4.  
  5.  public function executeList() {
  6.    $root = JmdataCategoriesPeer::retrieveTree(1);
  7.    $this->it=new myMenuOutput($root);
  8.    if ($this->getModuleName() == 'categories' && $this->request->getParameter('id')) {
  9.      $this->node=JmdataCategoriesPeer::getNode($this->request->getParameter('id'));
  10.      $this->node_path=JmdataCategoriesPeer::getPath($this->node);
  11.      $root=JmdataCategoriesPeer::retrieveRoot(1);
  12.      $this->getPath($root);
  13.      $this->tree=$this->cat_tree;
  14.    }
  15.  
  16.    
  17.  }
  18.  
  19.  private function getPath($root_node) {
  20.     foreach ($root_node->getChildren() as $node) {
  21.      if (!$this->checkPath($node) && $node->getLevel() == 1) {
  22.        $this->cat_tree[]=array('name'=>$node->getName(), 'id'=>$node->getId(),'level'=>$node->getLevel());
  23.      } else {
  24.        $this->cat_tree[]=array('name'=>$node->getName(), 'id'=>$node->getId(),'level'=>$node->getLevel());
  25.        $this->getPath($node);
  26.      }
  27.    }
  28.  }
  29.  
  30.  private function checkPath($node) {
  31.    foreach ($this->node_path as $child)
  32.      if ($child->getId() == $node->getId())
  33.        return true;
  34.    return false;
  35.  }
  36. }
  37. ?>


template:

  1. <div id="adminMenu">
  2. <h4><a href="">.::Kategorie::.</a></h4>
  3.  <ul>
  4.    <?php if (!$sf_request->hasParameter('id')): ?>
  5.      <?php foreach($it as $m): ?>
  6.        <?php if ($m->getLevel() == 1): ?>
  7.          <li style="padding-left: <?php echo $m->getLevel() ?>em;">
  8.          <?php echo link_to( $m->getName(),'categories/show?id='.$m->getId()) ?>
  9.          </li>
  10.        <?php endif; ?>
  11.      <?php endforeach; ?>
  12.    <?php else: ?>
  13.      <?php foreach ($tree as $nodes): ?>
  14.        <li style="padding-left: <?php echo $nodes['level']-1 ?>em;">
  15.        <?php echo link_to( $nodes['name'],'categories/show?id='.$nodes['id']) ?>
  16.        </li>
  17.      <?php endforeach ?>
  18.    <?php endif; ?>
  19.  </ul>
  20. </div>


Kod klasy myMenuOutput:

  1. <?php
  2. class myMenuOutput extends RecursiveIteratorIterator {
  3.  function __construct(JmdataCategories $m) {
  4.    parent::__construct($m, self::SELF_FIRST);
  5.  }
  6.  
  7.  function beginChildren() {
  8.    echo str_repeat("\t", $this->getDepth());
  9.  }
  10.  
  11.  function endChildren() {
  12.    echo str_repeat("\t", $this->getDepth() - 1);
  13.  }
  14. }
  15. ?>
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.