从一个模型获取数据并存储在另一个表中

时间:2014-02-25 07:03:56

标签: php yii

我是Yii的新手。我从模型中获取数据。我必须从表单中获取这些数据并存储DATE start,在另一个表中结束。我在这里附上了代码。我无法从此表单中获取值。请帮助我。

我的观点:

<?php
$host_start_date = "";
$host_start_date = $model->host_start_date;
if ($host_start_date != "") {
$host_start_date = date("d-m-Y", strtotime($host_start_date));
}

$host_end_date = "";
$host_end_date = $model->host_end_date;
if ($host_end_date != "") {
$host_end_date = date("d-m-Y", strtotime($host_end_date));
}

?>
<div id="content">
<div class="innerLR">
<div class="row-fluid">
<div class="form">

<?php
echo $id = $id;
$form = $this->beginWidget ( 'CActiveForm', array (
        'id' => 'nimsoft-host-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 
) );
?>

    <fieldset>
    <legend>Customer Host Information:</legend>
        <?php echo $form->errorSummary($model); ?>

            <div id="add_details">
        <tr>
        <td style="text-align: left;" class="tdSpan">
                    <?php echo $form->labelEx($model, 'host_name'); ?>
                </td>
            <td class="tdSpan">
                <div class="row">
                                    <?php echo $form->textField($model, 'host_name', array('size' => 60, 'maxlength' => 88)); ?>
                                </div>
            </td>
        </tr>
                <tr>
            <td style="text-align: left;" class="tdSpan">
                        <?php echo $form->labelEx($model, 'host_serviceid'); ?>
                        </td>
            <td class="tdSpan">
                <div class="row">
                                    <?php echo $form->textField($model, 'host_serviceid', array('rows' => 6, 'cols' => 50)); ?>
                                </div>
                        </td>
        </tr>

        <tr class="tdSpan">
            <td></td>
            <td>
                            <div class="row">
                                <?php echo $form->labelEx($model,'status'); ?>
                                <?php echo $form->radioButtonList($model, 'status', array('Enable'=>'Enable', 'Disable'=>'Disable')); ?>
                                <?php echo $form->error($model,'status'); ?>
                            </div>

                            <div class="row">
        <?php echo $form->labelEx($model,'host_start_date'); ?>
        <?php
            $this->widget('zii.widgets.jui.CJuiDatePicker', array(
            'attribute' => 'host_start_date',
            'model' => $model,
            'options' => array(
            //'mode' => 'focus',
            'dateFormat' => 'yyyy-mm-dd',
            'showAnim'=>'fold',
            ),
            'htmlOptions' => array(
            'style'=>'height:20px;',
            'value' => $host_start_date,
           // 'readonly' => 'readonly'
            ),
            ));
            ?>
                <?php echo $form->error($model,'host_start_date'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'host_end_date'); ?>
        <?php
            $this->widget('zii.widgets.jui.CJuiDatePicker', array(
            'attribute' => 'host_end_date',
            'model' => $model,
            'options' => array(
            //'mode' => 'focus',
            'dateFormat' => 'yyyy-mm-dd',
            'showAnim'=>'fold',
            ),
            'htmlOptions' => array(
            'style'=>'height:20px;',
            'value' => $host_end_date,
            //'readonly' => 'readonly'
            ),
            ));
            ?>
                  <?php echo $form->error($model,'host_end_date'); ?>

    </div>
            </div> 
                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <div class="row buttons">
                                    <div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div>
                                    <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save', array('onclick' => 'return checkForm();')); ?>
                                </div>
                        </td>
                    </tr>
                                        </tbody>
                    </table>
    <?php $this->endWidget(); ?>
    </fieldset>
</div>
</div>
</div>
</div>

<div id="footer" class="hidden-print">
    <?php $this->renderPartial('application.views.layouts._footer_inc'); ?>
</div>

我的控制器:

public function actionCreate($id)
    {
        $model=new NimsoftHost;
                // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);
                $host_date = new NimsoftHostsDetails;
                $cust_id=$id;
                $criteria = new CDbCriteria;
                $cust_name = MasterCustomers::model()->findByPk($cust_id);

                if(isset($_POST['NimsoftHost']))
        {
                    $model->attributes=$_POST['NimsoftHost'];
                        $model->host_customer_id=$id;
                        echo "I am here";
                        if($model->save())
                        {
                            $host_date->host_start_date = $model->host_start_date ;
                            $host_date->host_end_date = $model->host_end_date ;
                            echo $host_date->host_id = $model->id ;
                                                        die();

                            if($host_date->save()) 
                                {
                                $this->redirect(array('view','id'=>$model->host_id));
                            }
                         }
        }

        $this->render('create',array(
            'model'=>$model,'cust_id'=>$cust_id,'cust_name'=>$cust_name->cust_name
        ));
    }

请帮帮我。提前谢谢。

1 个答案:

答案 0 :(得分:0)

您尝试使用此代码..

