0

ノードからすべての祖先を取得しようとしているときに少し問題が発生しました。

これは私の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を反復してすべての先祖を取得することはできないため (単純なブレッドクラムを作成しようとしています)

これは私が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
+---------++---------++---------++---------++----------++---------+

これによると、Ancestors が false を返した場合、選択したノードがルートであることを意味します。

運の悪い解決策を探すのに何時間も費やしました。

さらに詳しい情報が必要な場合は、遠慮なくお尋ねください。

編集:表にあるものを入力するときに間違いを犯しました.olivierwのおかげで、これについて警告してくれました.

4

2 に答える 2

0

rgtテーブルのフィールドにエラーがあるようです。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   |
+---------++---------++---------++---------++----------++---------+

したがって、祖先を正しく取得できます。

于 2012-07-07T08:03:31.947 に答える
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>';
}
?>

コードは一目瞭然だと思いますが、質問があれば教えてください!

于 2012-07-07T16:45:39.107 に答える