2

我正在使用 codeigniter 为客户建立一个租赁网站。仅当没有租赁期与我的查询匹配时,我才尝试返回 true。基本上有一张出租表,上面有开始日期和结束日期。

我的客户从日期选择器中选择开始日期,结束日期是开始日期后的固定天数。

因此,我想了解在这些日期是否有任何出租物品正在出租,以验证客户是否可以预订该物品。这是我到目前为止所拥有的,我需要使用 Codeigniters 活动记录来完成这一点......

function check_product($periodLength) {

     //This is the start time they chose from the picker
    $startTime = $_POST['date'];
     //The period length is a variable of days, so the end date would be their chosen start date plus a certain amount of days...
    $endTime = strtotime('+'.$periodLength.'day', $startTime);

    $this->db->where('productsId', $_POST['productId']);
           //This is where I need help!!! What query would give me records that are NOT between the start and end dates that i'm wanting
    $query = $this->db->get('rentals');
    $data = array();

    foreach ($query->result() as $row) {
    $data[] = array(
        'id' => $row->id,
        'customerId' => $row->customerId,
        'productsId' => $row->productsId,
        'periodStart' => $row->periodStart,
        'periodEnd' => $row->periodEnd,
        'status' => $row->status,
        'specialInstructions' => $row->specialInstructions,
        'adminNotes' => $row->adminNotes
    );
    }
    return $data;
}

我确定我的大部分问题都在我的脑海中,但我需要弄清楚我的开始日期到结束日期期间是否已经保留。

4

1 回答 1

2

试试这个:

    $startTime = $_POST['date'];
    $endTime = strtotime('+'.$periodLength.'day', $startTime);

    $this->db->where('productsId', $_POST['productId']);
    $this->db->where(" $startTime NOT BETWEEN periodStart AND periodEnd AND $endTime NOT BETWEEN periodStart AND periodEnd OR ($startTime < periodStart AND $endTime > periodEnd) ");
    //Since this is a complex where clause i would recommend 
    //to put enitre clause in single where statement rather than 
    //putting in different where statement. 
    //And it is upto codeigniter active record standards

     $query = $this->db->get('rentals');
于 2013-02-20T06:04:06.607 回答