在您的表单代码中,我对您的日期窗口小部件进行了一些更改。

<?php
$prod_end_date = "";
$prod_end_date = $model->prod_end_date;
if ($prod_end_date != "") {
$prod_end_date = date("d-m-Y", strtotime($prod_end_date));
}

$prod_avl_date = "";
$prod_avl_date = $model->prod_avl_date;
if ($prod_avl_date != "") {
$prod_avl_date = date("d-m-Y", strtotime($prod_avl_date));
}

?>


    <div id="content">
    <div class="innerLR">
    <div class="row-fluid">
    <div class="form">

    <?php
    echo $id = $id;
    $form = $this->beginWidget ( 'CActiveForm', array (
    'id' => 'nimsoft-host-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 
    ) );
    ?>

    <fieldset>
    <legend>Customer Host Information:</legend>
    <?php echo $form->errorSummary($model); ?>

    <div id="add_details">
    <tr>
    <td style="text-align: left;" class="tdSpan">
    <?php echo $form->labelEx($model, 'host_name'); ?>
    </td>
    <td class="tdSpan">
    <div class="row">
    <?php echo $form->textField($model, 'host_name', array('size' => 60, 'maxlength' => 88)); ?>
    </div>
    </td>
    </tr>
    <tr>
    <td style="text-align: left;" class="tdSpan">
    <?php echo $form->labelEx($model, 'host_serviceid'); ?>
    </td>
    <td class="tdSpan">
    <div class="row">
    <?php echo $form->textField($model, 'host_serviceid', array('rows' => 6, 'cols' => 50)); ?>
    </div>
    </td>
    </tr>

    <tr class="tdSpan">
    <td></td>
    <td>
    <div class="row">
    <?php echo $form->labelEx($model,'status'); ?>
    <?php $status=array("Enable","Disable")?>
    <?php echo $form->radioButtonList($model,'status',$status,array('separator'=>'')); ?>
    <?php echo $form->error($model,'status'); ?>
    </div>

    <div class="row">
    <?php echo $form->labelEx($model,'host_start_date'); ?>
    <?php
            $this->widget('zii.widgets.jui.CJuiDatePicker', array(
            'attribute' => 'prod_avl_date',
            'model' => $model,
            'options' => array(
            'mode' => 'focus',
            'dateFormat' => 'dd-mm-yy',
            'showAnim' => 'slideDown',
            ),
            'htmlOptions' => array(
            'style'=>'height:20px;',
            'value' => $prod_avl_date,
            'readonly' => 'readonly'
            ),
            ));
            ?>
    <?php echo $form->error($model,'host_start_date'); ?>
    </div>

    <div class="row">
    <?php echo $form->labelEx($model,'host_end_date'); ?>
    <?php
            $this->widget('zii.widgets.jui.CJuiDatePicker', array(
            'attribute' => 'prod_end_date',
            'model' => $model,
            'options' => array(
            'mode' => 'focus',
            'dateFormat' => 'dd-mm-yy',
            'showAnim' => 'slideDown',
            ),
            'htmlOptions' => array(
            'style'=>'height:20px;',
            'value' => $prod_end_date,
            'readonly' => 'readonly'
            ),
            ));
            ?>
    <?php echo $form->error($model,'host_end_date'); ?>

    </div>
    </div> 
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <div class="row buttons">
    <div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div>
    <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save', array('onclick' => 'return checkForm();')); ?>
    </div>
    </td>
    </tr>
    </tbody>
    </table>
    <?php $this->endWidget(); ?>
    </fieldset>
    </div>
    </div>
    </div>
    </div>

    <div id="footer" class="hidden-print">
    <?php $this->renderPartial('application.views.layouts._footer_inc'); ?>
    </div>

现在您想将日期存储在其他表中。 所以我在你的控制器上做了一些改动。看一看。

 public function actionCreate($id)
    {
        $model=new NimsoftHost;
                // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);
                $host_date = new NimsoftHostsDetails;
                $cust_id=$id;
                $criteria = new CDbCriteria;
                $cust_name = MasterCustomers::model()->findByPk($cust_id);

                if(isset($_POST['NimsoftHost']))
        {
                    $model->attributes=$_POST['NimsoftHost'];
                        $model->host_customer_id=$id;
                        $host_date->host_start_date = $model->host_start_date ;
                        $host_date->host_end_date = $model->host_end_date ;
                        $host_date->host_id = $model->id ;

                        if($model->save() AND $host_date->save())
                        {
                                $this->redirect(array('view','id'=>$model->host_id));
                        }
        }

        $this->render('create',array(
            'model'=>$model,'cust_id'=>$cust_id,'cust_name'=>$cust_name->cust_name
        ));
    }

更新模型类。

在你的模型类中,将访问修饰符定义为这样的属性。

public $host_start_date;

希望它对你有所帮助。

您也可以查看我的问题。 Insert data in two different tables from single Controller in Yii

由于