nested-sets

Recursive CTE while Parent Id not in a List

蓝咒 提交于 2019-12-12 19:45:20
问题 I have the following Nested Set That results in this tree 1 - |---- 2 - | |---- 4 - | |---- 7 | |---- 8 |----10 - |---- 9 3 - |---- 5 |---- 6 13- |---- 11 |---- 12 I have a list of produts SELECT Id, Name ... FROM Products A many-to-many relationship with the categories. All Categories can have Promotions. Ok now the problem. Let's say I have a ProductX in the categories 7,8,6. And Promotions in the categories 1,2,3. I need get the closest parent with a promotion per category or until there

Yii nested set to dropdown menu

[亡魂溺海] 提交于 2019-12-12 11:21:27
问题 I'm using Yii nested set behavior , which helps me to keep my categories nested as seen here (nevermind title rows, they are in russian): And all I want to do is to have Bootstrap nested menu, which should be like this: $criteria = new CDbCriteria; $criteria->order = 'root, lft'; $categories = Category::model()->findAll($criteria); foreach($categories as $i => $category) { $items[$i]['label'] = $category->title; $items[$i]['url'] = $category->url; $items[$i]['active'] = false; $items[$i][

How to calculate the amount of each ingredient?

孤者浪人 提交于 2019-12-11 15:17:41
问题 I'm trying to solve a problem, where I need to calculate nutrition values of a recipe like a "My sandwich" I have a database with "recipes" and "ingredients" and the amount needed in kg. a recipe can contain ingredients but also other recipes which then again can contain recipes/ingredients. This is an example of a "part list" of a "Test Sandwich" with a total weight of 0.095 kg. array ( 0 => array ( 'id' => 2, 'name' => 'Test Cheese', 'metric_id' => 1, 'category_id' => NULL, 'quantity' => 0

How to locate a specific child of a specific level2 node in a nested set

筅森魡賤 提交于 2019-12-11 04:09:01
问题 I have a standard nested set model with each node having name, lft & rgt attributes. I can find the superiors of a specific employee using: SELECT P2.* FROM Personnel AS P1, Personnel AS P2 WHERE P1.lft BETWEEN P2.lft AND P2.rgt AND P1.name = "Bob" ORDER BY P2.lft I can also find the level2 manager by adding limit & offset: SELECT P2.* FROM Personnel AS P1, Personnel AS P2 WHERE P1.lft BETWEEN P2.lft AND P2.rgt AND P1.name = "Bob" ORDER BY P2.lft LIMIT 2 OFFSET 1 In some situations I may have

How to display a form for a subset of associated records, some of which don't exist yet?

拈花ヽ惹草 提交于 2019-12-10 13:48:16
问题 I have Tasks and Users. When a user completes a task, I create a Completion which has a field for the user to indicate how long they spent. I need a form that shows all the tasks with their completion status and time_spent attribute. On submit, completions that existed should be updated and new ones should be created. I'd like to do this in Formtastic if possible but I'll be happy with a basic Rails 3 solution. class Completion < ActiveRecord::Base belongs_to :task belongs_to :user #

Linq to get immediate children in Nested Set Model

左心房为你撑大大i 提交于 2019-12-10 10:43:31
问题 In Nested Set Model, there's a challenge for me to convert sql to linq. The above wikipedia link shows how to list up immediate children of given node as below sql syntax, and it works well when I test it with LinqPad. SELECT DISTINCT Child.Name FROM ModelTable AS Child, ModelTable AS Parent WHERE Parent.Lft < Child.Lft AND Parent.Rgt > Child.Rgt -- associate Child Nodes with ancestors GROUP BY Child.Name HAVING MAX(Parent.Lft) = 16 -- Subset for those with the given Parent Node as the

Nested sets, php array and transformation

萝らか妹 提交于 2019-12-10 08:07:19
问题 I need to transform my nested sets structure (mysql) into json for this spacetree 1) http://blog.thejit.org/wp-content/jit-1.0a/examples/spacetree.html I found this function to create an array from nested sets: 2) http://semlabs.co.uk/journal/converting-nested-set-model-data-in-to-multi-dimensional-arrays-in-php I can also convert php array into json with PHP function json_encode My problem: the function nestify (from second link) gives me not exactly that i need. I need something like this:

Transaction safe insertion of node in nested set?

旧时模样 提交于 2019-12-08 07:33:09
问题 I am storing hierarchical data in mysql in the form of a nested set. myTable id, title, lft, rgt I use the following series of sql statements to insert a new node: SELECT @myLeft := lft FROM myTable WHERE ID = $id; UPDATE myTable SET rgt = rgt + 2 WHERE rgt > @myLeft; UPDATE myTable SET lft = lft + 2 WHERE lft > @myLeft; INSERT INTO myTable(title, lft, rgt) VALUES($title, @myLeft + 1, @myLeft + 2); This works, but there are potentially problems if a lot of nodes get added (near)

How to reflect “Nested Sets” model in JPA

夙愿已清 提交于 2019-12-08 00:46:38
问题 It is good to use Nested sets for the hierarchical data. But in this design you should calculate always the right and the left nodes if you delete or insert some data. Additionally you don't have any foreignkeys. How can i reflect this model with JPA?? Is it possible? 回答1: I think you would still want the PARENT_ID even with the RIGHT and LEFT, otherwise the parent/child queries would be inefficient, or I'm not even sure possible. The RIGHT and LEFT values you could update in your application

PHP: Sort an array

随声附和 提交于 2019-12-07 11:10:54
问题 I've got an array with data from a MySQL table in nested set model I'd like to get sorted, not only alphabetical but also with the child nodes directly after the parent node. Example - array to be sorted (before the sorting): Array ( [0] => Array ( [id] => 1 [name] => Kompetenser [parent] => 0 [depth] => 0 ) [1] => Array ( [id] => 2 [name] => Administration [parent] => 1 [depth] => 1 ) [2] => Array ( [id] => 11 [name] => Organisation [parent] => 2 [depth] => 2 ) [3] => Array ( [id] => 4 [name