Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [SF][SF2][Doctrine2] Problem z pobraniem kategorii podrzednych w oparciu o Nested Tree
Forum PHP.pl > Forum > PHP > Frameworki
swiezak
Witajcie.
Korzystam z Nested Tree (https://github.com/Atlantic18/DoctrineExten...ter/doc/tree.md) do budowania drzewa kategorii i mam problem z pobraniem kategorii podrzednych (getChildren()).

Struktura menu jest mniej wiecej taka:

Kategorie (root)
--Akcesoria lazienkowe
----kosze
----dozowniki
----wieszaki
--Wanny
--Kabiny prysznicowe

Jesli klikne w menu na kategorie (np. Wanny, czy Kabiny prysznicowe), ktora nie ma potomkow, wowczas korzystam w kontrolerze z ponizszego kodu do wyswietlania danych:
  1. public function showcategoryAction($slug)
  2. {
  3. $em = $this->getDoctrine()->getEntityManager();
  4. $category = $em->getRepository('MlFrontendBundle:Categories')->findOneBySlug($slug);
  5. if (!$category) {
  6. throw $this->createNotFoundException('Brak kategorii o podanym parametrze!');
  7. }
  8.  
  9. if ($category->getParent() && $category->getLvl() != 1) {
  10. return $this->render('MlFrontendBundle:Category:showcategory.html.twig', array('category' => $category));
  11. }
  12. }


Dziala to dobrze. W templatce wyswietla mi sie nazwa pojedynczej kategorii i iine zadane informacje.

Problem mam z pobraniem potomkow z kategorii Akcesoria lazienkowe. Zachowanie powinno byc takie, ze po kliknieciu w menu powinna sie pojawic w templatce lista podkategorii.

Probuje w ten sposob:
  1. $categories = $category->getChildren(); // nie dziala


Moja encja z kategoria:
  1. namespace Ml\FrontendBundle\Entity;
  2.  
  3. use Gedmo\Sluggable\Sluggable;
  4. //use Gedmo\Translatable\Translatable;
  5. use Gedmo\Mapping\Annotation as Gedmo;
  6. use Symfony\Component\Validator\Constraints as Assert;
  7.  
  8. use Doctrine\ORM\Mapping as ORM;
  9.  
  10. /**
  11.  * Categories
  12.  *
  13.  * @Gedmo\Tree(type="nested")
  14.  * @ORM\Table(name="categories")
  15.  * @ORM\Entity(repositoryClass="Gedmo\Tree\Entity\Repository\NestedTreeRepository")
  16.  */
  17. class Categories
  18. {
  19. /**
  20.   * @var integer
  21.   * @ORM\Column(name="id", type="integer")
  22.   * @ORM\Id
  23.   * @ORM\GeneratedValue(strategy="AUTO")
  24.   */
  25. private $id;
  26.  
  27. /**
  28.   * @var string
  29.   *
  30.   * @Assert\NotBlank()
  31.   * @ORM\Column(name="name", type="string", length=255)
  32.   */
  33. private $name;
  34.  
  35. /**
  36. * @var string $slug
  37. *
  38. * @Gedmo\Slug(fields={"name"})
  39. * @ORM\Column(length=255, unique=true)
  40. */
  41. private $slug;
  42.  
  43. /**
  44.   * @Gedmo\TreeLeft
  45.   * @ORM\Column(name="lft", type="integer")
  46.   */
  47. private $lft;
  48.  
  49. /**
  50.   * @Gedmo\TreeLevel
  51.   * @ORM\Column(name="lvl", type="integer")
  52.   */
  53. private $lvl;
  54.  
  55. /**
  56.   * @Gedmo\TreeRight
  57.   * @ORM\Column(name="rgt", type="integer")
  58.   */
  59. private $rgt;
  60.  
  61. /**
  62.   * @Gedmo\TreeRoot
  63.   * @ORM\Column(name="root", type="integer", nullable=true)
  64.   */
  65. private $root;
  66.  
  67. /**
  68.   * @Gedmo\TreeParent
  69.   * @ORM\ManyToOne(targetEntity="Categories", inversedBy="children")
  70.   * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE")
  71.   */
  72. private $parent;
  73.  
  74. /**
  75.   * @ORM\OneToMany(targetEntity="Categories", mappedBy="parent")
  76.   * @ORM\OrderBy({"lft" = "ASC"})
  77.   */
  78. private $children;
  79.  
  80. ...
  81.  
  82. public function __toString() {
  83. return $this->name;
  84. }
  85.  
  86. /**
  87.   * Constructor
  88.   */
  89. public function __construct()
  90. {
  91. $this->children = new \Doctrine\Common\Collections\ArrayCollection();
  92. $this->products = new \Doctrine\Common\Collections\ArrayCollection();
  93. }
  94.  
  95. /**
  96.   * Get id
  97.   *
  98.   * @return integer
  99.   */
  100. public function getId()
  101. {
  102. return $this->id;
  103. }
  104.  
  105. /**
  106.   * Set name
  107.   *
  108.   * @param string $name
  109.   *
  110.   * @return Categories
  111.   */
  112. public function setName($name)
  113. {
  114. $this->name = $name;
  115.  
  116. return $this;
  117. }
  118.  
  119. /**
  120.   * Get name
  121.   *
  122.   * @return string
  123.   */
  124. public function getName()
  125. {
  126. return $this->name;
  127. }
  128.  
  129. /**
  130.   * Set slug
  131.   *
  132.   * @param string $slug
  133.   *
  134.   * @return Categories
  135.   */
  136. public function setSlug($slug)
  137. {
  138. $this->slug = $slug;
  139.  
  140. return $this;
  141. }
  142.  
  143. /**
  144.   * Get slug
  145.   *
  146.   * @return string
  147.   */
  148. public function getSlug()
  149. {
  150. return $this->slug;
  151. }
  152.  
  153. /**
  154.   * Set lft
  155.   *
  156.   * @param integer $lft
  157.   *
  158.   * @return Categories
  159.   */
  160. public function setLft($lft)
  161. {
  162. $this->lft = $lft;
  163.  
  164. return $this;
  165. }
  166.  
  167. /**
  168.   * Get lft
  169.   *
  170.   * @return integer
  171.   */
  172. public function getLft()
  173. {
  174. return $this->lft;
  175. }
  176.  
  177. /**
  178.   * Set lvl
  179.   *
  180.   * @param integer $lvl
  181.   *
  182.   * @return Categories
  183.   */
  184. public function setLvl($lvl)
  185. {
  186. $this->lvl = $lvl;
  187.  
  188. return $this;
  189. }
  190.  
  191. /**
  192.   * Get lvl
  193.   *
  194.   * @return integer
  195.   */
  196. public function getLvl()
  197. {
  198. return $this->lvl;
  199. }
  200.  
  201. /**
  202.   * Set rgt
  203.   *
  204.   * @param integer $rgt
  205.   *
  206.   * @return Categories
  207.   */
  208. public function setRgt($rgt)
  209. {
  210. $this->rgt = $rgt;
  211.  
  212. return $this;
  213. }
  214.  
  215. /**
  216.   * Get rgt
  217.   *
  218.   * @return integer
  219.   */
  220. public function getRgt()
  221. {
  222. return $this->rgt;
  223. }
  224.  
  225. /**
  226.   * Set root
  227.   *
  228.   * @param integer $root
  229.   *
  230.   * @return Categories
  231.   */
  232. public function setRoot($root)
  233. {
  234. $this->root = $root;
  235.  
  236. return $this;
  237. }
  238.  
  239. /**
  240.   * Get root
  241.   *
  242.   * @return integer
  243.   */
  244. public function getRoot()
  245. {
  246. return $this->root;
  247. }
  248.  
  249. /**
  250.   * Set parent
  251.   *
  252.   * @param \Ml\FrontendBundle\Entity\Categories $parent
  253.   *
  254.   * @return Categories
  255.   */
  256. public function setParent(\Ml\FrontendBundle\Entity\Categories $parent = null)
  257. {
  258. $this->parent = $parent;
  259.  
  260. return $this;
  261. }
  262.  
  263. /**
  264.   * Get parent
  265.   *
  266.   * @return \Ml\FrontendBundle\Entity\Categories
  267.   */
  268. public function getParent()
  269. {
  270. return $this->parent;
  271. }
  272.  
  273. /**
  274.   * Add child
  275.   *
  276.   * @param \Ml\FrontendBundle\Entity\Categories $child
  277.   *
  278.   * @return Categories
  279.   */
  280. public function addChild(\Ml\FrontendBundle\Entity\Categories $child)
  281. {
  282. $this->children[] = $child;
  283.  
  284. return $this;
  285. }
  286.  
  287. /**
  288.   * Remove child
  289.   *
  290.   * @param \Ml\FrontendBundle\Entity\Categories $child
  291.   */
  292. public function removeChild(\Ml\FrontendBundle\Entity\Categories $child)
  293. {
  294. $this->children->removeElement($child);
  295. }
  296.  
  297. /**
  298.   * Get children
  299.   *
  300.   * @return \Doctrine\Common\Collections\Collection
  301.   */
  302. public function getChildren()
  303. {
  304. return $this->children;
  305. }
  306. ...
  307. }



Prosze o pomoc.
lukaskolista
https://github.com/Atlantic18/DoctrineExten...using-functionsW dokumentacji, którą podałeś masz przykład, podałem Ci link do konkretnej sekcji. Zamiast uzywać getChildren() na modelu użyj children() na repozytorium.
swiezak
Ok, juz dziala. Slepy jestem, niby czytalem te dokumetacje, a nie zwrocilem uwagi na wyszczegolniona przez Ciebie sekcje.


Mam jeszcze jeden problem - nie wiem jak pozbyc sie nazwy drzewa z menu kategorii.

W kontrolerze mam taki oto zapis:
  1. public function menucategoriesAction()
  2. {
  3. $em = $this->getDoctrine()->getEntityManager();
  4. $repo = $em->getRepository('MlFrontendBundle:Categories');
  5.  
  6. $options = array(
  7. 'decorate' => true,
  8. 'rootOpen' => '<ul>',
  9. 'rootClose' => '</ul>',
  10. 'childOpen' => '<li>',
  11. 'childClose' => '</li>',
  12. 'nodeDecorator' => function($node) {
  13. return '<li><a href="'.$this->getRequest()->getUriForPath('/category/'.$node['slug']).'">'.$node['name'].'</a></li>';
  14. }
  15. );
  16. $htmlTree = $repo->childrenHierarchy(
  17. null, // starting from root nodes
  18. false, // true: load all children, false: only direct
  19. $options
  20. );
  21.  
  22. return $this->render('MlFrontendBundle:Category:menucategories.html.twig', array('htmlTree' => $htmlTree));
  23. }


Do szczescie brakuje mi juz tylko ostylowac elementy listy i pozbyc sie nazwy "Kategorie". Jak tego dokonac?
lukaskolista
Jako element ROOT podaj element "Kategorie". Podając null odwołujesz się do całego drzewa, w którym możesz trzymać gałęzie (jedną z nich są kategorie jak się domyślam).
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.