Best solutions for tree in doctrine with extension - doctrine-orm

I am finding Best solutions for tree in doctrine with extension.
I need tree:
in one table
with multiple roots
add onne root item to another root item
tree items need order (weight) also roots
create list with one select and without recursion
I try from this extension: https://github.com/Atlantic18/DoctrineExtensions/blob/v2.4.x/doc/tree.md
nested
- I can not order root items
closure
- not good idea , need 2 tables
materializedPath
- maybe bes way but do not have order (weight)
Does anyone have experience how to solve it?

Related

Porting and existing nested set hierarchy

I have a nested set category table developed with the PHP ORM Doctrine and I would like to port it to a Django app.
I started porting it to django-treebeard, but I am having difficulties and I am not sure it can work.
The original table had the needed fields lft, rgt and depth, so I added the tree_id field.
I also had a foreign key to Accounts with one tree/account. Thus the table hold multiple independent trees that are not under a common root, with the lft and depth columns starting at 1 for each tree. So basically one nested set for each account in the table.
I can add nodes to a tree just fine, but when I call the get_last_child method, I get nodes from other accounts.
Does anyone know if there is a way to use treebeard, mptt or any other package without having to restructure the trees?
I made some progress by adding the correct tree_id as a sequential number by account_id, which fixed some of the issues, with the query:
UPDATE category c,
(SELECT id, DENSE_RANK() OVER (ORDER BY account_id) AS seq
FROM category ) tree_rank
SET c.tree_id = tree_rank.seq
WHERE c.id = tree_rank.id;
Now trying to get the admin to work.

How to add multiple nodes to relations

How to add multiple node to relations here is my query
MATCH (n:Customer{name:"motoM"})-[:RECENT {default:TRUE}]-(l:Location{name:"Adugodi"}) return l how to write a query to add one more "location" node to the relation "recent" if location node is not found and setting default to true to newly created realtion
What about this?
MATCH (n:Customer{name:"motoM"})-[:RECENT {default:TRUE}]-(l:Location{name:"Adugodi"})
MERGE (n)-[:RECENT]->(l2:Location)
ON CREATE SET l2.default = true
RETURN l, l2
The direction needs to be specified so I made it up, but it might need to go the other way.
Well, I don't know if I understood what you were looking for, but this might help you :)
Try with this query:
MATCH (n:Customer{name:"motoM"})-[r:RECENT {default:TRUE}]-(:Location{name:"Adugodi"})
CREATE (l2:Location{name:"Wherever You need"})
With r,n,l,l2
Set r.default = false
With n,l2
CREATE (n)-[r2:RECENT{default:TRUE}]->(l2)
I'm using Withto make the query easier to read, but you can do it in a single query.
In fact, I think your problem is your Graph model.
You should probably do something like a Customer node, related to Location nodes with a "VISITED" relation, and when you create your VISITED relation, you set date property to timestamp. Then, when you get your relations, you can simply compare timestamps to get the closest one, and you know which one is the one your need. Also, if you need a default property, set it on the node, it'll be easier to match.
Tell me if you need a code example for match, create and set data with this graph model.

Doctrine Extensions - Tree - Multiple roots

We heavily use the tree doctrine extension in our zf2 project - with some big tree data structures. We know that inserts and updates in a nested set are expensive. We also know that the tree plugin uses the "root" column to find out which tree shall be updated.
Yesterday I read the tree documentation again and found:
"Support for multiple roots in nested-set"
What does it mean and how does it work? I couldn't find any documentation for this feature.
Our hope would be that we could define a second root item of a lower branch of a big tree so that inserts and updates into this lower branch will not affect the whole tree but only this branch. Is it possible?
yes it is possible, tree root branches will be separated by level 0 nodes, see mapping example of TreeRoot column there should be examples for all mapping types to map treeRoot column. The column must be of the same type as ID, it does not support a ManyToOne relation for now, but there is a plan someday to support it.
root1
child
root2
child
child2
When updating or inserting any child on root2 or root1 branch, it will affect only that certain branch. Also note that, tree is still not concurrently safe, you have to manage locking yourself, see documentation reference here.
The doc directory contains most of the information given here.

Prevent document being indexed in DSE search

I am using DSE search to build index of some Cassandra table and I want DSE search not to index a document if some boolean field is true.
I can achieve the same result by filtering during query time but as the number of documents containing true could be large, preventing them from being indexed may provide better query performance.
What is the best way to achieve this goal in DSE search/Solr?
Thanks
One suggestion would be to split up the docs you do want to index into a separate table and not index your main table.

Django: How to create ordered siblings

I want to create a model that will order its children models in the appropriate way. For instance, a Book has many Chapters, but the Chapters have to be in a specific order.
I assume that I need to put an IntegerField on the Chapter model that specifies the order of the Chapters like the following question suggests: Ordered lists in django
My main issue is that whenever I want to insert a new Chapter in between two existing chapters or reorder them in any way, I have to update (almost) every Chapter in the Book. Is there a way (perhaps in the Django Admin, which I'm using) to avoid having to manually change every index on every Chapter whenever I change the order?
I'm not a big fan of creating a "Linked List" style model, as proposed in the above-linked question, as I am under the impression that's not good practice for database creation.
What is the "right" way to model this relationship?
The answer you alluded to was probably the best way to handle this efficiently. Probably requiring a raw SQL statement UPDATE Chapter SET order = order + 1 WHERE book_id = <id_for_book> AND order <= <insert_index_location>. For Django 1.1+: You could use F() to write this in a single line as the following, but it might still be O(n) queries under the hood, using transactions.
Book.objects.get(id=<id_of_book>).chapter_set.filter(order__gt=<place_to_insert>).update(order=F('order')+1)
Use a float instead of an integer to avoid your problem of updating multiple items when you insert between two.
So if you want to insert an item between item 42 and item 43, you can give it an order value halfway between the two (42.5), and you won't have to update any other items.
Insert z between x and y...
z.order = (y.order - x.order) / 2 + x.order