i am new in yii and i use to upload images in databse but images are stored in folder but not saved in database what is the problem with this code coud not undurstand
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,
]);
}
model is here named as 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'),
];
}
}
the view file is here named as (_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
plzz help me to upload the image in database i dont know what the problem with this code.