1

嘿,如果有相同的日期和时间,我如何优先考虑。例如,我有 2 个用户在同一日期和时间按下一个按钮。因此,在我的数据库中,我有两个具有相同日期和时间的值。现在我想要的是优先考虑哪个是第一个,哪个是第二个。然后我将只接受第一个的值并显示错误消息“不可用”到第二个

例如:

User 1 press a button = 2014-07-07 03:22:00

User 2 press a button = 2014-07-07 03:22:00

现在从这两个我想优先考虑哪个是第一个,哪个是第二个

下面是我在表中保存数据的代码,我使用 Yii 框架创建了它

public function actionAdd($bookId ) {

    $book = Book::model()->findByPk($bookId);

    //check if booking is allowed
    if (!Helper::allowBooking($bookId)) {
        throw new CHttpException(400,'Book is not available for booking');
    }

    //if booking allowed, save data into table
    $model = new Booking;
    $model->user_id = Yii::app()->user->id;
    $model->book_id = $bookId;

    $model->booked = date('Y-m-d H:i:s');

    if ($model->save()) {
        Yii::app()->user->setFlash('success', $book->title . ' successfully added to your booked list.<br>Kindly find your book at Rack: <b>' . $book->rack . '</b> and display ID: <b>' . $model->id . '</b> when you want to check out the book from the library');
        $this->redirect(array('/users/history'));


    }
}

这是我的允许预订代码

public static function allowBooking($id) {
    $bookings = Booking::model()->findAllByAttributes(array('user_id' => Yii::app()->user->id));    

    //check global limit for borrowing
    $limit = Booking::model()->findBySql("SELECT count(*) as qty FROM bookings WHERE user_id = '" . Yii::app()->user->id . "' AND returned IS null AND ((time_to_sec(timediff(DATE_ADD(booked, INTERVAL 2 HOUR), NOW())) / 3600) > 0 AND (time_to_sec(timediff(DATE_ADD(booked, INTERVAL 2 HOUR), NOW())) / 3600) < 2)");

       //Only Limit For 5 Booking
       if ($limit->qty >= 5)  
       {
       return false;
       }        

    $book = Book::model()->findByPk($id);
    //don't take old booking into account, check for within 2 hours
    $booked = Booking::model()->findBySql("SELECT count(*) as qty FROM bookings WHERE book_id = '$id' AND returned IS null");

    $availableQty = $book->stock - $booked->qty;

        if ($availableQty > 0)
        {
        return true;
        }

        return false;
 }
4

0 回答 0