如何在Yii中以一种形式修改两个数据库表

Mus*_*oom 2 php database forms yii

我已经从我的网站页面创建了一个表格来编辑我的数据库中的一个表(hotel_rooms),它可以正常工作.但我需要在私人表(hotel_rooms)的edditing网页中的另一个表(hotel_rooms_checked)中包含一些字段,而不是所有表格,我该怎么办呢?其中,hotel_rooms.Id = hotel_rooms_checked.Id 这里是表单代码

    <?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 -->
Run Code Online (Sandbox Code Playgroud)

这是控制器代码

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,
        ));
    }
Run Code Online (Sandbox Code Playgroud)

视图代码是

<?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',
        ....
    ),
)); ?>
Run Code Online (Sandbox Code Playgroud)

桌子的结构哦

Hotel_Rooms
Id
HotelName
HotelRoomsType
.....

Hotel_Rooms_Checked
Id
DateCheckedIn
DateCheckedOut
....
Run Code Online (Sandbox Code Playgroud)

d.y*_*yuk 5

嗯,问题似乎不够明确.我会尽量猜猜你想要什么.

对于view,只需为另一个添加字段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 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 -->
Run Code Online (Sandbox Code Playgroud)

现在,对于控制器,您可以在实际保存之前对两个模型执行验证.

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,
        ));
    }
Run Code Online (Sandbox Code Playgroud)