在_form.php文件中我有来自两个不同模型的文本框,我已对create
控制器进行了更改以插入两个模型的记录,但问题出在update
控制器中,如何加载第二个模型的文本框中的第二个模型的数据?
只会填充与第一个模型相关的文本框。
答案 0 :(得分:1)
只需将两个模型渲染到控制器中的update
视图即可。
$this->render('update', array(
'model1' => $model1,
'model2' => $model2,
));
并在_form.php
中调用类似这样的文本框
<?php echo $form->labelEx($model1, ‘data1’); ?>
<?php echo $form->textField($model1, ‘data1’, array(‘size’ => 60, ‘maxlength’ => 250)); ?>
<?php echo $form->labelEx($model2, ‘data2’); ?>
<?php echo $form->textField($model2, ‘data2’, array(‘size’ => 60, ‘maxlength’ => 250)); ?>
希望这会有所帮助。
修改强>
由于您可能正在为create
视图使用相同的_form.php页面,因此您需要创建另一个_form.php文件,例如 _formUpdate.php [_form.php的副本]页面并从您的 update.php 调用渲染_formUpdate.php而不是_form.php并进行上述更改
答案 1 :(得分:0)
_form
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'tabel1-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,'name'); ?>
<?php echo $form->textField($model,'name',array('size'=>44,'maxlength'=>44)); ?>
<?php echo $form->error($model,'name'); ?>
</div>
<div class="row">
<?php echo $form->labelEx(Table2::model(),'address'); ?>
<?php echo $form->textField(Table2::model(),'address',array('size'=>44,'maxlength'=>44)); ?>
<?php echo $form->error(Table2::model(),'address'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
update.php
<?php
$this->breadcrumbs=array(
'Tabel1s'=>array('index'),
$model->name=>array('view','id'=>$model->id),
'Update',
);
$this->menu=array(
array('label'=>'List Tabel1', 'url'=>array('index')),
array('label'=>'Create Tabel1', 'url'=>array('create')),
array('label'=>'View Tabel1', 'url'=>array('view', 'id'=>$model->id)),
array('label'=>'Manage Tabel1', 'url'=>array('admin')),
);
?>
<h1>Update Tabel1 <?php echo $model->id; ?></h1>
<?php echo $this->renderPartial('_formUpdate', array('model'=>$model,'model2'=>$model2)); ?>
Tabel1Controller.php
public function actionCreate()
{
$model=new Tabel1;
$model2=new Table2;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Tabel1'])&&isset($_POST['Table2']))
{
$model->attributes=$_POST['Tabel1'];
$model2->attributes=$_POST['Table2'];
if($model->save()&&$model2->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('create',array(
'model'=>$model,
'model2'=>$model2
));
}
/**
* Updates a particular model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id the ID of the model to be updated
*/
public function actionUpdate($id)
{
$model=$this->loadModel($id);
$model2=$this->loadModel2($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
//echo $model2->address;
if(isset($_POST['Tabel1'])&&isset($_POST['Table2']))
{
$model->attributes=$_POST['Tabel1'];
$model2->attributes=$_POST['Table2'];
if($model->save()&&$model2->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('update',array(
'model'=>$model,
'model2'=>$model2
));
}
public function loadModel2($id)
{
$model=Table2::model()->findByPk(1);
if($model===null)
throw new CHttpException(404,'The requested page does not exist.');
return $model;
}