2016-05-13 18 views
0

我得到錯誤:Gedmo樹的getPath錯誤:節點是不相關的這個倉庫500內部服務器錯誤 - InvalidArgumentException

Node is not related to this repository 
500 Internal Server Error - InvalidArgumentException 

更新1:如果我成立了樹repository with traitsextend abstract repository錯誤不要緊是一樣的。

更新2:全堆棧跟蹤http://pastebin.com/TtaJnyzf

我想從數據庫樹狀結構輸出HTML樹,特別我需要去選擇的節點從根路徑。據我所知,這是用getPath()函數完成的。

我使用:

  • 的Symfony v3.0.6;
  • 學說v2.5.4
  • StofDoctrineExtensionsBundle [1]

以便管理樹結構。

要設置樹結構我用[2]接着文檔在GitHub上Symfony.com文檔[3],[4],[5],[6]。

到目前爲止,我有在數據庫中的樹形結構和我得到的HTML樹是這樣的:

<?php 

namespace AppBundle\Controller; 

use AppBundle\Entity\Category; 
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; 
use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Symfony\Component\HttpFoundation\Request; 

class TreeController extends Controller 
{ 
    /** 
    * @Route("/tree", name="tree") 
    */ 
    public function treeAction(Request $request) 
    { 
     $em = $this->getDoctrine()->getManager(); 

     $repo = $em->getRepository('AppBundle:Category'); 
     $options = array(
      'decorate' => true, 
      'rootOpen' => '<ul>', 
      'rootClose' => '</ul>', 
      'childOpen' => '<li>', 
      'childClose' => '</li>', 
      nodeDecorator' => function($node) 
      { 
       return '<a href="/some_path/...">'. $node['title'] .'</a>'; 
      } 
     ); 

     $htmlTree = $repo->childrenHierarchy(
      null, /* starting from root nodes */ 
      false, /* false: load all children, true: only direct */ 
      $options 
     ); 

     return $this->render('tree/tree_show.html.twig', array('project_tree' => $htmlTree)); 
    } 
} 

我,以便從一個樹元素的根顯示所選項目

路徑改變兩行
nodeDecorator' => function($node) use ($repo) 
{ 
    return '<a href="/project_path/'. implode('/', $repo->getPath($node)) .'">'. $node['title'] .'</a>'; 
} 

如[7]和[8]的函數的getPath看出(),其應該從根返回元件的陣列,以所選擇的項目的存在。

我認爲這個問題可能在於在此代碼塊:

$repo->getPath($node) 

請指教。 謝謝你的時間和知識。

+0

你可以顯示完整的堆棧跟蹤嗎? – xabbuh

+0

@xabbuh我用堆棧跟蹤更新了我的問題 – Rikijs

回答

0

得到它的工作!

這裏有必要的修改:

代替

nodeDecorator' => function($node) use ($repo) 
{ 
    return '<a href="/project_path/'. implode('/', $repo->getPath($node)) .'">'. $node['title'] .'</a>'; 
} 

應該寫

'nodeDecorator' => function($node) use ($repo) 
{ 
    return '<a href="/project_path/'. @implode('/', $repo->getPath($repo->findOneBy(array('id' => $node['id'])))) .'">'. $node['title'] .'</a>'; 
} 

和類別類添加

public function __toString() 
{ 
    return $this->getTitle(); 
} 

就是它,路徑到每個號碼現在應該顯示。