T
T
The Dragger2015-04-09 08:29:38
MySQL
The Dragger, 2015-04-09 08:29:38

How to save a record to the database?

public function actionCreate() {
        $model = new MyGallery;
        if(!empty($_FILES)){
            move_uploaded_file($_FILES['file']['tmp_name'],
            Yii::app()->request->baseUrl . 'images/upload/' . '13' . $_FILES['file']['name']);
            $model->img_url = $_FILES['file']['name'];
        };
        if (isset($_POST['MyGallery'])) {
            $model->attributes = $_POST['MyGallery'];
//            $model->img_url = $_FILES['file']['name'];
            if ($model->save())
                $this->redirect(array('view', 'id' => $model->id));
        }
        $this->render('create', array(
            'model' => $model,
        ));
    }

There is a controller that should save records to the database, but does not save the name of the picture, and this results in an error: You must fill in the "Path to the picture" field. The file itself is loaded normally, but does not want to write it to the database, although it does not complain about other fields.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
eldar_web, 2015-04-09
@eldar_web

Rearrange, that is, write like this:

public function actionCreate() {
        $model = new MyGallery;
      
        if (isset($_POST['MyGallery'])) {
            $model->attributes = $_POST['MyGallery'];
if(!empty($_FILES)){
            move_uploaded_file($_FILES['file']['tmp_name'],
            Yii::app()->request->baseUrl . 'images/upload/' . '13' . $_FILES['file']['name']);
            $model->img_url = $_FILES['file']['name'];
        };
            if ($model->save())
                $this->redirect(array('view', 'id' => $model->id));
        }

  
        $this->render('create', array(
            'model' => $model,
        ));
    }

A
arab789, 2015-04-09
@arab789

You can just save like this:

$model->updateByPk($id, array('img_url'=>$_FILES['file']['name']));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question