https://github.com/drupol/adjacencylist
Doctrine and Adjacency Lists
https://github.com/drupol/adjacencylist
Last synced: about 1 month ago
JSON representation
Doctrine and Adjacency Lists
- Host: GitHub
- URL: https://github.com/drupol/adjacencylist
- Owner: drupol
- Created: 2019-07-13T16:54:45.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2019-07-17T16:40:15.000Z (almost 7 years ago)
- Last Synced: 2025-03-05T11:50:00.196Z (about 1 year ago)
- Language: PHP
- Size: 50.8 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# API Platform / Doctrine / Adjacency list
An adjacency list is a way to store a tree data structure using a table having
2 columns.
This tree containing nodes:
```
root
node1
node2
node2.1
node2.2
node3
node3.1
```
Could be stored in a table as such:
| id | parent |
|---------|--------|
| root | null |
| node1 | root |
| node2 | root |
| node2.1 | node2 |
| node2.2 | node2 |
| node3 | root |
| node3.1 | node3 |
The SQL structure of that table would be:
```sql
CREATE TABLE adjacency_list (
id INT AUTO_INCREMENT NOT NULL,
parent_id INT DEFAULT NULL,
INDEX IDX_FDE5D6BF727ACA70 (parent_id),
PRIMARY KEY(id)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB
ALTER TABLE adjacency_list
ADD
CONSTRAINT FK_FDE5D6BF727ACA70
FOREIGN KEY (parent_id)
REFERENCES adjacency_list (id)
```
Then, another table can be created that would contains the metadata of each node.
| id | nodeId | firstname | lastname |
|----|---------|-----------|----------|
| 1 | root | A | B |
| 2 | node1 | C | D |
| 3 | node2 | E | F |
| 4 | node2.1 | G | H |
| 5 | node2.2 | I | J |
| 6 | node3 | K | L |
| 7 | node3.1 | M | N |
The structure would be:
```sql
CREATE TABLE adjacency_list_item (
id INT AUTO_INCREMENT NOT NULL,
nodeId INT DEFAULT NOT NULL,
firstname VARCHAR(255) NOT NULL,
lastname VARCHAR(255) NOT NULL,
UNIQUE INDEX UNIQ_DDADA13E126F525E (nodeId),
PRIMARY KEY(id)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB
ALTER TABLE adjacency_list_item
ADD
CONSTRAINT FK_DDADA13E126F525E
FOREIGN KEY (nodeId)
REFERENCES adjacency_list (id)
```
# Issue
Find why we are not able to validate the schema.
* Run: `./bin/console doctrine:schema:validate`
# Usage
In order to get started with this, do:
* Run: `composer install`
* Update the `.env` file and set the URL to your database, mysql or mariadb is advised here.
* Run: `./bin/console doctrine:database:create`
* Run: `./bin/console doctrine:schema:update --force --complete --dump-sql`
To generate the fixtures in the database, do:
* Run: `./bin/console hautelook:fixtures:load`
### More informations:
Find the difference between the database schema and the entities:
* Run:
```shell
bin/console do:da:dr --force
bin/console do:da:cr
bin/console do:sc:up --force
bin/console do:sc:val
bin/console do:sc:up --dump-sql
```