将信息存储到数据库中,而图像存储在Yii中的文件夹中

将信息存储到数据库中,而图像存储在Yii中的文件夹中,yii,Yii,我想做的是存储用户的信息(id、姓名、照片)。Id和名称将存储在数据库中,而照片则存储在images文件夹中。 我设法将照片存储到文件夹中,但奇怪的是,其他信息并没有存储到数据库中。我做错了什么?请指出我的错误! Tq 这是密码 views/_form.php <?php /* @var $this PhotoController */ /* @var $model Photo */ /* @var $form CActiveForm */ ?> <div class="

我想做的是存储用户的信息(id、姓名、照片)。Id和名称将存储在数据库中,而照片则存储在images文件夹中。 我设法将照片存储到文件夹中,但奇怪的是,其他信息并没有存储到数据库中。我做错了什么?请指出我的错误! 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 -->

带*的字段是必需的

models/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,
    ));
}
}

$model->save不起作用,因为验证时某些条件可能会失败

$model->foto未保存

你必须把钱存起来

$model->attributes=$_POST['Photo'];

$model->foto = CUploadedFile::getInstance($model, 'foto');
//do all your upload things over here
$model->save(false);

据我所知,我认为这个
$this->refresh()正在产生问题。请尝试对此行添加注释,然后重试,因为$this->refresh刷新当前页面。此方法调用的效果与用户按浏览器上的刷新按钮的效果相同(无post数据)。希望这会有所帮助。

您可以打开日志记录,查看日志中是否有错误。
$model->save()
是否返回true或false?
$model->attributes=$_POST['Photo'];

$model->foto = CUploadedFile::getInstance($model, 'foto');
//do all your upload things over here
$model->save(false);