在 _form.php 文件中,我有来自两个不同模型的文本框,我对create
控制器进行了更改以插入两个模型的记录,但问题出在update
控制器中,如何将第二个模型的数据加载到第二个模型的文本框中?
只有与第一个模型相关的文本框才会被填充。
问问题
183 次
2 回答
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)); ?>
希望这可以帮助。
编辑
由于您可能使用相同的 _form.php 页面进行create
查看,因此您需要创建另一个 _form.php 文件,例如_formUpdate.php [_form.php 的副本] 页面并从您的update.php调用渲染 _formUpdate.php 而不是 _form .php 并进行了上述更改
于 2013-07-12T08:10:27.807 回答
0
_形式
<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 -->
更新.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)); ?>
表1控制器.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;
}
于 2013-07-12T17:39:10.777 回答