簡體   English   中英

使用Doctrine 1.2和Symfony 1.4獲取節點祖先

[英]Getting node ancestors using Doctrine 1.2 and 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

這就是我試圖從節點(不是根)獲取所有祖先的方式

$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

由於$ancestors == false我無法遍歷並獲得所有祖先(我正在嘗試構建一個簡單的面包屑)

這是我存儲在數據庫中的數據,是真實數據(僅用於測試puporse)

+---------++---------++---------++---------++----------++---------+
|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
+---------++---------++---------++---------++----------++---------+

據此,如果祖先返回false,則表示所選節點是根。

我花了幾個小時尋找沒有運氣的解決方案。

如果您需要更多信息,請隨時詢問!

編輯:鍵入表中的內容時,我犯了一個錯誤,這要感謝olivierw提醒我。

表格中的rgt欄位似乎有錯誤。 如果id 56是根,則id 56應該為rgt = 4id 57應該為rgt = 3 因此,您的表應顯示為:

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

因此,您將正確地獲得祖先。

我想分享我的解決方案,希望對其他人有用:

行動:

//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>';
}
?>

我認為代碼是不言自明的,但是如果您有任何疑問,請告訴我!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM