Hi everyone,
Let’s say i have 2 custom pages : Driver & Car.
So far i have 4 new blueprints for this:
drivers.yml
driver.yml
cars.yml
car.yml
Now i want to display all cars owned by a driver, so the relationship is one Driver has many Car.
So i add a new multiselect field in driver.yml with a query language options and all works fine, i can select cars and save my driver page with the following text:
But, if i change the slug of Super Car in admin panel, the relationship is removed from the driver.txt.
I expected that the driver.txt would be updated with the new slug.
I’d like to know if i’m not using the correct field type, or if kirby is not designed to allow this kind of relationship constraints.
If you don’t want to constantly use hooks the only way to get around this issue is by assigning unique ids to all pages and store these unique ids instead if the page id.
The easy, and efficient, way to represent “one to many” relationships in Kirby is to have parent and child pages.
If one driver has many cars, and each car belongs to only one driver, the cars ideally would be child pages of the drivers. This way, the relationship between a driver and his cars is given simply by the existence of the car pages: no need to store them in a field. To get the cars of a driver you simply do $driver->children() and to get the owner of a car you do $car->parent().
It gets more complicated only when you need “many to many” relationships, like when each car belongs to many drivers and each driver still has many cars.