if ($modell->validate()) {
$modell->save();
} else {
print_r($modell->errors);
}
public static function IdAuthor($name){
return Avtor1::find()->select('id')->where('name=:name',[':name' => $name])->scalar();
}
public static function IdAuthor($name){
return
Avtor1::find()->where(['name' => $name])->scalar();
}
public function actionUpload()
{
$model = new UploadForm();
if (Yii::$app->request->isPost ) {
$model->fName = UploadedFile::getInstance($model, 'fName');
if ($fName =$model->upload()) {
//путь к файлу
if (($handle = fopen($fName, 'r')) !== false) {
while (($row = fgetcsv($handle, 1000, ',')) !== false) {
if ($row[1]==='ID'){
continue;
}
$modell = new Kniga1();
$modell->name = $row [2];
$modell-> id_avtor1 = Avtor1::idAuthor($row[4]);
$modell->creation_date = $row [6];
if ($modell->validate()) {
$modell->save();
} else {
print_r($modell->errors);
}
}
fclose($handle);
return $this->redirect(['index']);
};
//... код после импорта
}
}
return $this->render('upload', ['model' => $model]);
}
public static function IdAuthor($name){
return Avtor1::find()->select('id')->where('name=:name',[':name' => $name])->scalar();
}
class UploadForm extends Model
{
/**
* @var UploadedFile
*/
public $fName;
public function rules()
{
return [
[['fName'], 'safe'],
];
}
public function upload()
{
if ($this->validate()) {
$path = \Yii::getAlias('@runtime').'/uploads/' . $this->fName->baseName . '.' . $this->fName->extension;
$this->fName->saveAs($path);
return $path;
} else {
return false;
}
}
}
$model = Avtor1::find()->where("name")= $row [4];
if ($model->validate()) {
$model->save();
} else {
$model->save();
print_r($model->errors);
}
ведь в контроллере в коде импорта я прописал
вместо
Который у меня был в прошлом варианте)