Elixir Ecto: 使用Arbor在Postgresql中实现邻接列表与树的遍历
发布时间:2020-12-13 17:01:11 所属栏目:百科 来源:网络整理
导读:Arbor 项目地址 什么是CTEs? 使用CTEs实现Ecto的邻接列表与树的遍历. Arbor 使用 parent_id 和 CTEs 创建简单的树状结构. 使用 defmodule Comment do use Ecto.Schema # See config options below use Arbor.Tree,foreign_key_type: :binary_id schema "comm
使用defmodule Comment do use Ecto.Schema # See config options below use Arbor.Tree,foreign_key_type: :binary_id schema "comments" do field :body,:string belongs_to :parent,Arbor.Comment timestamps end end 获取根级节点roots = Comment.roots |> Repo.all 获取兄弟节点siblings = my_comment |> Comment.siblings |> Comment.order_by_popularity |> Repo.all 获取祖先节点获取当前节点的先辈节点,从下网上返回所有父辈节点. 主要用于显示一个分类导航路径(类似网站的 descendants = my_comment |> Comment.ancestors |> Comment.order_by_inserted_at |> Repo.all 获取后代节点获取当前节点的所有后代节点,返回后代节点列表 descendants = my_comment |> Comment.descendants |> Comment.order_by_inserted_at |> Repo.all 获取子节点获取当前节点的所有子节点 children = my_comment |> Comment.children |> Repo.all 获取父节点获取当前节点的父节点 parent = my_comment |> Comment.parent |> Repo.first 选项
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- Cocos开发VR菜鸟宝典 第八讲:为GearVR开发一款深海捕鱼VR游
- 解决jenkins构建job报错“NoClassDefFoundError” in jenki
- ios – NSPredicate’ALL或ANY运算符的左侧必须是NSArray或
- C语言中sscanf()函数的字符串格式化用法
- 在Ruby中的另一个类中调用一个类中的方法
- ajaxSubmit提交
- ruby-on-rails – 如何访问rails action mailer中的current
- stm32f103 Flash 读写
- ruby-on-rails – Rails Paperclip,多种不同类型(PDF,Image
- 部分Dojo常用函数简介