Php 从一个模型获取数据并存储在另一个表中
我是Yii的新手。我正在从模型中获取数据。我必须从表单中获取这些数据,并将开始、结束日期存储在另一个表中。我把代码附在这里了。我无法从此表单中获取值。请帮我做这件事 我的看法是:Php 从一个模型获取数据并存储在另一个表中,php,yii,Php,Yii,我是Yii的新手。我正在从模型中获取数据。我必须从表单中获取这些数据,并将开始、结束日期存储在另一个表中。我把代码附在这里了。我无法从此表单中获取值。请帮我做这件事 我的看法是: <?php $host_start_date = ""; $host_start_date = $model->host_start_date; if ($host_start_date != "") { $host_start_date = date("d-m-Y", strtotime($host_st
<?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>
<div class="row buttons">
<div> </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>
请帮帮我。提前感谢。您可以尝试此代码
在您的表单代码中,我对日期小部件做了一些更改
<?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>
<div class="row buttons">
<div> </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 $host_start_date;
希望对你有帮助
你也可以回顾一下我的问题
谢谢无法获取数据?正在提交表单吗?要在哪个表中存储开始和结束日期?您是否为该表创建了模型?此日期字段将被提取并存储到不同的表中。你的代码也这么做了吗?它给了我这个错误“属性”nimsofhost.host\u start\u date“未定义”。是的,我更改了型号名称否,让我清楚地描述一下。我的模型中没有字段列。但是从这个模型的角度来看,我必须知道日期。这些日期将存储在另一个表中。那么我应该怎么做?我应该在哪个模型中定义?
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;