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 created
and modified
fields. Your tables will look something like this (add extra columns as needed):
actors
id
year
director
description
created # automatically filled in by cake
modified # automatically filled in by cake
images
id
actor_id
path
created
modified
Now, update your models to reflect the relationships. You have Actor hasMany Image here, and Image belongsTo Actor.
/app/Model/Actor.php
<?php
class Actor extends AppModel {
public $hasMany = array('Image');
}
/app/Model/Image.php
<?php
class Image extends AppModel {
public $belongsTo = array('Actor');
}
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
<?php
App::uses('AppController', 'Controller');
class ActorsController extends AppController {
public function index() {
// get all actors and their images
$this->Actor->recursive = 1;
$actors = $this->Actor->find('all');
// send to view as a variable named '$actors'
$this->set('actors', $actors);
}
}
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
<table>
<thead>
<tr>
<th>Name</th>
<th>Image</th>
</tr>
</thead>
<tbody>
<?php
foreach ($actors as $actor) {
$name = $this->Html->tag('td', $actor['Actor']['name']);
// show first image
$img = $this->Html->image($actor['Image'][0]['path']);
$image = $this->Html->tag('td', $img);
echo $this->Html->tag('tr', $name.$image);
}
?>
</tbody>
</table>
99% of this work can be done by using Cake's bake
shell. 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.