php - How to modify two databes tables in one form in Yii -
i have created 1 form eddit 1 table(hotel_rooms) in database website page, works perdectcly. need include fields,not table, table (hotel_rooms_checked)in edditing web page of privious table (hotel_rooms) how can please ? hotel_rooms.id = hotel_rooms_checked.id
here code form
<?php /* @var $this hotelroomscontroller */ /* @var $model hotelrooms */ /* @var $form cactiveform */ ?> <div class="form"> <?php $form=$this->beginwidget('cactiveform', array( 'id'=>'hotel-rooms-form', 'enableajaxvalidation'=>false, )); ?> <p class="note">fields <span class="required">*</span> required.</p> <?php echo $form->errorsummary($model); ?> <div class="row"> <?php echo $form->labelex($model,'id'); ?> <?php echo $form->textfield($model,'id',array('size'=>60,'maxlength'=>255)); ?> <?php echo $form->error($model,'id'); ?> </div> ....... <?php $this->endwidget(); ?> </div><!-- form -->
and controller code
public function actionupdate($id) { $model=$this->loadmodel($id); // uncomment following line if ajax validation needed // $this->performajaxvalidation($model); if(isset($_post['hotelrooms'])) { $model->attributes=$_post['hotelrooms']; if($model->save()) // $this->redirect(yii::app()->request->urlreferrer); $this->redirect(array('hotels/index','id'=>$model->id)); } $this->render('update',array( 'model'=>$model, )); }
the view code is
<?php /* @var $this hotelroomscontroller */ /* @var $model hotelrooms */ $this->breadcrumbs=array( 'hotel rooms'=>array('index'), $model->id, ); $this->menu=array( array('label'=>'list hotelrooms', 'url'=>array('index')), array('label'=>'create hotelrooms', 'url'=>array('create')), array('label'=>'update hotelrooms', 'url'=>array('update', 'id'=>$model->id)), array('label'=>'delete hotelrooms', 'url'=>'#', 'linkoptions'=>array('submit'=>array('delete','id'=>$model->id),'confirm'=>'are sure want delete item?')), array('label'=>'manage hotelrooms', 'url'=>array('admin')), ); ?> <h1>view room : <?php echo $model->id; ?></h1> <?php $this->widget('zii.widgets.cdetailview', array( 'data'=>$model, 'data'=>$model2, 'attributes'=>array( 'id', 'hotelname', 'hotelroomstype', .... ), )); ?>
the structure oh tables
hotel_rooms id hotelname hotelroomstype ..... hotel_rooms_checked id datecheckedin datecheckedout ....
well, question seems not clear enough. try guess want though.
for view
, add fields other model
(i suppose have created model hotel_rooms_checked
)
<?php /* @var $this propunitcontroller */ /* @var $model hotelrooms */ /* @var $model2 hotelroomschecked */ /* @var $form cactiveform */ ?> <div class="form"> <?php $form=$this->beginwidget('cactiveform', array( 'id'=>'hotel-rooms-form', 'enableajaxvalidation'=>false, )); ?> <p class="note">fields <span class="required">*</span> required.</p> <?php echo $form->errorsummary(array($model, $model2)); ?> <div class="row"> <?php echo $form->labelex($model,'id'); ?> <?php echo $form->textfield($model,'id',array('size'=>60,'maxlength'=>255)); ?> <?php echo $form->error($model,'id'); ?> </div> <!--for example, let's have attribute `status` hotelroomschecked--> <div class="row"> <?php echo $form->labelex($model2,'status'); ?> <?php echo $form->textfield($model2,'status',array('size'=>60,'maxlength'=>255)); ?> <?php echo $form->error($model2,'status'); ?> </div> ....... <?php $this->endwidget(); ?> </div><!-- form -->
and controller, perform validation both models before saving.
public function actionupdate($id) { $model=$this->loadmodel($id); $model2 = hotelroomschecked::model()->findbypk($id); // uncomment following line if ajax validation needed // $this->performajaxvalidation($model); if(isset($_post['hotelrooms']) && isset($_post['hotelroomschecked'])) { $model->attributes=$_post['hotelrooms']; $model2->attributes = $_post['hotelroomschecked']; $valid = $model->validate(); $valid = $valid && $model2->validate(); if($valid) { // $this->redirect(yii::app()->request->urlreferrer); $model->save(); $model2->save(); $this->redirect(array('hotels/index','id'=>$model->id)); } } $this->render('update',array( 'model'=>$model, 'model2' => $model2, )); }
Comments
Post a Comment