Yii Prioritize a same date and time

65 views Asked by At

Hye there how do i prioritize if there are same date and time. For example i have 2 users press a button on a same date and time. Therefore in my database i have two values with the same date and time. Now what i want is to prioritize which is the first and which is the second. Then i will only accept value for the first and show and error message "Not Available" to the second

For Example:

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

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

Now from this two i want to prioritize it which one is the first and which one is the second

Below Is My Code To Save Data In Table where i created it using Yii framework

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'));


    }
}

This Is My Code For Allow Booking

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

There are 0 answers