I have a table in our MySQL database called "folder". It stores the names of virtual folders for different users in our website. Structure below:
CREATE TABLE IF NOT EXISTS `folder` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`userId` int(11) NOT NULL,
`parentId` int(11) DEFAULT NULL,
`folderName` varchar(255) NOT NULL,
`isPublic` int(1) NOT NULL DEFAULT '0'
PRIMARY KEY (`id`)
);
The website allows for any number of child folders within folders, so:
Base Folder
- Child Folder 1
--- Child Folder 2
etc
Each folder can have a public or private status. So:
Base Folder (Public)
- Child Folder 1 (Private)
--- Child Folder 2 (Private)
If "Child Folder 1" is set to "Private" it will always cause sub-folders to be private, no matter if they've been set as public in the dd.
My question is, how can I decide whether a specific folder is public or private via MySQL, considering the inheritance?
Take "Child Folder 2" as an example. To check if that is private or public we can do this:
SELECT isPublic FROM folder WHERE folderName = "Child Folder 2";
(just an example, in the code we use the folder ids :))
This doesn't account for inheritance though. So the parent folder may be set as private.
Any idea how I can loop through any number of parent folders until a private state is found or there's no more parent folders. Ideally I need to end up with a 1 or a 0 returned from the query, 1 being public and 0 private.
Thanks in advance, Red.