D
D
des1roer2015-07-02 07:57:59
Yii
des1roer, 2015-07-02 07:57:59

Yii 1.* uploading files?

if we take this instruction as a basis yiiframework.ru/doc/cookbook/ru/form.file.upload
then the image field is a field in the table required?
when you select a photo and click create, I get

The following errors need to be corrected:
The "Image" field must be filled in.

in general, everything seems to have turned out with a thick model yiiframework.ru/doc/cookbook/ru/form.file.upload.f...
but here's the jamb
public function beforeSave()
    {
        if (!parent::beforeSave())
            return false;
        if (CUploadedFile::getInstance($this, 'document') != null)
        {
            if (($this->scenario == 'insert' || $this->scenario == 'update') &&
                    ($document = CUploadedFile::getInstance($this, 'document'))
            )
            {
                $this->deleteDocument(); // старый документ удалим, потому что загружаем новый

                $this->document = $document;
                $this->document->saveAs(
                        Yii::getPathOfAlias('webroot.media') . DIRECTORY_SEPARATOR . $this->document);
            }
        }
        return true;
    }

the creature deletes a photo in any result at updating.
$document = CUploadedFile::getInstance($this, 'document') isn't it instructed? how to specify that the photo is updated (read - the old one is deleted and the new one is loaded). only when updating via file upload. well, and of course, the jamb that saves despite the fact that the file with the same name already exists (here, though, as an option, you can set a unique file name, or generate it yourself based on the id, so it’s not entirely critical)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
des1roer, 2015-07-03
@des1roer

the fat model turned out to be unviable
, implemented it like this des1roer.blogspot.ru/2015/07/yii.html

P
Pavel Gogolinsky, 2015-07-02
@gogolinsky

It is required by default. It is necessary to specify allowEmpty=>true in the model in the file validator

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question