weno Posted June 26, 2014 Share Posted June 26, 2014 I have three tables in my database booking, bookingrooms, room and the bookingrooms is the link table resolving many to many. CRUD has created forms for booking and room, but the booking form is too simple and does not include the bookingrooms section that I want in the form. How do I put this area onto the booking form? I presume it's something to do with multiple models and custom forms. Link to comment Share on other sites More sharing options...
Edward Posted June 26, 2014 Share Posted June 26, 2014 You can't put the MANY_MANY relationship into Gii, you don't actually need to create a model for the intermediary table bookingrooms. All you are required to do is create a MANY_MANY relation in the Booking model(). You could also create the relation in the Room model if you have to. But here is what the relation will look like in your Booking model(): public function relations() { return array( 'bookingRoom'=>array(self::MANY_MANY,'Room','booking_rooms(booking_id,room_id)'), ); } Link to comment Share on other sites More sharing options...
weno Posted June 26, 2014 Author Share Posted June 26, 2014 I have already implemented the relationships in the Booking model. I'm looking to get started with the next step. Such as Handling related models in Yii forms - http://www.larryullman.com/2010/08/10/handling-related-models-in-yii-forms/ but I see that Larry has got different kindof mapped many-to-many relationship in his example. Link to comment Share on other sites More sharing options...
weno Posted June 26, 2014 Author Share Posted June 26, 2014 I've followed Larry's example and trying to apply it to my own application . The dropdown box on my update form is displaying, but there aren't any values in the dropdown. Link to comment Share on other sites More sharing options...
weno Posted June 26, 2014 Author Share Posted June 26, 2014 Solved this now - empty dropdowns. Link to comment Share on other sites More sharing options...
Edward Posted June 27, 2014 Share Posted June 27, 2014 Glad you managed to get the problem worked out. Link to comment Share on other sites More sharing options...
weno Posted June 27, 2014 Author Share Posted June 27, 2014 Hi, I have run into another problem. I have begun implementing the master detail form. In the create.php form I have duplicated rows(HTML and fields) which are for user input, but this causes the highlighted fields(that have been chosen in the last form submission) on the webpage to duplicate... if I choose a value in the first row it automatically chooses the same in the second row. I use two models - both of them are passed to the form. <?php /* @var $this BookingController */ /* @var $model Booking */ $this->breadcrumbs=array( 'Bookings'=>array('index'), 'Create', ); $this->menu=array( array('label'=>'List Booking', 'url'=>array('index')), array('label'=>'Manage Booking', 'url'=>array('admin')), ); ?> <h1>Create Booking</h1> <?php //$this->renderPartial('_form', array('model'=>$model)); ?> <?php /* @var $this BookingController */ /* @var $model Booking */ /* @var $form CActiveForm */ ?> <div class="form"> <?php $form=$this->beginWidget('CActiveForm', array( 'id'=>'booking-form', // Please note: When you enable ajax validation, make sure the corresponding // controller action is handling ajax validation correctly. // There is a call to performAjaxValidation() commented in generated controller code. // See class documentation of CActiveForm for details on this. 'enableAjaxValidation'=>false, )); ?> <p class="note">Fields with <span class="required">*</span> are required.</p> <?php echo $form->errorSummary($model); ?> <div class="row"> <?php echo $form->labelEx($model,'Id'); ?> <?php echo $form->textField($model,'Id'); ?> <?php echo $form->error($model,'Id'); ?> </div> <div class="row"> <?php echo $form->labelEx($model,'customerId'); ?> <?php echo $form->textField($model,'customerId'); ?> <?php echo $form->error($model,'customerId'); ?> </div> <div class="row"> <?php echo $form->labelEx($model,'date'); ?> <?php echo $form->textField($model,'date'); ?> <?php echo $form->error($model,'date'); ?> </div> <div> <?php echo $form->labelEx($BookingRoom,'Room No'); ?> <?php echo $form->dropDownList($BookingRoom, 'roomId', CHtml::listData( Room::model()->findAll(), 'id', 'id'), array('single'=>'single', 'size'=>5) ); ?> <?php echo $form->error($BookingRoom,'roomId'); ?> </div> <div> <?php echo $form->labelEx($BookingRoom,'startDate'); ?> <?php echo $form->textField($BookingRoom,'startDate'); ?> <?php echo $form->error($BookingRoom,'startDate'); ?> </div> <div> <?php echo $form->labelEx($BookingRoom,'endDate'); ?> <?php echo $form->textField($BookingRoom,'endDate'); ?> <?php echo $form->error($BookingRoom,'endDate'); ?> </div> <div> <?php echo $form->labelEx($BookingRoom,'adults'); ?> <?php echo $form->dropDownList($BookingRoom, 'adults', array('1'=>'1', '2'=>'2', '3'=>'3', '4'=>'4', '5'=>'5'), array('single'=>'single', 'size'=>5) ); ?> <?php echo $form->error($BookingRoom,'adults'); ?> </div> <div> <?php echo $form->labelEx($BookingRoom,'children'); ?> <?php echo $form->dropDownList($BookingRoom, 'children', array('1'=>'1', '2'=>'2', '3'=>'3', '4'=>'4', '5'=>'5'), array('single'=>'single', 'size'=>5) ); ?> <?php echo $form->error($BookingRoom,'children'); ?> </div> <div> <?php echo $form->labelEx($BookingRoom,'Room No'); ?> <?php echo $form->dropDownList($BookingRoom, 'roomId', CHtml::listData( Room::model()->findAll(), 'id', 'id'), array('single'=>'single', 'size'=>5) ); ?> <?php echo $form->error($BookingRoom,'roomId'); ?> </div> <div> <?php echo $form->labelEx($BookingRoom,'startDate'); ?> <?php echo $form->textField($BookingRoom,'startDate'); ?> <?php echo $form->error($BookingRoom,'startDate'); ?> </div> <div> <?php echo $form->labelEx($BookingRoom,'endDate'); ?> <?php echo $form->textField($BookingRoom,'endDate'); ?> <?php echo $form->error($BookingRoom,'endDate'); ?> </div> <div> <?php echo $form->labelEx($BookingRoom,'adults'); ?> <?php echo $form->dropDownList($BookingRoom, 'adults', array('1'=>'1', '2'=>'2', '3'=>'3', '4'=>'4', '5'=>'5'), array('single'=>'single', 'size'=>5) ); ?> <?php echo $form->error($BookingRoom,'adults'); ?> </div> <div> <?php echo $form->labelEx($BookingRoom,'children'); ?> <?php echo $form->dropDownList($BookingRoom, 'children', array('1'=>'1', '2'=>'2', '3'=>'3', '4'=>'4', '5'=>'5'), array('single'=>'single', 'size'=>5) ); ?> <?php echo $form->error($BookingRoom,'children'); ?> </div> <div class="row buttons"> <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?> </div> <?php $this->endWidget(); ?> </div><!-- form --> 1 Link to comment Share on other sites More sharing options...
weno Posted June 29, 2014 Author Share Posted June 29, 2014 Please ignore this I am past this stage now Link to comment Share on other sites More sharing options...
Larry Posted June 30, 2014 Share Posted June 30, 2014 Sorry I wasn't able to help (I'm traveling now) but glad you figured it out. Also, in the future, it'd be appreciated if you also shared your solution. Link to comment Share on other sites More sharing options...
Recommended Posts