I have a dropdown to select a parent, which is self-referencing to the Page. I want to limit the results of that dropdown, so that it won't allow me to nest a Page more than one level.
If I edit the 'Son' Page, and the 'Son' has a 'Grandson', than I shouldn't be allowed to select 'Dad' as parent for the son, since it would create a nest that is to deep
In the folllowing case, When I'm editing the Son record, I shouldn't be able to select Dad as it's parent, since the son has children.
+----+-----------+----------+
| id | parent_id | title |
+----+-----------+----------+
| 1 | NULL | Dad |
| 2 | NULL | Son |
| 3 | 2 | Grandson |
+----+-----------+----------+
Now in this case, I should be able to select Dad as the parent when I'm editing the Son record, since the Son doesn't have any children
+----+-----------+----------+
| id | parent_id | title |
+----+-----------+----------+
| 1 | NULL | Dad |
| 2 | NULL | Son |
| 3 | NULL | Grandson |
+----+-----------+----------+
I'm struggling to get my head around this, and on how to wrap this all in the query builder.
What I have so far
The following code works if the Son has a Child of it's own, I won't be able to select Dad, which is good. But it fails when there's no children.
It comes down to this: My parent select should also allow pages where parent_id is null to be shown, but only if the current record (Son) doesn't have any children.
Recap: Only show the record if it doesn't occur in any parent_id, so has no children, if it does however no records are to be shown.. If it doesn't, it should show the records where parent_id is null. Is this possible in one query?
$query->where('id', '<>', $page->id);
$query->where('parent_id', '<>', $page->id);

