Part 7: Creating child tables
Now we have a list of films, but lets expand it a bit. It could be useful to show which actors played in a film. By using a child table we can let the user click on a film in our first table, which will then open a list of actors that played in that movie.
Let’s check how the tables of films and actors are related. This can be done in the DBPTK schema. We see that film_actor connects the film and actor table.
We can see that the
film_actor
table contains two columns linking actors with films. This is perfect for our child table, because we can click an entry to use one of it’s fields as a<primarykey>
. In our case it will be thefilm_id
of the selected film. We will then display the child tablefilm_actor
which contains only the rows where the specified primary key matches a<foreignkey>
. The foreign key will also befilm_id
, asfilm_actor
has that column.
Lets define a new child table in our XML template. This will be done similarily to the first table, but we need 2 extra tags:
<parent>
, which is the name of the parent table, and as mentioned<foreignkey>
, which will be matched against the primary key of the parent table.
Our child table now looks like this:
- The child actor table is still very barebones, so let’s improve it by using some previously learned techniques to replace the IDs with names using
<lookup>
and change the column header usingas
.
- As a last step it is usually good for child tables like this to have a
<header>
tag that shows which element the child table is a list for, in our case which film. Because we don’t have access to film titles within thefilm_actor
table, we need to use thefilm_id
to perform a lookup on the film table and retrieve the title.
Our child table should now look like this: