我有一个CListView,其中我添加了一个表单来上传图像。这样每个项目都有自己的文件上载..
表单有一个发布请求的ajaxLink。我在accessRule中添加了'foto'。我的控制器被请求到达,没有错误。仅发布文件且未将数据保存在我的数据库中。
我认为由于视图中的表单,它无法获取uploadfile的实例'image'。我是对的,我该如何解决这个问题?
CListView _view:
<div class="view">
<!--more content and..-->
<?php
$model=new Nieuws;
echo $this->renderPartial('_plaatje', array('model'=>$model, 'itemId'=>$data->id)); ?>
</div>
形式:
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'plaatjes-form',
'enableAjaxValidation'=>false,
'htmlOptions' => array(
'enctype' => 'multipart/form-data',
),
)); ?>
<div class="row">
<?php echo $form->labelEx($model,'pad'); ?>
<?php echo CHtml::activeFileField($model, 'image'); ?>
<?php echo $form->error($model,'pad'); ?>
</div>
<br />
<div class="row">
<?php echo $form->labelEx($model,'ondertitel'); ?>
<?php echo $form->textField($model,'ondertitel',array('size'=>60,'maxlength'=>100)); ?>
<?php echo $form->error($model,'ondertitel'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'url'); ?>
<?php echo $form->textField($model,'url',array('size'=>60,'maxlength'=>100)); ?>
<?php echo $form->error($model,'url'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
型号:
class Nieuws extends CActiveRecord
{
public $image;
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'nieuws';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('titel, tekst, verwijderd', 'numerical', 'integerOnly'=>true),
array('foto', 'length', 'max'=>100),
array('datum, verwijderDatum', 'safe'),
array('image', 'file', 'types'=>'jpg, gif, png', 'allowEmpty'=>true, 'on'=>'foto'),
array('image', 'length', 'max'=>255, 'on'=>'foto'),
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
array('id, titel, datum, tekst, foto, verwijderd, verwijderDatum', 'safe', 'on'=>'search'),
);
}
}
控制器:
public function actionFoto($id)
{
$model=$this->loadModel($id);
if(isset($_POST['Foto']))
{
$model->foto=CUploadedFile::getInstance($model,'image');
if($model->save())
{
$model->foto->saveAs('uploads/');
}
}
}