Задать вопрос
@lexstile

Как правильно формировать запросы на основе get-параметров?

Есть запрос:
public function getListEvents($route) {
		$max = 10;
		$params = [
			'max' => $max,
			'start' => ((($route['num'] ?? 1) - 1) * $max),
		];

		if (isset($_GET['country_id']) && !empty($_GET['country_id'])) {
			$params['country_id'] = $_GET['country_id'];

			return $this->db->row('
				SELECT events.id, events.name as event_name, events.date_start, events.date_end, events.country_id, events.description, countries.name as country_name, links.links, images.images FROM events
				JOIN countries ON events.country_id = countries.id
				LEFT JOIN (SELECT event_id, GROUP_CONCAT(link) as links FROM links GROUP BY event_id) AS links ON events.id = links.event_id
				LEFT JOIN (SELECT event_id, GROUP_CONCAT(name) as images FROM images GROUP BY event_id) AS images ON events.id = images.event_id
				WHERE
				'. (isset($_GET['country_id']) ? 'country_id = :country_id' : '') .'
				'. (isset($_GET['date_start']) ? 'events.date_start >= :date_start' : '') .'
				'. (isset($_GET['date_end']) ? 'AND events.date_start <= :date_end' : '') .'
				ORDER BY events.date_start '.  (isset($_GET['by']) ? $_GET['by'] : '') .'
				LIMIT :start, :max
			', $params);
		}
		
		return $this->db->row('
			SELECT events.id, events.name as event_name, events.date_start, events.date_end, events.country_id, events.description, countries.name as country_name, links.links, images.images FROM events
			JOIN countries ON events.country_id = countries.id
			LEFT JOIN (SELECT event_id, GROUP_CONCAT(link) as links FROM links GROUP BY event_id) AS links ON events.id = links.event_id
			LEFT JOIN (SELECT event_id, GROUP_CONCAT(name) as images FROM images GROUP BY event_id) AS images ON events.id = images.event_id
			ORDER BY id DESC
			LIMIT :start, :max
		', $params);
	}

Хочется сделать запрос более гибким.
Есть ли какой-то способ удобной работы и формирования запросов с помощью get-параметров?
  • Вопрос задан
  • 19 просмотров
Подписаться 1 Простой Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы