MilMike
MilMike

Reputation: 12831

How to use one table multiple times?

I have a table called flights in which I set the departure and arrival airport. The airports is just another table and I want just to use the airport id in the flights table. How can I do this?, normally I can only use one id (airport_id) how can I use the second one? something like airport_id_2 ?..

My Flight model looks like this:

class Flight extends AppModel{
    public $hasOne = "Airport";
}

Or should I do this manually by using joins?

Upvotes: 0

Views: 41

Answers (1)

arilia
arilia

Reputation: 9398

you can use two fields:

arrival_airport_id

departure_airport_id

and in your Model

public $belongsTo = array(
    "DepartureAirport" => array(
        'className' => 'Airport',
        'foreignKey' => 'departure_airport_id'
    ),
    "ArrivalAirport" => array(
         'className' => 'Airport',
         'foreignKey' => 'arrival_airport_id'
    )
)

PS: I think the right relationships is belongsTo and not HasOne because one airport can be related to many flights

Upvotes: 1

Related Questions