简体   繁体   English

在酒店预订系统上工作时,我卡在了检查可用房间上

[英]working on hotel reservation system and I get stuck on check avaibility room

there is my db structure:有我的数据库结构:

  1. Rooms: roomId-roomName-roomCapacity房间:roomId-roomName-roomCapacity
  2. Reservation: rsId-rsroomId-date_in-date_out预约:rsId-rsroomId-date_in-date_out

I want to create a search function for searching available rooms.我想创建一个搜索 function 来搜索可用房间。 I can't get the ones which are available, which are not booked for those days.我无法获得那些天没有预订的可用的。 I have tried lots of things but till now can't solve this issue.我已经尝试了很多东西,但直到现在还无法解决这个问题。 There is my latest code:有我的最新代码:

function search ($name, $date_in, $date_out) {
    $this->db->select('*')->from('Rooms');
    $this->db->join('Reservation', 'rooms.roomid = reservation.rsroomId', 'LEFT');
    $this->db->like('date_in', $date_in);
    $this->db->like('date_out', $date_out);
    $this->db->like('roomName', $name);
    return $this->db->get()->result_array();
}

Thank You谢谢你

I am not very familiar with the CodeIgniter way to build a query, but I understand what you want to achieve.我不太熟悉 CodeIgniter 构建查询的方式,但我了解您想要实现的目标。 The raw query would look something like this:原始查询看起来像这样:

SELECT  Rooms.*
FROM    Rooms
        LEFT JOIN Reservation
            ON Rooms.roomId = Reservation.rsroomId
                AND date_in < '$date_out' AND date_out > '$date_in'
WHERE   rsId IS NULL;

To select all overlapping reservations in the JOIN ON condition, you must compare the date_in of existing reservations with the $date_out of the desired reservation, and vice versa.对于 select JOIN ON条件中的所有重叠预订,您必须将现有预订的date_in与所需预订的$date_out进行比较,反之亦然。 If an existing reservation checks out before you want to check in, it does not overlap.如果现有预订在您要入住之前退房,则不会重叠。 If an existing reservation checks in after you want to check out, it does not overlap.如果现有预订在您要退房后入住,则不会重叠。

After carefully crafting these JOIN ON conditions to get the overlapping reservations, the WHERE rsId IS NULL condition ensures that you get only rooms for which there is no such existing overlapping reservation.在精心设计这些JOIN ON条件以获得重叠预订后, WHERE rsId IS NULL条件可确保您仅获得不存在此类现有重叠预订的房间。

That last part is easy in CodeIgniter, but getting all the three JOIN ON conditions into $this->db->join() is going to be messy.最后一部分在 CodeIgniter 中很容易,但是将所有三个JOIN ON条件都放入$this->db->join()会很麻烦。 The like() and where() functions automatically escape your values, but here you need to add this protection against SQL injection manually. like()where()函数会自动转义您的值,但在这里您需要手动添加此保护以防止 SQL 注入。 Perhaps something like this:也许是这样的:

function search ($date_in, $date_out) {
    // Ensuring these values are safe in raw SQL:
    $date_in = $this->db->escape($date_in);
    $date_out = $this->db->escape($date_out);
    
    $joinCondition = 'rooms.roomid = reservation.rsroomId';
    $joinCondition .= " AND date_in < $date_out AND date_out > $date_in";
    
    $this->db->select('*')->from('Rooms');
    $this->db->join('Reservation', $joinCondition, 'LEFT');
    $this->db->where('rsId', NULL);
    return $this->db->get()->result_array();
}

An alternative is to put question marks in the raw query, so CodeIgniter can escape your values via query bindings:另一种方法是在原始查询中加上问号,这样 CodeIgniter 就可以通过查询绑定转义您的值:

function search ($date_in, $date_out) {
    $sql = "SELECT  Rooms.*
            FROM    Rooms
                    LEFT JOIN Reservation
                        ON Rooms.roomId = Reservation.rsroomId
                            AND date_in < ? AND date_out > ?
            WHERE   rsId IS NULL";
    $binds = [$date_out, $date_in];
    return $this->db->query($sql, $binds)->result_array();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM