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']); } } }
return 'errno';insteadreturn 'errno';print_r($model->errors);and see the errors. - pa3py6akaC:\OSPanel\userdata\temp? Although, in which line of the code does this error pop up? - pa3py6akauploadBase()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 thefunction uploadBase()remove the validation and also save when saving -$model->save(false)- pa3py6aka