Yii2 modelda rasm va dbga malumot kiritish

+1 ovoz
54 marta ko‘rilgan
so‘radi 17 sentabr, 18 Blackstar_valley (50 bal)
Yii2da rasm papkaga va Shu bilan birga dbga malumot yozmoqchiman. Ilojisi bosa ozila qigan proyektdan tawela. Guideda faqat filenikini bergan shuni ulabberila

1 ta javob

0 ovoz
javob berdi 18 sentabr, 18 MrHope (765 bal)
tanlangan 18 sentabr, 18 Blackstar_valley
 
Eng yaxshi javob
yii2 advanced uchun. frontend da web ni ichida images papkasi ochiladi

Controller

namespace backend\controllers;

use yii\web\UploadedFile;

.......

public function actionCreate()
    {
        $model = new Rasm();

        if ($model->load(Yii::$app->request->post())) {
            $model->img = UploadedFile::getInstance($model,'img');
            $name = md5($model->name).'.'.$model->img->extension;
            $model->img->saveAs(realpath(dirname(__FILE__)).'/../../frontend/web/images/'.$name);
            $model->img = $name;
            if($model->save(false)){
                return $this->redirect(['view', 'id' => $model->id]);
            }else{
                return print_r($model->errors);
            }
            } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

Viev:

<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;

?>

<div class="banner-form">

    <?php $form = ActiveForm::begin(['options'=>['enctype'=>'multipart/form-data']]); ?>

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

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

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

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

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

</div>

Model:

<?php

namespace common\models;

use Yii;

class Banner extends \yii\db\ActiveRecord
{
    public static function tableName()
    {
        return 'banner';
    }

    public function rules()
    {
        return [
            [['name', 'img'], 'required'],
            [['name', 'img', 'url'], 'string', 'max' => 255],
            [['img'],'file','skipOnEmpty' => false, 'extensions' => 'png, jpg, gif, jpeg'],
        ];
    }

    public function attributeLabels()
    {
        return [
            'id' => Yii::t('main', 'ID'),
            'name' => Yii::t('main', 'Name'),
            'img' => Yii::t('main', 'Img'),
            'url' => Yii::t('main', 'Url'),
        ];
    }
}

Tushunishga harakat qilib ko'ring . Tushunmagan joyiz bo'lsa murojaat qiling. tushuntirishga harakat qilaman
izoh qoldirdi 18 sentabr, 18 Blackstar_valley (50 bal)
Kotta rahmat hammasini tushundim!

Assalomu alaykum, yordam.uz saytimizga xush kelibsiz.

Bu saytda o`zingizni qiziqtirgan savollarga javob olishingiz va o`z sohangiz bo`yicha savollarga javob berishingiz mumkin.

Bizning Oilamizga a'zo bo`lganingiz uchun chuqur Minnatdorchilik bildiramiz !!!

Telegram kanal YordamUzRss

...