Doctrine Nested set: récupérer les items de toute la sous-arborescence.
Bonjour,
Je poste ce message pour être sûr que mon idée est bien la bonne car je suis débutant.
Petit récapitulatif (juste l'affichage des items couplé à leur catégorie):
Schema.yml:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
Category:
actAs:
NestedSet:
hasManyRoots: true
rootColumnName: root_id
columns:
name:
type: string()
Item:
columns:
name:
type: string()
category_id:
type: integer
relations:
Category:
local: category_id
foreign: id
foreignAlias: Items |
fixture:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
Category:
arbre:
name: Arbre
children:
1:
name: Catégorie principale 1
children:
11:
name: Catégorie secondaire 1
12:
name: Catégorie secondaire 2
13:
name: Catégorie secondaire 3
14:
name: Catégorie secondaire 4
children:
141:
name: Catégorie tertiaire 1
142:
name: Catégorie tertiaire 2
15:
name: Catégorie secondaire 5
16:
name: Catégorie secondaire 6
2:
name: Catégorie principale 2
3:
name: Catégorie principale 3
children:
31:
name: Catégorie secondaire 1
32:
name: Catégorie secondaire 2
33:
name: Catégorie secondaire 3
4:
name: Catégorie principale 4
children:
41:
name: Catégorie secondaire 1
Item:
i1:
name: Baseline
Category: 11
i2:
name: Product
Category: 11
i3:
name: Analysis
Category: 12
i4:
name: Architecture
Category: 12
i5:
name: Design review
Category: 12
i6:
name: Feature analysis
Category: 13
i7:
name: Implementation
Category: 141
i8:
name: Review
Category: 141
i9:
name: Test
Category: 142
i10:
name: Stabilization
Category: 16
i11:
name: Feature
Category: 16
i12:
name: Feature view
Category: 2
i13:
name: Fixing
Category: 2
i14:
name: Repport
Category: 41 |
Avec ça je peux afficher les items couplé à leur catégorie dans mon template avec un truc du genre:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
class CategoryTable extends Doctrine_Table
{
public function withItems()
{
$q = Doctrine_Query::create()
->select('c.name')
->from('Category c')
->leftJoin('c.Items i');
$treeObject = Doctrine_Core::getTable('Category')->getTree();
$treeObject->setBaseQuery($q);
$tree = $treeObject->fetchTree();
$treeObject->resetBaseQuery();
return $tree;
}
} |
et:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
class Category extends BaseCategory
{
public function getItems()
{
$q = Doctrine_Query::create()
->from('Item i')
->where('i.category_id = ?', $this->getId());
return $q->execute();
}
} |
MAINTENANT:
Je veux afficher une categorie avec ses items et ceux des categories sous-adjacentes.
J'ai eu l'idée de rajouter les colonnes "rgt" et "lft" à ma table Item, ces dernières couplé avec "rgt" et "lft" de la table Category.
et de faire:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
class Category extends BaseCategory
{
public function getItems()
{
$q = Doctrine_Query::create()
->from('Item i')
->where('i.lft BETWEEN ? AND ?', array($this->getLft(), $this->getRgt())
return $q->execute();
}
} |
Cette methode est-elle bonne oubien il y a t-il une meilleure?