Yii优先考虑相同的日期和时间

时间:2014-07-06 20:02:29

标签: php yii yii-events

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

例如:

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;
 }

0 个答案:

没有答案