{"action":"created","issue":{"url":"https://api.github.com/repos/Atlantic18/DoctrineExtensions/issues/1481","labels_url":"https://api.github.com/repos/Atlantic18/DoctrineExtensions/issues/1481/labels{/name}","comments_url":"https://api.github.com/repos/Atlantic18/DoctrineExtensions/issues/1481/comments","events_url":"https://api.github.com/repos/Atlantic18/DoctrineExtensions/issues/1481/events","html_url":"https://github.com/Atlantic18/DoctrineExtensions/issues/1481","id":121820802,"number":1481,"title":"[Tree] persistAs*SiblingOf without TreeRoot and no parent saves as last sibling anyway","user":{"login":"Ymox","id":1888653,"avatar_url":"https://avatars.githubusercontent.com/u/1888653?v=3","gravatar_id":"","url":"https://api.github.com/users/Ymox","html_url":"https://github.com/Ymox","followers_url":"https://api.github.com/users/Ymox/followers","following_url":"https://api.github.com/users/Ymox/following{/other_user}","gists_url":"https://api.github.com/users/Ymox/gists{/gist_id}","starred_url":"https://api.github.com/users/Ymox/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Ymox/subscriptions","organizations_url":"https://api.github.com/users/Ymox/orgs","repos_url":"https://api.github.com/users/Ymox/repos","events_url":"https://api.github.com/users/Ymox/events{/privacy}","received_events_url":"https://api.github.com/users/Ymox/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":7,"created_at":"2015-12-12T00:47:09Z","updated_at":"2015-12-15T11:57:50Z","closed_at":null,"body":"Hello everyone !\r\n\r\nIn my Symfony project, I have an entity that uses the NestedTree strategy, without `@Gedmo\\TreeRoot`, to have a list of tasks (and incidentally subtasks) ordered. As those tasks are linked to a Project entity, this lets me prioritize not only tasks, but projects as well, even indirectly.\r\n\r\nAt a certain point, I display only tasks for a particular project, and I create some as well. For subtasks, I don't have any problem, but for tasks at level 0, I do.  \r\nThe main goal would be to set the newly created task at top of the list _for the project_. Speaking with a family, this means to me saving the new task as the _previous sibling of_ the first one for this project.\r\n\r\nSo I made my code, and soon discovered that I might have something wrong, as everytime I create a new task, it is placed at the very end of the complete list. After a serial of various tests – including persisting \"normally\", flushing, persisting as previous sibling, flushing again), I've been digging through the code, and I saw things that puzzled me.\r\n\r\nIn Gedmo\\Tree\\Strategy\\ORM\\Nested::updateNode, I see a complex treatment if there is a parent. In my case, of course, there's none, and I fell in [the second case](/Atlantic18/DoctrineExtensions/blob/master/lib/Gedmo/Tree/Strategy/ORM/Nested.php#L388).\r\n\r\nIt looks like this case assumes there's no way to know where to save the node, and therefore puts it at the end of the set. But we have the `sibling` property, [set in NestedTreeRepository](/Atlantic18/DoctrineExtensions/blob/master/lib/Gedmo/Tree/Entity/Repository/NestedTreeRepository.php#L113), so we don't need to fetch the \"end of the tree\". In this case, we simply can save the sibling's TreeLeft value, move the sibling down, and set the right and left weights to the new node according to the saved TreeLeft number. If the sibling has a parent, we can as well have the information from the sibling.\r\nSimilar for `persistAsNextSiblingOf`, where we have to save the TreeRight instead, and move down only the \"next\" ones,\r\n\r\nI therefore wondered if I wasn't torturing myself and tried to add TreeRoot, as my case would be similar to multiple roots. But at this moment I get the error \"Cannot persist sibling for a root node, tree operation is not possible\".\r\n\r\nNow's what's puzzling me : which difficulty did I miss that makes blocking the implementation of persisting a new node as a sibling of a given root node, with or without TreeRoot?\r\n\r\n_Edit_ : I read #208, this feels like a duplicate, sorry. I'd continue over there in case it is better.\r\nMeanwhile, I'll try to understand the parent problem.\r\nThe parent property is only here as help, so as the root property. Why would one of these be needed when the Nested Set model uses only the right and left values?"},"comment":{"url":"https://api.github.com/repos/Atlantic18/DoctrineExtensions/issues/comments/164741965","html_url":"https://github.com/Atlantic18/DoctrineExtensions/issues/1481#issuecomment-164741965","issue_url":"https://api.github.com/repos/Atlantic18/DoctrineExtensions/issues/1481","id":164741965,"user":{"login":"l3pp4rd","id":132389,"avatar_url":"https://avatars.githubusercontent.com/u/132389?v=3","gravatar_id":"","url":"https://api.github.com/users/l3pp4rd","html_url":"https://github.com/l3pp4rd","followers_url":"https://api.github.com/users/l3pp4rd/followers","following_url":"https://api.github.com/users/l3pp4rd/following{/other_user}","gists_url":"https://api.github.com/users/l3pp4rd/gists{/gist_id}","starred_url":"https://api.github.com/users/l3pp4rd/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/l3pp4rd/subscriptions","organizations_url":"https://api.github.com/users/l3pp4rd/orgs","repos_url":"https://api.github.com/users/l3pp4rd/repos","events_url":"https://api.github.com/users/l3pp4rd/events{/privacy}","received_events_url":"https://api.github.com/users/l3pp4rd/received_events","type":"User","site_admin":false},"created_at":"2015-12-15T11:57:50Z","updated_at":"2015-12-15T11:57:50Z","body":"@stof if tree has tree root then yes sure, but I think @Ymox uses tree without TreeRoot annotation and in that case **lft, rgt** values make sense to make ordering..\r\nAlso it would probably totally make sense to have TreeRoot as relation, which is another issue.."}}