[英]Getting node ancestors using Doctrine 1.2 and Symfony 1.4
I've a little trouble while trying to get all ancestors from a node; 尝试从节点中获取所有祖先时遇到了一些麻烦。
This is my schema.yml: 这是我的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
And this is how I'm trying to get all ancestors from a node (which is not a root) 这就是我试图从节点(不是根)获取所有祖先的方式
$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
As $ancestors == false
I cannot iterate over it and get all ancestors (I'm trying to build a simple breadcrumb) 由于$ancestors == false
我无法遍历并获得所有祖先(我正在尝试构建一个简单的面包屑)
This is what I have stored in DB, this is real data (for testing puporse only) 这是我存储在数据库中的数据,是真实数据(仅用于测试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
+---------++---------++---------++---------++----------++---------+
According to this if Ancestors returns false, it means that the selected node is a root. 据此,如果祖先返回false,则表示所选节点是根。
I've spent hours looking for a solution with no luck. 我花了几个小时寻找没有运气的解决方案。
If you need further information, don't hesitate to ask for it! 如果您需要更多信息,请随时询问!
EDIT: I've made a mistake when typing what is in the table, thanks to olivierw by alert me about this. 编辑:键入表中的内容时,我犯了一个错误,这要感谢olivierw提醒我。
It seems you have an error in the rgt
field in your table. 表格中的rgt
栏位似乎有错误。 If id 56
is the root, it should have rgt = 4
and id 57
should have rgt = 3
. 如果id 56
是根,则id 56
应该为rgt = 4
而id 57
应该为rgt = 3
。 So your table should read: 因此,您的表应显示为:
+---------++---------++---------++---------++----------++---------+
|ID ||PARENT_ID||LFT ||RGT ||LEVEL ||NOMBRE |
|---------||---------||---------||---------||----------||---------|
|56 ||56 ||1 ||4 ||0 ||COUNTRY |
|57 ||56 ||2 ||3 ||1 ||CANADA |
+---------++---------++---------++---------++----------++---------+
So you will get the ancestors correctly. 因此,您将正确地获得祖先。
I want to share my solution, hopefully it will be useful for somebody else: 我想分享我的解决方案,希望对其他人有用:
Action: 行动:
//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');
}
View: 视图:
//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;
?>
Partial: 部分:
//_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>';
}
?>
I think that the code is self-explanatory, but if you have any question, please let me know! 我认为代码是不言自明的,但是如果您有任何疑问,请告诉我!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.