0

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?

4

2 回答 2

3

Man, search and learn about Entity–Relationship models. This question has nothing to do with PHP or CakePHP.

Answering your question, You represent your data depending on what you want to achieve

1. Actor has many pictures

As you said, there could be two tables: each one with an unique id also known as primary key (PK). And the second table with a "one to many" relationship (or 1:n), this table has also a foreign key (FK) that relates to the first one. So for example, actor with id=4 could have four pictures (id=1,2,5,9) but all of them with the foreign key (actor_id=4)

The structure would look like this:

Actors                        Pictures
----------------              -------------------
id (PK)                       id (PK)
name                          actor_id (FK)
address                       file_name
birth_date
...                    

With this setup you have pictures related to the actor. But you talked about films, so you may need the second option.

2. Actor has many films and these films have many pictures

Adding another table in between, called for example 'Film', you can relate the pictures to this table.

So you would have for example the same actor with id=4 and two films (id=2,6) both with the same relationship to the actor (actor_id=4) and the first film having two pictures (id=1,2 film_id=2) and the second film with four more pictures (id=3,4,5,6 film_id=6)

Actors                        Films                         Pictures
----------------              -------------------           -------------------
id (PK)                       id (PK)                       id (PK)
name                          actor_id (FK)                 film_id (FK)
address                       name                          file_name
birth_date                    year
...

Did it help you?

于 2012-07-25T15:24:47.727 回答
3

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.

于 2012-07-25T15:29:13.783 回答