如何从Yii的前一个视图中获取价值

时间:2014-09-05 17:08:33

标签: php yii

我不确定如何描述我的问题。首先,我在CCGridview上添加了一个按钮:

array(
    'class'=>'CButtonColumn',
    'template' => '{view}{update}{delete}{upload_image}',
    'buttons' => array(
        'upload_image' => array(
        'label' => 'upload foto',
        'url' => 'Yii::app()->createUrl("/image/create",
        array("product_id" => $data->product_id))',
        ),
    ),
),

当我点击它时会将我带到具有product_id值的/ image / create视图。例如,在该gridview上,我单击了记录号7,因此URL将是:

  

(web应用)/index.php/image/create?product_id=7

由于它呈现部分_form,因此表单具有根据具有以下属性的图像表的属性:id,title,filename,product_id。

所以视图将是这样的:

<div class="row">
    <?php echo $form->labelEx($model,'title'); ?>
    <?php echo $form->textField($model,'title',array('size'=>45,'maxlength'=>45)); ?>
    <?php echo $form->error($model,'title'); ?>
</div>

<div class="row">
    <?php echo $form->labelEx($model,'filename'); ?>
    <?php echo $form->fileField($model,'filename',array('size'=>45,'maxlength'=>45)); ?>
    <?php echo $form->error($model,'filename'); ?>
</div>

<div class="row">
    <?php echo $form->labelEx($model,'product_id'); ?>
    <?php echo $form->textField($model,'product_id'); ?>
    <?php echo $form->error($model,'product_id'); ?>
</div>

<div class="row buttons">
    <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>

我的问题是,我们如何使用我之前提到的url中的值7(../ create?product_id = 7)到product_id属性中,而不必在提供的textField中输入它?

换句话说,我将从视图中删除它:

<div class="row">
    <?php echo $form->labelEx($model,'product_id'); ?>
    <?php echo $form->textField($model,'product_id'); ?>
    <?php echo $form->error($model,'product_id'); ?>
</div>

但是当我提交时,应该在product_id字段传递/保存值(7)的表单。

添加了: 我的控制器actionCreate是

//...
public function actionCreate()
{
            $dir = Yii::app()->basePath . '/../productimages/';
            $uploaded = false;
            $model=new Image();

            if(isset($_POST['Image']))
            {
                $model->attributes=$_POST['Image'];
                $tempSave=CUploadedFile::getInstance($model,'filename');
                if($model->validate())
                {
                    $uploaded = $tempSave->saveAs($dir.'/'.$tempSave->getName());
                    $this->redirect(array('/products/index'));
                }
            }
            $this->render('index', array(
                'model' => $model,
                'uploaded' => $uploaded,
                'dir' => $dir,
            ));
}

就是这样。非常感谢..

1 个答案:

答案 0 :(得分:1)

以这种方式修改你的控制器:

if(isset($_POST['Image']))
{
        $model->attributes=$_POST['Image'];
        $tempSave=CUploadedFile::getInstance($model,'filename');
        if($model->validate())
        {
            $uploaded = $tempSave->saveAs($dir.'/'.$tempSave->getName());
            $this->redirect(array('/products/index'));
        }
} else {
   //initialize defaults
   $model->product_id=intval($_GET['product_id']);
}