D
D
Dmitry2017-04-09 12:30:51
MySQL
Dmitry, 2017-04-09 12:30:51

How to organize the search for round-trip tickets?

Good afternoon.
The site has a ticket search.
The search is carried out on the following fields:
1) City of departure
2) City of arrival
3) Date of departure
4) Date of arrival
5) Checkbox to select the direction, one way or round trip ticket.
Now the search model contains the following conditions:

$query->andWhere(['status' => Flights::STATUS_ACTIVE])
              ->andWhere(['>=', 'date_start', $this->date_from ? strtotime($this->date_from . ' 00:00:00') : null])
              ->andWhere(['=', 'city_start_id', $this->city_from])
              ->andWhere(['=', 'city_end_id', $this->city_to]);

        $query->andFilterWhere(['<=', 'date_end', $this->date_to ? strtotime($this->date_to . ' 23:59:59') : null])
              ->andFilterWhere(['=', 'airline_id', $this->airline])
              ->andFilterWhere(['in', 'airport_start_id', $this->airport_id])
              ->andFilterWhere(['in', 'airline_id', $this->company_id])
              ->andFilterWhere(['=', 'agent', $this->partner]);

The choice of direction is given by the variable
// если переменная равна 1, то билеты надо искать туда/обратно, если равна 2, то билеты искать в одну сторону.
public $direction = 1;

If you are looking for a one-way ticket, then there are no particular problems, it seems to work fine.
But with the search for tickets there / back is difficult.
How to specify the search conditions correctly?
Or, when entering flights into the database, make a connection between round-trip tickets?
PS Actually, I can’t add up in my head what parameters to search for the opposite to do.
If the ticket is "there", then there are two dates, departure / arrival. But if you look for a ticket "back", then it is not clear how to do it with dates. You shouldn’t look for a ticket back on the date of arrival, you need some kind of interval ...
In short, I got completely confused ...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
padlyuck, 2017-04-09
@slo_nik

Usually, services make a fixed departure date. those. if the user is looking for a ticket only "there", then he enters the desired departure date and the system looks for a ticket from point A to point B with a departure on the specified date.
If a round-trip ticket is searched, then the results of the search for the second date are added to the results of the first search, where the starting points are reversed. If you need to set the date of departure and return as an interval, and not a fixed one, then the query will be something like this:
1. to search only "there"

SELECT * FROM `flights` WHERE
    (`departure_time` BETWEEN 'user_departure_wants_min' AND 'user_departure_wants_max'
    AND `departure_airport` = user_departure_wants_from
    AND `arrive_airport` = user_arrive_wants_to);

2. to search "back and forth"
SELECT * FROM `flights` WHERE
    (`departure_time` BETWEEN 'user_departure_1_wants_min' AND 'user_departure_1_wants_max'
    AND `departure_airport` = user_departure_wants_from
    AND `arrive_airport` = user_arrive_wants_to)
    OR
    (`departure_time` BETWEEN 'user_departure_2_wants_min' AND 'user_departure_2_wants_max'
    AND `departure_airport` = user_arrive_wants_to
    AND `arrive_airport` = user_departure_wants_from)

Where user_departure_wants_min, user_departure_wants_max, user_departure_1_wants_min, user_departure_1_wants_max - the minimum and maximum date of departure "there"
user_departure_2_wants_min, user_departure_2_wants_max are the minimum and maximum dates of departure "back"
user_departure_wants_from - the airport from which the user wants to fly (and where he wants to return in case of searching "there and back")
user_arrive_wants_to - the airport where the user wants to fly to (and where he wants to fly from in case of searching for "there and back")
Somehow it seems to me that it should be. This option only takes into account the search for direct flights. Search with transfers will naturally be much more difficult.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question