教义 1.2 NestedSet 属性和关系从祖先继承

发布于 2024-11-06 09:11:44 字数 693 浏览 0 评论 0原文

我有一个 Doctrine 1.2 项目,我正在重构该项目,以便使用具有多个根的 Doctrine Beahaviour NestedSet 为表提供树结构。

我需要的是从祖先到后代的继承(不是面向对象的常识),其中后代从最近的祖先继承属性,而他们自己的属性丢失了。同样的事情也会发生在关系上。

让我用一个例子来解释:

Category:
  actAs:
    NestedSet:
      hasManyRoots: true
      rootColumnName: root_id
  columns:
    name: string(50)
    another_property: string(50)
    active: boolean
Tag:
  columns:
    value: string(50)
CategoryTag:
  columns:
    category_id: integer
    tag_id: integer

我想要执行的是:

  • 检索一个类别是否处于活动状态,这意味着验证是否所有 祖先处于活动状态
  • 如果给定类别缺少 another_property,则 ,继承自 其中存在最近祖先的
  • 给定类别的检索标签;如果标签丢失,请检索它们 来自最接近的祖先

为了最大限度地提高速度和灵活性,您建议的最佳方法是什么?

I have a Doctrine 1.2 project that I'm refactoring in order to have a tree structure for a table using doctrine beahaviour NestedSet with multiple roots.

What I need is an inheritance (Not in the OO common sense) from ancestors to descendants in which the descendants inherits properties from the closest ancestor where their own properties are missing. Same thing would happen with relations.

Let me explain with an example:

Category:
  actAs:
    NestedSet:
      hasManyRoots: true
      rootColumnName: root_id
  columns:
    name: string(50)
    another_property: string(50)
    active: boolean
Tag:
  columns:
    value: string(50)
CategoryTag:
  columns:
    category_id: integer
    tag_id: integer

What I want to perform is:

  • retrieve if a category is active, that means verifying if all the
    ancestors are active
  • if another_property is missing for a given category, inherit it from
    closest ancestor in which is present
  • retrieve tags for a given category; if tags are missing, retrieve them
    from the closest ancestor

What would you suggest as best approach in order to maximize speed and flexibility?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我家小可爱 2024-11-13 09:11:44

嗯,这太简单了。只需获取具有所需关系的树,如下所示:

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

        $q = $this->createQuery('g')
          ->leftJoin('g.Tags t')
          ->orderBy('g.root_id')
          ->addOrderBy('g.lft')
          ->where('g.root_id NOT NULL')
;
        return $q->execute(array(),  Doctrine_Core::HYDRATE_ARRAY_HIERARCHY);
      }
    }

然后,您可以像这样渲染它:

<?php function echoNode($tree, $parent=null) { ?>
  <ul>
  <?php foreach ($tree as $node): ?>
    <li data-property='<?php echo false != $node['property'] ? $node['property'] : $parent['property']  ?>'>
      <?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) ?>

注意,如果父节点不存在,如何从父节点获取“属性”。解决此问题的另一种方法是使用 Doctrine_Core::HYDRATE_RECORD_HIERARCHY。这允许在循环时调用 $nodes 上的方法。例如,您可以创建 Model::getClosestProperty() 方法,以从最接近的父级获取属性。但是,这不如阵列水合有效。

Well, this is faily simple. Just get the tree with relations that you need like this:

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

        $q = $this->createQuery('g')
          ->leftJoin('g.Tags t')
          ->orderBy('g.root_id')
          ->addOrderBy('g.lft')
          ->where('g.root_id NOT NULL')
;
        return $q->execute(array(),  Doctrine_Core::HYDRATE_ARRAY_HIERARCHY);
      }
    }

Then, you can render it like this:

<?php function echoNode($tree, $parent=null) { ?>
  <ul>
  <?php foreach ($tree as $node): ?>
    <li data-property='<?php echo false != $node['property'] ? $node['property'] : $parent['property']  ?>'>
      <?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) ?>

Notice, how you can get 'property' from parent node, if it absent. Another way to address this issue is to use Doctrine_Core::HYDRATE_RECORD_HIERARCHY. This allows use to call methods on $nodes when looping. You can create a Model::getClosestProperty() method, for example, to get the property from closest parent. But, this is not as efficient as array hydration.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文