2011-05-13 10 views
0

현재 Doctrine의 nestedSet에서 네비게이션 메뉴의 관점에서 계층 구조를 출력하고 있습니다.nestedSet - 부모/자식 노드

나는 여러 명의 부모를두고 몇 명의 자녀를두고 있습니다.

현재로서는 부모와 자녀 (손자 없음)의 2 단계 만 있습니다. 이 밖으로 렌더링

//actions: 
public function executeShow(sfWebRequest $request) 
{ 
    $this->tree = Doctrine::getTable('Model')->getMenuTree(); 
} 

//lib: 
class ModelTable extends Doctrine_Table 
{ 
    /** 
    * Gets tree element in one query 
    */ 
    public function getMenuTree() 
    { 

    $q = $this->createQuery('g') 
     ->orderBy('g.root_id') 
     ->addOrderBy('g.lft') 
     ->where('g.root_id NOT NULL'); 

    return $q->execute(array(), Doctrine_Core::HYDRATE_ARRAY_HIERARCHY); 
    } 
} 


//template: 
<?php function echoNode($tree, $parent=null) { ?> 
    <ul> 
    <?php foreach ($tree as $node): ?> 
    <li data-id='<?php echo $node['id'] ?>'> 
     <?php echo $node['name'] ?> 
     <?php if (count($node['__children']) > 0): ?> 
     <?php echo echoNode($node['__children'], $node) ?> 
     <?php endif; ?> 
    </li> 
    <?php endforeach; ?>  
    </ul> 
<?php } ?> 
<?php echo echoNode($tree) ?> 

: 대단한

Parent Node 1 
    Child Node 1 
    Child Node 2 
Parent Node 2 
    Child Node 3 

나는 다음과 같은 코드가 있습니다.

문제는 URL이 부모/자녀 관계와 일치하도록하는 것입니다.

따라서 하위 노드 2에 대한 URL은 /parent-node-1/child-node-2 (이 필드는 slug 개임)입니다.

부모의 자식에게는 부모 노드의 경로 슬러그도 있어야합니다.

나는 그것이 의미가 있기를 바랍니다.

감사

답변

1

음,이 hadle하는 한 가지 방법은 당신이 당신의 노드에서 메소드를 호출 할 수 있습니다 Doctrine_Core::HYDRATE_RECORD_HIERARCHY 수화를 사용하는 것입니다.

class Model extends BaseModel 
{ 
    public function __toString() 
    { 
    return $this->getSlug(); 
    } 
    public function getUrl() 
    { 
    //getPath uses __toString method to render a node 
    $url = $this->getNode()->getPath('/', true); 
    return $url; 
    } 
} 

을 그리고 다음과 같이 템플릿에 전화 :

이제 노드에 대한 사용자 정의 메소드를 만들 수 있습니다

<a href="<?php echo $node->getUrl() ?>"><?php echo $node['name'] ?></a> 
+0

이 prefectly 일 그! 지금 nestSet 동작을 이해하고 있다고 생각하십시오! –

+0

여기서 주목해야 할 것은 성능입니다. getPath()를 호출 할 때마다 db에 대한 별도의 쿼리가 생성됩니다. 다음은 하나의 쿼리를 사용하는 대체 솔루션입니다. http://stackoverflow.com/questions/6009163/breadcrumb-navigation-with-doctrine-nestedset/6010461#6010461 (내 대답 참조) – Dziamid

+0

그래, 위의 솔루션은 몇 가지 쿼리를 만드는 것처럼 보입니다. 하나는 훨씬 더 좋을 것입니다! 나는 당신의 예를 보겠습니다. –

관련 문제