I am trying to master Yii2 on one small internal project. There was a problem, the model ceased to persist. What could be the problem: Model:

<?php namespace app\models; use Yii; use yii\db\ActiveRecord; use yii\base\Model; use yii\web\UploadedFile; //Класс описывающий модель рассылок class Send extends ActiveRecord { //public $id; //ID рассылки // public $type; //Тип рассылки /** * @var UploadedFile */ public $baseFile; public $baseText; public static function tableName() { return '{{%send}}'; } //RULES public function rules(){ return [ ['type', 'required'], [['baseFile'], 'file', 'skipOnEmpty' => true, 'extensions' => 'txt, doc, docx'], ]; } public function uploadBase() { if ($this->validate()) { $this->baseFile->saveAs('../base/' . $this->baseFile->baseName .date('Y_m_d_H_i'). '.' . $this->baseFile->extension); return true; } else { return false; } } } 

Controller Method:

 <?php namespace app\controllers; use Yii; use yii\web\Response; use yii\web\Controller; use app\models\User; use app\models\Send; use yii\web\UploadedFile; class SendController extends Controller { public $layout = 'client_panel'; public function actionWatext(){ if (!Yii::$app->user->isGuest) { $model = new Send(); //Если модель валидна, то переводим на страницу лога рассылок if($model->load(Yii::$app->request->post())){ $model->baseFile = UploadedFile::getInstance($model, 'baseFile'); $model->type = 'WA_text'; $model->user_id = Yii::$app->user->identity->id; $model->text = $_POST['Send']['text']; if(!empty($_POST['Send']['baseText'])){ $model->base_text = $_POST['Send']['baseText']; } if($_POST['send_date'] == 'Выбрать дату'){ $model->date = $_POST['send_date_calendar']; } else{ $model->date = 'Сразу'; } if(!empty($model->baseFile)){ $model->uploadBase(); $model->base_file = '../base/' . $model->baseFile->baseName .date('Y_m_d_H_i'). '.' . $model->baseFile->extension; } if ($model->save()) { return $this->redirect(['client/allsend']); } else{ return 'errro'; } }//Иначе генерируем форму else{ return $this->render('watext',['model' => $model]); } } else{ return $this->redirect(['site/index']); } } } 
  • one
    Most likely validation error, put return 'errno'; instead return 'errno'; print_r($model->errors); and see the errors. - pa3py6aka
  • So, figured it out. Invalid file extension was. But another error came out: finfo_file (C: \ OSPanel \ userdata \ temp \ php2AD9.tmp): failed to open stream: No such file or directory - dmitriy_vlz
  • maybe you should create a folder C:\OSPanel\userdata\temp ? Although, in which line of the code does this error pop up? - pa3py6aka
  • one
    I think this is due to the fact that when model-> save () validation is called again, you try to check the baseFile file, but the temporary file is already deleted. In the uploadBase() function, when saving a file, you can add a flag so that the temporary file is not deleted immediately - $this->baseFile->saveAs('../base/' . $this->baseFile->baseName .date('Y_m_d_H_i'). '.' . $this->baseFile->extension, false); . But it is better to change the validation logic - validate here - if($model->load(Yii::$app->request->post()) && $model->validate()){ , in the function uploadBase() remove the validation and also save when saving - $model->save(false) - pa3py6aka
  • one
    Thank you so much. So far, I made one for your version. And about the logic right now, I read, I will try to penetrate fully. - Dmitriy_vlz

0