【问题标题】:How to load multiple models in update controller如何在更新控制器中加载多个模型
【发布时间】:2013-07-10 17:00:07
【问题描述】:

在 _form.php 文件中,我有来自两个不同模型的文本框,我对 create 控制器进行了更改以插入两个模型的记录,但问题出在 update 控制器中,如何加载第二个模型的数据在第二个模型的文本框中?
只会填充与第一个模型相关的文本框。

【问题讨论】:

标签: php yii


【解决方案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 [copy of _form.php] 页面并从您的 update.php 调用 render _formUpdate.php 而不是 _form.php 并进行了上述更改

【讨论】:

  • 我尝试使用上面的建议但是地址文本框中没有填写数据
  • 很高兴知道这一点。 :) 如果它真的有效,请将其标记为答案,谢谢
  • 如果你清楚地检查了我在 _form.php 中显示 Table2 所做的工作&lt;?php echo $form-&gt;labelEx($model2, ‘data2’); ?&gt;
【解决方案2】:

_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 -->

更新.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;
    }

【讨论】:

  • 我尝试使用 zzlalani 的建议,但文本框中没有填写数据
  • 请查看我的评论
猜你喜欢
  • 1970-01-01
  • 2021-06-01
  • 1970-01-01
  • 2011-09-08
  • 1970-01-01
  • 1970-01-01
  • 2018-06-09
  • 2018-07-05
  • 2014-04-19
相关资源
最近更新 更多