2012-07-06 4 views
0

노드에서 모든 조상을 가져 오는 동안 약간의 문제가 있습니다. Doctrine 1.2 및 Symfony 1.4를 사용하여 노드 조상 얻기

내 schema.yml 파일입니다 :

Constante: 
connection: doctrine 
tableName: constante 
actAs: 
NestedSet: 
    hasManyRoots: true 
    rootColumnName: parent_id 
columns: 
id: 
    type: integer(8) 
    fixed: false 
    unsigned: false 
    primary: true 
    autoincrement: true 
parent_id: 
    type: integer(8) 
    fixed: false 
    unsigned: false 
    primary: false 
    notnull: true 
    autoincrement: false 
lft: 
    type: integer(8) 
    fixed: false 
    unsigned: false 
    primary: false 
    notnull: true 
    autoincrement: false 
rgt: 
    type: integer(8) 
    fixed: false 
    unsigned: false 
    primary: false 
    notnull: true 
    autoincrement: false 
level: 
    type: integer(8) 
    fixed: false 
    unsigned: false 
    primary: false 
    notnull: true 
    autoincrement: false 
cod_interno: 
    type: string(5) 
    fixed: false 
    unsigned: false 
    primary: false 
    notnull: false 
    autoincrement: false 
nombre: 
    type: string(64) 
    fixed: false 
    unsigned: false 
    primary: false 
    notnull: true 
    autoincrement: false 

그리고 이것은 내가 $ancestors == false I로

$path  = Doctrine_Core::getTable('Constante')->find($condicion); // $condicion = 57 
$node  = $path->getNode(); 
$isLeaf  = $node->isLeaf(); //var_dump throws true 
$ancestors = $node->getAncestors(); //var_dump throws false 
$isValidNode = $node->isValidNode(); //var_dump throws true 

(루트가 아닌 한) 노드의 모든 조상을 얻으려고하는 방법이다 그것을 반복하고 조상을 얻지 못한다. (단순한 빵 부스러기를 만들려고 노력하고있다.)

이것이 DB에 저장되어있다. 이것은 실제 데이터이다. (전용 테스트 용)

+---------++---------++---------++---------++----------++---------+ 
|ID  ||PARENT_ID||LFT  ||RGT  ||LEVEL  ||NOMBRE | 
|---------||---------||---------||---------||----------||---------| 
|56  ||56  ||1  ||4  ||0   ||COUNTRY | --> this is root 
|57  ||56  ||2  ||3  ||1   ||CANADA | --> child of root 
+---------++---------++---------++---------++----------++---------+ 

According to this Ancestors가 false를 반환하면 선택된 노드가 루트임을 의미합니다.

나는 운이없는 해결책을 찾기 위해 몇 시간을 보냈습니다.

추가 정보가 필요하면 언제든지 문의하십시오.

편집 : 테이블에 무엇을 입력 할 때 실수를했습니다. olivierw에게 감사드립니다.

답변

0

내가 잘하면 다른 사람을 위해 도움이 될 것입니다, 내 솔루션을 공유하려는 :

작업 :

//action.class.php 
public function executeIndex(sfWebRequest $request) { 

$condicion = $request->getParameter('id') ? $request->getParameter('id') : 0; 

if ($condicion > 0) { 
    $path = $tree = Doctrine_Core::getTable('Constante')->find($condicion); 
} else { 
    $tree = Doctrine_Core::getTable('Constante'); 
    $path = null; 
} 

$this->constantes = $tree; 
$this->path = $path; 
$this->setTemplate('index'); 

} 

보기 :

//indexSuccess.php 
<?php 
$var = (is_null($sf_request->getParameter('id'))) ? $constantes->getTree()->fetchRoots() : $constantes->getNode()->getChildren(); 
?> 

<?php if ($var): foreach ($var as $constante): ?> 
    <tr> 
     <td><?php echo $constante->getNombre() ?></td> 
    </tr> 
    <?php 
    endforeach; 
endif; 
?> 

부분 :

//_breadcrumb.php 
<?php 

if ($path) { 

    echo '<ul class="breadcrumb">'; 

    $node = $path->getNode(); 
    $ancestors = $node->getAncestors(); 

    if ($ancestors) 
    foreach ($ancestors AS $ancestor) { 
     echo '<li>' . link_to($ancestor->getNombre(), 'constantes/more?id=' . $ancestor->getId()) . '<span class="divider">/</span></li>'; 
    } 

    echo '</ul>'; 
} 
?> 

내가 생각하기에 t 그는 코드가 자명하지만, 질문이 있으면 알려 주시기 바랍니다!

+0

좋아요! 하지만 왜'$ node-> getAncestors()'가 지금 작동하고 질문에서'false'를 반환하지 않는지 이해할 수 없습니다. –

+0

안녕하세요. 나는이 함수를 처음부터 다시 작성했다. 이전 함수의 문제는 잘못된 명령문을 가진 setBaseQuery라고 생각한다. 나는 이것이 우아한 방법이라고 생각한다. –

+0

'setBaseQuery'? 확실 해요! 이것은 우아한 방법입니다! –

0

테이블의 rgt 입력란에 오류가있는 것 같습니다. id 56이 루트이면 rgt = 4이고 id 57rgt = 3이어야합니다. 따라서 테이블은 다음과 같이 읽어야합니다.

+---------++---------++---------++---------++----------++---------+ 
|ID  ||PARENT_ID||LFT  ||RGT  ||LEVEL  ||NOMBRE | 
|---------||---------||---------||---------||----------||---------| 
|56  ||56  ||1  ||4  ||0   ||COUNTRY | 
|57  ||56  ||2  ||3  ||1   ||CANADA | 
+---------++---------++---------++---------++----------++---------+ 

그러면 조상이 올바르게 나타납니다.

+0

안녕하세요, 내 솔루션을 게시했습니다. 확인해주세요. 고마워요 –