0
votes

Tl;dr: (I'd rather learn to fish than be fed a fish but am having trouble teaching myself CakePHP associations with a 5 table DB)

So, I'm working on a flight search project with a database person. He's designed the database with appropriate foreign keys and I trust him to have done a good job because he works with databases at a major Silicon Valley Co..

We've got these tables in the database:

  1. Trips
  2. Legs
  3. Segments (A segment is a part of a leg and a trip contains multiple legs)
  4. Airports
  5. Airlines

I should probably be doing my first project in CakePHP with a simpler schema but that's not really an option so: What are the correct associations here?

Right now I have this:

Trips hasMany legs; foreignkey trip_id in legs table

Legs belongsTo trips; foreign key trip_id in legs table

Legs hasMany segments; foreign key leg_id in segments table

And that's it. So clearly I need more to fully use CakePHP (I think so, at least) and I think I'm impeded by the database design (it's not guaranteed that my friend has designed it optimally). I just am not sure how to do the airports and airlines.

I have spent a few hours googling and re-reading the book I'm using. Thanks for any help and GENERAL CakePHP-Association advice/thoughts you have.

1

1 Answers

2
votes

Generally, keep your associations two-ways.

  • Trip hasMany Leg
  • Leg belongsTo Trip
  • Leg hasMany Segment
  • Segment belongsTo Leg

This will allow you to access models like so:

$this->Leg->Segment->create();
$this->Segment->Leg->create();
$this->Segment->Leg->Trip->create();

Same goes for Leg and Segment. Also, the naming convention for CakePHP models dictates that they be singular. :)