Unable To Insert The Room Booking Details

Hi,

i have problem in insert the booking details.Basically i am creating a room booking system. This is how my idea go, i fill in all the booking details, if there are available it will allow to book, else it not allow to continue. This is to prevent any duplicate booking details.

I create few tables which is ‘bookingdetail’,‘timeslot’,‘user’, ‘refroom’,‘rootype’

table bookingdetail

bookingID,UserID,RoomTypeID,RefroomID,TimeID,BookingStatus,Event,StartDate,EndDate`

table timeslot

TimeSlotID,TimeSlot

1 8.00-9.00am

2 9.00-10.00am

3 10.00-11.00am

4 11.00-12.00pm

table user

UserID,Username

1 ALEX

table refroom

RefroomID,Room,RoomID

1 TR1 1

2 TR2 1

3 MRU1 2

4 MRU2 2

table roomtype

RoomID,Room

1 Tutorial Room

2 Meeting Room

These are the table i create to run the room booking system.


public function actionProcessBooking() {

        $modelBooking = new Bookingdetail();

        //$Booking = get_class($modelBooking);

        $this->performAjaxValidation($modelBooking);

        

        $bookingStatus = Yii::app()->db->createCommand()

                ->select('BookingID,RefroomID,BookingStatus,TimeID,StartDate,EndDate')

                ->from('bookingdetail')

                ->where('BookingStatus=:BookingStatus',array(':BookingStatus'=> "1" ))

                ->queryAll();

        

            if ( $bookingStatus) {

               

                 echo "The time slot of the room have been booked!"

                . "Please reserve another room and time slot.Thank You.";

                 

            } else {

                $command = Yii::app()->db->createCommand();

                $command->insert('bookingdetail', array(

                    'UserID' => Yii::app()->session['ID'],

                    'RoomTypeID' => $_POST['roomtype'],

                    'RefroomID' => $_POST['referroomtype'],

                    'BookingStatus' => '1',

                    'Event' => $_POST['Bookingdetail']['Event'],

                    'TimeID' => $_POST['timeslot'],

                    'StartDate' => $_POST['Bookingdetail']['StartDate'],

                    'EndDate' => $_POST['Bookingdetail']['EndDate']

                ));

                $this->redirect(Yii::app()->request->hostInfo . Yii::app()->request->baseURL . "/index.php/site/adminuserbookingdetails");

             

            }

}

my idea is if the room is booked, the booking status is 1. So, i select from booking details which status is 1 will able to avoid user to book the same time,same room but somehow,the code above is not working. I hope got anyone able to guide me and correct me. I sorry if i not so good in php and database.

Looking at your code, it looks like if you have ANY booking for ANY room, the user will not be able to book a room. This is because your are SELECTing all records WHERE status=1. Your If() check checks if you are getting back any records. There is now checking for which room is booked.

I’m interested in why you are using straight SQL instead of ActiveRecord? Just curious.

As for checking if the room is booked, you look at how the default search action works. I think it could be modified to return a single record/model.

I might try something like:




public function actionProcessBooking() {

   $modelBooking = new Bookingdetail();


   //$this->performAjaxValidation($modelBooking);  //<-- Not sure about using this


        if (isset($_POST['Bookingdetail'])) {

            $modelBooking ->attributes = $_POST['Bookingdetail'];


            $valid = $modelBooking->validate();


            if (!$valid) {

               if($modelBooking->searchForBooking($modelBooking)) {

                 // I'd use Yii::app()->user->setFlash() instead


                 echo "The time slot of the room have been booked!"

                . "Please reserve another room and time slot.Thank You.";

            } else {

                $this->redirect(Yii::app()->request->hostInfo . Yii::app()->request->baseURL . "/index.php/site/adminuserbookingdetails");

             

            }

        $this->render('bookingForm', array(

            'modelBooking' => $modelBooking,

        ));

}

Code is rough and not tested.

Thank for the guide.i try to modified it.