Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [ZendFramework] Zend_Db
Forum PHP.pl > Forum > PHP > Frameworki
nexis
Mam następujące tabele:

Kod
product (przechowuje główne cechy produktu - np. nazwę):
- id
- label


Kod
item (przechowuje cechy konkretnego egzemplarza, który może się różnić rozmiarem, kolorem, itd. - mają różne ceny):
- id
- product (klucz product.id)
- price


Kod
category:
- id
- parent
- label


Kod
product2category:
- product (klucz product.id)
- category (klucz category.id)


Korzystam z nich za pomocą Zend_Db_Table - np.:

  1. <?php
  2. class Model_DbTable_Category extends Zend_Db_Table_Abstract
  3. {
  4. protected $_name = 'category';
  5. }
  6. ?>


Kategorie mają konstrukcję rekurencyjną tj.:

Kod
|--+ Buty
   |--- Adidas
   |--- Nike
   |--+ Puma
      |--- X
      |--- Y
      |--- Pozostałe


Dla danej kategorii - np. "Buty" - chciałbym otrzymać wszystkie produkty (product.id, product.label) wraz z najniższą ceną (MIN(item.price)) dla tej kategorii i wszystkich podkategorii. Jak coś takiego skonstruować w ZF, aby nadawało się do paginacji?
pgrzelka
jak zmienisz strukturę kategorii na drzewka metodą ip to będziesz mógł dużo łatwiej pobierać wszystkie rekordy

Kod
WHERE ip LIKE CONCAT("'.$cat['ip'].'.%")


http://blog.mwojcik.pl/2008/02/17/drzewa-k...-php-metoda-ip/
Sajrox
Polecam także metodę Nested Set:

http://dev.mysql.com/tech-resources/articl...hical-data.html

Sam używam i sprawuje się bardzo dobrze.

Moja klasa gdyby była potrzebna smile.gif
  1. class Model_Categories extends Ext_Db {
  2.  
  3. /**
  4.   * @var String $type zmienna wskazuje moduł do którego mamy pobrać kategorie
  5.   */
  6. protected $_type = NULL;
  7.  
  8.  
  9. /**
  10.   * Ustawienie Modelu
  11.   ********************************************************************************
    ***************/
  12. public function __construct($type) {
  13. parent::__construct();
  14.  
  15. if (isset($type))
  16. $this->_type = $type;
  17. else
  18. throw new Exception('Not given the <b>type</b> of category');
  19. }
  20.  
  21. /**
  22.   * Ustawienie Modelu
  23.   ********************************************************************************
    ***************/
  24. public function _setup() {
  25. parent::_setup();
  26.  
  27. $this->_name = 'categories';
  28. $this->_tableDescriptions = 'categories_descriptions';
  29. $this->_dependentTables = array('Model_CategoriesDescriptions');
  30. }
  31.  
  32. /**
  33.   * Pobranie drzewa kategorii
  34.   ********************************************************************************
    ***************/
  35. public function fetchTree($lang, $id=null) {
  36.  
  37. $tree = array();
  38.  
  39. // pobierz parametry glownego wezla
  40. $whereRoot['type=?'] = $this->_type;
  41.  
  42. if (!is_null($id))
  43. $whereRoot['id=?'] = $id;
  44. else
  45. $whereRoot['depth=?'] = 0;
  46.  
  47. $root = $this->fetchOneByWhere($whereRoot);
  48.  
  49. if ($root != false) {
  50. $tree[] = $root->toArray();
  51.  
  52. // Pobranie wszystkich węzłów
  53. $select = $this->select();
  54. $select->setIntegrityCheck(false);
  55. $select->from(array('t1' => $this->_name));
  56. $select->joinleft(array('t2' => $this->_tableDescriptions), "t1.id = t2.{$this->_name}_id");
  57. $select->order('t1.lft ASC');
  58.  
  59. $select = $this->buildSql($select, array(
  60. 't1.lft >= ?' => $root->lft,
  61. 't1.lft <= ?' => $root->rgt,
  62. 't1.type=?' => $this->_type,
  63. 't2.languages_code=\''.$lang.'\' OR t2.languages_code=\'\''
  64. ));
  65.  
  66.  
  67. $childs = $this->fetchAll($select);
  68.  
  69. if ($childs != false) {
  70. // Połączenie tabeli rodzica z węzłami podrzędnymi
  71. $tree = $childs->toArray();
  72. }
  73. }
  74.  
  75. return $tree;
  76. }//End
  77.  
  78. /**
  79.   * Dodanie kategorii
  80.   ********************************************************************************
    ***************/
  81. public function insertNode($id, $name, $lang) {
  82.  
  83. $this->begin();
  84.  
  85. try {
  86. //$this->prepare("LOCK TABLES `{$this->_name}` WRITE;");
  87.  
  88. // pobierz parametry węzła nadrzędnego do którego dodajemy kategorię
  89. if (is_null($id)) {
  90. $root = $this->fetchOneByWhere(array(
  91. 'type=?' => $this->_type,
  92. 'depth=?' => 0
  93. ));
  94. }
  95. else {
  96. $root = $this->fetchOneByWhere(array(
  97. 'type=?' => $this->_type,
  98. 'id=?' => $id
  99. ));
  100. }
  101.  
  102. if ($root != false) {
  103. $id = $root->id;
  104. $lft = $root->lft;
  105. $rgt = $root->rgt;
  106. $depth = $root->depth+1;
  107.  
  108. // Aktualizacja tabeli - przesuwanie węzłów
  109. $this->query("UPDATE {$this->_name} SET `rgt` = `rgt`+2 WHERE `type` = '{$this->_type}' AND `rgt` > ".($rgt-1));
  110. $this->query("UPDATE {$this->_name} SET `lft` = `lft`+2 WHERE `type` = '{$this->_type}' AND `lft` > ".($rgt-1));
  111.  
  112. $lastId = $this->insert(array(
  113. 'parent_id' => $id,
  114. 'lft' => $rgt,
  115. 'rgt' => ($rgt+1),
  116. 'depth' => $depth,
  117. 'type' => $this->_type
  118. ));
  119.  
  120. $this->getAdapter()->insert($this->_tableDescriptions, array(
  121. 'categories_id' => $lastId,
  122. 'languages_code' => $lang,
  123. 'name' => $name
  124. ));
  125.  
  126. //$this->prepare("UNLOCK TABLES");
  127. $this->commit();
  128.  
  129. return $lastId;
  130. }
  131. else
  132. throw new Exception('Root category is not exists');
  133.  
  134. } catch (Exception $e) {
  135. $this->rollBack();
  136. new Ext_Exception($e);
  137. }
  138. }//End
  139. }

melkorm
Też używam tej metody smile.gif polecam jeszcze dorzucenie nested_dpeth, czyli głębokość drzewa czasem się przydaje winksmiley.jpg
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.