我想要的是存储用户的信息(身份证,姓名,照片)。当照片存储在images文件夹中时,ID和名称将存储在数据库中。 我设法将照片存储到文件夹中,但奇怪的是其他信息没有存储到数据库中。我做错了什么?请指出我! TQ 这是代码 views / _form.php
<?php
/* @var $this PhotoController */
/* @var $model Photo */
/* @var $form CActiveForm */
?>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'photo-form',
'enableAjaxValidation'=>false,
'htmlOptions'=>array('enctype'=>'multipart/form-data'),
'clientOptions'=>array('validateOnSubmit'=>true,),
)); ?>
<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'=>32,'maxlength'=>32)); ?>
<?php echo $form->error($model,'name'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'foto'); ?>
<?php echo $form->fileField($model,'foto'); ?>
<?php echo $form->error($model,'foto'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
模型/ Photo.php
<?php
/**
* This is the model class for table "tbl_photo".
*
* The followings are the available columns in table 'tbl_photo':
* @property integer $id
* @property string $name
*/
class Photo extends CActiveRecord{
public $foto;
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return Photo the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'tbl_photo';
}
/**
* @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('name', 'required'),
array('name', 'length', 'max'=>32),
array('foto', 'file', 'types'=>'jpg, jpeg, gif, png'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, name', 'safe', 'on'=>'search'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'name' => 'Name',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('name',$this->name,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
}
控制器/ PhotoController.php
<?php
class PhotoController extends Controller{
...
...
/**
* Creates a new model.
* If creation is successful, the browser will be redirected to the 'view' page.
*/
public function actionCreate(){
$model=new Photo;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Photo'])){
$model->attributes=$_POST['Photo'];
if(isset($_FILES) && $_FILES['Photo']['error']['foto']==0){
$uploadedFile = CUploadedFile::getInstance($model, 'foto');
if($uploadedFile->getExtensionName()=="jpeg" || $uf->getExtensionName()=="png" || $uf->getExtensionName()=="gif"){
$uploadedFile->saveAs(Yii::getPathOfAlias('webroot').'/images/'.$uploadedFile->getName());
Yii::app()->user->setFlash('foto','/images/'.$uploadedFile->getName());
$this->refresh();
}
}
if($model->save())$this->redirect(array('view','id'=>$model->id));
}
$this->render('create',array('model'=>$model,));
}
}
答案 0 :(得分:0)
$ model-&gt;保存不会工作,因为验证时某些条件可能会失败。
$ model-&gt; foto未保存
你必须保存为
$model->attributes=$_POST['Photo'];
$model->foto = CUploadedFile::getInstance($model, 'foto');
//do all your upload things over here
$model->save(false);
答案 1 :(得分:0)
据我所知,我认为这$this->refresh();
正在制造问题。尝试对此行进行评论,然后重试,因为 $ this-&gt; refresh 刷新当前页面。此方法调用的效果与用户按下浏览器(没有发布数据)上的刷新按钮的效果相同。希望这会有所帮助。