Reputation: 285
I have created one form to eddit one table(hotel_rooms) in my database from my website page, it works perdectcly.
But I need to include some fields,not all the table, from another table (hotel_rooms_checked)in the edditing web page of the privious table (hotel_rooms)
how can I do it please ? where hotel_rooms.Id = hotel_rooms_checked.Id
here is the code for the 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 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',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'Id'); ?>
</div>
.......
<?php $this->endWidget(); ?>
</div><!-- form -->
and this is the controller code
public function actionUpdate($id)
{
$model=$this->loadModel($id);
// Uncomment the following line if AJAX validation is 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 you sure you want to delete this 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 the tables
Hotel_Rooms
Id
HotelName
HotelRoomsType
.....
Hotel_Rooms_Checked
Id
DateCheckedIn
DateCheckedOut
....
Upvotes: 2
Views: 1153
Reputation: 809
Well, the question seems to be not clear enough. I would try to guess what you want though.
For the view
, just add the fields for the other model
(I suppose you have created a model for 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 with <span class="required">*</span> are 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 say we have an attribute `status` for 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 now for the controller, you could perform the validation for both models before actually saving.
public function actionUpdate($id)
{
$model=$this->loadModel($id);
$model2 = HotelRoomsChecked::model()->findByPk($id);
// Uncomment the following line if AJAX validation is 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,
));
}
Upvotes: 5
Reputation: 79113
Simply pass both models from the controller:
public function actionUpdate($id)
{
$model=$this->loadModel($id);
// assuming second model has same ID. Modify $id accordingly
$model2=HotelRoomsChecked::model()->findByPk($id);
if(isset($_POST['HotelRooms']) && isset($_POST['HotelRoomsChecked'])) //!
{
$model->attributes=$_POST['HotelRooms'];
$model2->attributes=$_POST['HotelRoomsChecked']; //!
if($model->save() && $model2->save()) //!
$this->redirect(array('hotels/index','id'=>$model->Id));
}
$this->render('update',array(
'model'=>$model,
'model2'=>$model2, //!
));
}
And simply use the second model in your form the same way that you have posted in your question.
Upvotes: 0