I’m building my very first and simple app, but because I’m new to web-development I have some questions on how to properly organize my DB structure (and models).
One of my pages is an actor filmography so I need to display:
Year, director, description etc. And also I need to display a few pictures related to current film.
As far as I know I need to make 2 tables in my database.
First one with ID and all other fields (Year, director, description). And second table with film ID and some fields for images file names. It is a right way?
But I don’t understand how to link this tables together and when retrieve my data from this two tables and send it to the View. I read a cookbook but it’s not simple for a newbie.
Can someone please provide me an example related to my requirements?
You’re correct in assuming you’ll need 2 tables. For easiest configuration, follow Cake’s conventions. Tables are plural. We’ll also use the automatically updated
createdandmodifiedfields. Your tables will look something like this (add extra columns as needed):actors
images
Now, update your models to reflect the relationships. You have Actor hasMany Image here, and Image belongsTo Actor.
/app/Model/Actor.php
/app/Model/Image.php
Since we’ve used Cake’s conventions, it will automatically know the table, foreign keys, and pull the relationships when you do a
find().We’ll make a quick index action for your actors.
/app/Controller/ActorsController.php
In your view, iterate through the actors and display information as you like! Here’s an example of a very simple view that shows the actors name and first image in a table.
/app/View/Actors/index.ctp
99% of this work can be done by using Cake’s
bakeshell. It will generate models, controllers, and views for you. Then you can go in and customize them as you like. I really recommend getting bake working as it will make your life easier and even provide you with best practices hints.