Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [SF2][SF] Menu - Drzewo kategorii w oparciu o tree nested
swiezak
post 13.01.2016, 00:48:17
Post #1





Grupa: Zarejestrowani
Postów: 159
Pomógł: 0
Dołączył: 21.08.2011

Ostrzeżenie: (0%)
-----


Witam.
Mam problem z uzyskaniem menu w formie drzewa. Korzystam z Tree nested i otrzymuję kod błędu: "Error: Call to a member function childrenHierarchy() on a non-object".
Dopiero zaczynam przygode z SF2, dlatego prosze o wyrozumialosc.

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



Categories.php
  1. namespace Ml\FrontendBundle\Entity;
  2.  
  3. use Gedmo\Sluggable\Sluggable;
  4. use Gedmo\Mapping\Annotation as Gedmo;
  5. use Symfony\Component\Validator\Constraints as Assert;
  6.  
  7. use Doctrine\ORM\Mapping as ORM;
  8.  
  9. /**
  10.  * Categories
  11.  *
  12.  * @Gedmo\Tree(type="nested")
  13.  * @ORM\Table(name="categories")
  14.  * @ORM\Entity(repositoryClass="Gedmo\Tree\Entity\Repository\NestedTreeRepository")
  15.  */
  16. class Categories
  17. {
  18. /**
  19.   * @var integer
  20.   * @ORM\Column(name="id", type="integer")
  21.   * @ORM\Id
  22.   * @ORM\GeneratedValue(strategy="AUTO")
  23.   */
  24. private $id;
  25.  
  26. /**
  27.   * @var string
  28.   *
  29.   * @Assert\NotBlank()
  30.   * @ORM\Column(name="name", type="string", length=255)
  31.   */
  32. private $name;
  33.  
  34. /**
  35. * @var string $slug
  36. *
  37. * @Gedmo\Slug(fields={"name"})
  38. * @ORM\Column(length=255, unique=true)
  39. */
  40. private $slug;
  41.  
  42. /**
  43.   * @Gedmo\TreeLeft
  44.   * @ORM\Column(name="lft", type="integer")
  45.   */
  46. private $lft;
  47.  
  48. /**
  49.   * @Gedmo\TreeLevel
  50.   * @ORM\Column(name="lvl", type="integer")
  51.   */
  52. private $lvl;
  53.  
  54. /**
  55.   * @Gedmo\TreeRight
  56.   * @ORM\Column(name="rgt", type="integer")
  57.   */
  58. private $rgt;
  59.  
  60. /**
  61.   * @Gedmo\TreeRoot
  62.   * @ORM\Column(name="root", type="integer", nullable=true)
  63.   */
  64. private $root;
  65.  
  66. /**
  67.   * @Gedmo\TreeParent
  68.   * @ORM\ManyToOne(targetEntity="Categories", inversedBy="children")
  69.   * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE")
  70.   */
  71. private $parent;
  72.  
  73. /**
  74.   * @ORM\OneToMany(targetEntity="Categories", mappedBy="parent")
  75.   * @ORM\OrderBy({"lft" = "ASC"})
  76.   */
  77. private $children;
  78.  
  79. ...
  80. }


W plikach konfiguracyjnych mam odpowiednie wpisy, a mimo to nie dziala.


Czy ktos z Was mial stycznosc z tree nested i mi pomoze?
Go to the top of the page
+Quote Post
Forti
post 13.01.2016, 10:38:38
Post #2





Grupa: Zarejestrowani
Postów: 655
Pomógł: 73
Dołączył: 2.05.2014

Ostrzeżenie: (0%)
-----


W config.yml masz włączone tree? W bazie zapisuje prawidłowo lft, rgt, root i parent?

Czy findAll() zwraca prawidłowo zmapowane rekordy? czy są to obiekty Gedmo tree?

Spróbuj zrobić custom query zamiast findAll()
http://stackoverflow.com/questions/2850252...ns-gedmo-object


I repozytorium zrób własne z extends na Gedmo\Tree\Entity\Repository\NestedTreeRepository


--------------------
Overwatch24 - najbardziej zaawansowany Polski portal Overwatch od fanów dla fanów.

Fachowo.co

Behance.net/fachowo
Go to the top of the page
+Quote Post
swiezak
post 14.01.2016, 11:33:53
Post #3





Grupa: Zarejestrowani
Postów: 159
Pomógł: 0
Dołączył: 21.08.2011

Ostrzeżenie: (0%)
-----


W pliku config.yml mam m.in. takie wpisy:
  1. doctrine:
  2. orm:
  3. auto_generate_proxy_classes: "%kernel.debug%"
  4. naming_strategy: doctrine.orm.naming_strategy.underscore
  5. auto_mapping: true
  6.  
  7. mappings:
  8. # gedmo_translatable:
  9. # type: annotation
  10. # prefix: Gedmo\Translatable\Entity
  11. # dir: "%kernel.root_dir%/../vendor/gedmo/doctrine-extensions/lib/Gedmo/Translatable/Entity"
  12. # alias: GedmoTranslatable # this one is optional and will default to the name set for the mapping
  13. # is_bundle: false
  14. gedmo_translator:
  15. type: annotation
  16. prefix: Gedmo\Translator\Entity
  17. dir: "%kernel.root_dir%/../vendor/gedmo/doctrine-extensions/lib/Gedmo/Translator/Entity"
  18. alias: GedmoTranslator # this one is optional and will default to the name set for the mapping
  19. is_bundle: false
  20. # gedmo_loggable:
  21. # type: annotation
  22. # prefix: Gedmo\Loggable\Entity
  23. # dir: "%kernel.root_dir%/../vendor/gedmo/doctrine-extensions/lib/Gedmo/Loggable/Entity"
  24. # alias: GedmoLoggable # this one is optional and will default to the name set for the mapping
  25. # is_bundle: false
  26. gedmo_tree:
  27. type: annotation
  28. prefix: Gedmo\Tree\Entity
  29. dir: "%kernel.root_dir%/../vendor/gedmo/doctrine-extensions/lib/Gedmo/Tree/Entity"
  30. alias: GedmoTree # this one is optional and will default to the name set for the mapping
  31. is_bundle: false
  32. gedmo_sortable:
  33. type: annotation
  34. prefix: Gedmo\Sortable\Entity
  35. dir: "%kernel.root_dir%/../vendor/gedmo/doctrine-extensions/lib/Gedmo/Sortable/Entity"
  36. alias: GedmoSortable # this one is optional and will default to the name set for the mapping
  37. is_bundle: false
  38.  
  39. stof_doctrine_extensions:
  40. default_locale: pl
  41. translation_fallback: true
  42. orm:
  43. default:
  44. tree: true
  45. loggable: false
  46. timestampable: true
  47. sluggable: true
  48. translatable: false
  49. sortable: true


W pliku CategoriesRepository.php mam wpis następujący:
  1. namespace Ml\FrontendBundle\Entity;
  2.  
  3. use Doctrine\ORM\EntityRepository;
  4. use Doctrine\ORM\Tools\Pagination\Paginator;
  5.  
  6. use Gedmo\Tree\Entity\Repository\NestedTreeRepository;
  7.  
  8. /**
  9.  * CategoriesRepository
  10.  */
  11.  
  12.  
  13. class CategoriesRepository extends \Doctrine\ORM\EntityRepository
  14. {
  15. ...
  16. }


W bazie danych poprawnie zapisują się root, parent_id, lft, rgt, lvl, natomiast problem jest przy pobieraniu struktury drzewiastej przy użyciu Gedmo tree.
Go to the top of the page
+Quote Post
Riggs
post 18.01.2016, 07:19:40
Post #4





Grupa: Zarejestrowani
Postów: 162
Pomógł: 13
Dołączył: 16.06.2007

Ostrzeżenie: (0%)
-----


Źle dziedziczysz , zamiast
  1. class CategoriesRepository extends \Doctrine\ORM\EntityRepository
  2. {
  3. ...
  4. }

zrób
  1. class CategoriesRepository extends NestedTreeRepository
  2. {
  3. ...
  4. }
Go to the top of the page
+Quote Post
swiezak
post 18.01.2016, 13:21:55
Post #5





Grupa: Zarejestrowani
Postów: 159
Pomógł: 0
Dołączył: 21.08.2011

Ostrzeżenie: (0%)
-----


Riggs, ta zmiana, ktora zasugerowales w klasie repozytorium nie dala oczekiwanych rezultatow. Problem istnieje nadal.
Go to the top of the page
+Quote Post
Riggs
post 19.01.2016, 08:18:36
Post #6





Grupa: Zarejestrowani
Postów: 162
Pomógł: 13
Dołączył: 16.06.2007

Ostrzeżenie: (0%)
-----


Nie doczytałem dobrze błędu.
  1. $repo = $em->getRepository('MlFrontendBundle:Categories')->findAll();
to daje ci wszystkie rekordy z tabeli Categories a nie repozytorium! Zamień to na
  1. $repo = $em->getRepository('MlFrontendBundle:Categories');
, reszta bez zmian i powinno być ok.
Go to the top of the page
+Quote Post
swiezak
post 19.01.2016, 10:11:58
Post #7





Grupa: Zarejestrowani
Postów: 159
Pomógł: 0
Dołączył: 21.08.2011

Ostrzeżenie: (0%)
-----


Riggs, dzieki wielkie za pomoc. Popelnilem kilka bledow, ktore uniemozliwily wczytanie drzewa kategorii.

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


Zawartość pliku CategoriesRepository.php
  1. namespace Ml\FrontendBundle\Entity;
  2.  
  3. use Doctrine\ORM\EntityRepository;
  4. use Doctrine\ORM\Tools\Pagination\Paginator;
  5. use Gedmo\Tree\Entity\Repository\NestedTreeRepository;
  6.  
  7. class CategoriesRepository extends NestedTreeRepository
  8. {
  9.  
  10. use NestedTreeRepositoryTrait;
  11.  
  12. public function __construct(EntityManager $em, ClassMetadata $class)
  13. {
  14. parent::__construct($em, $class);
  15.  
  16. $this->initializeTreeRepository($em, $class);
  17. }
  18.  
  19. }


Juz wszystko dziala.


Pozdrawiam
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 17.05.2024 - 20:38