在yii2.0中,图像保存在文件夹中,但未保存在数据库中

在yii2.0中,图像保存在文件夹中,但未保存在数据库中,yii2,Yii2,我是yii新手,我经常在数据库中上传图像,但图像存储在文件夹中,但不保存在数据库中。这段代码有什么问题 public function actionCreate() { $model = new Jobs; // $site_url=Yii::$app->getUrlManager()->createAbsoluteUrl(''); if ($model->load(Yii::$app->reques

我是yii新手,我经常在数据库中上传图像,但图像存储在文件夹中,但不保存在数据库中。这段代码有什么问题

   public function actionCreate()
    {

        $model = new Jobs;


    //  $site_url=Yii::$app->getUrlManager()->createAbsoluteUrl('');

            if ($model->load(Yii::$app->request->post()) && $model->save()) {

            $image = UploadedFile::getInstance($model,'image');

            $imagepath='upload/jobs/';

            $path=$imagepath.rand(10,100).$image->name;

            if(!empty($image)){
                $image->saveAs($path);
                $model->image=$image->name;
                $model->save();
            }               
                return $this->redirect(['view', 'id' => $model->id]);
            }                       
        else {

           // echo "file not ulpoaded";
        }

     return $this->render('create', [
                    'model' => $model,
            ]);
    }
模型在这里命名为Jobs.php。
namespace app\models;

use Yii;
use yii\web\UploadedFile;
use yii\base\Model;

/**
 * This is the model class for table "jobs".
 *
 * @property integer $id
 * @property integer $users_id
 * @property string $title
 * @property string $deadline
 * @property string $urgency
 */

class Jobs extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     * 
     * 
     */
    public $image;

    public static function tableName()
    {
        return 'jobs';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['users_id', 'title', 'deadline', 'urgency'], 'required'],
            [['users_id'], 'integer'],
            [['content'], 'string'],
            [['deadline'], 'required'],
            [['urgency'], 'string'],
            [['urgency'], 'safe'],
            [['image'],'file', 'skipOnEmpty'=>true,'extensions' => 'png,gif,jpg'],
            [['title'], 'string', 'max' => 255]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'users_id' => Yii::t('app', 'Users ID'),
            'content'=>Yii::t('app','Content'),
            'title' => Yii::t('app', 'Title'),
            'deadline' => Yii::t('app', 'Deadline'),
            'urgency' => Yii::t('app', 'Urgency'),
            'image' => Yii::t('app', 'image'),
        ];
    }

}
视图文件在这里命名为(_form.php)
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\web\UploadedFile;
//use kartik\widgets\FileInput;

/* @var $this yii\web\View */
/* @var $model app\models\Jobs */
/* @var $form yii\widgets\ActiveForm */


?>
<div class="jobs-form">
<?php 

   $form = ActiveForm::begin([

        'options' => ['enctype' => 'multipart/form-data']]); ?>

    <?= $form->field($model, 'users_id')->textInput() ?>

    <?= $form->field($model, 'content')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'deadline')->textInput() ?>

    <?= $form->field($model, 'urgency')->dropDownList([ 'No', 'yes', ], ['prompt' => '']) ?>


    <?= $form->field($model, 'image')->fileInput() ?>

    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>


    enter code here
使用yii\helpers\Html;
使用yii\widgets\ActiveForm;
使用yii\web\UploadedFile;
//使用kartik\widgets\FileInput;
/*@var$this yii\web\View*/
/*@var$model app\models\Jobs*/
/*@var$form yii\widgets\ActiveForm*/
?>
在这里输入代码

plzz帮助我上传数据库中的图像我不知道这个代码有什么问题

您在代码中混合了图像名称和图像文件。 如果数据库表中的字段是
image
,则不需要使用
public$image
。而是在模型中添加
public$imageFile
,并相应地更新表单和控制器

请记住,
$this->image
,或
$model->image
是图像的名称,作为一个字符串,您正在混合一个上载对象。而是创建另一个变量,以便可以将上载的图像存储在中,并使用图像变量保存实际名称以供以后参考

看看你是否能弄明白,如果你不能让它工作,更新你的问题


更改上传代码,如下所示:

 if ($model->load(Yii::$app->request->post()) && $model->save()) {

            $image = UploadedFile::getInstance($model,'image');

            $imagepath='upload/jobs/';

            $rand_name=rand(10,100);

            if ($image)
            {
                $model->image = "category_{$rand_name}-{$image}"; 
            }

                if($model->save()):
                    if($image):
                     $image->saveAs($imagepath.$model->image);
                    endif;
                endif;             
                return $this->redirect(['view', 'id' => $model->id]);
            }