What is the reason for the error?

Invalid argument supplied for foreach ()

I understand that the argument is not supported by the function, but how to fix it? Regardless of the error, the models are created, the pictures are saved in the folder, and the paths are recorded in the database. Here is the controller:

public function actionCreate() { $model = new Vote(); $model1 = new Images(); if ($model->load(Yii::$app->request->post()) && $model->save()) { $model1->image = UploadedFile::getInstances($model1, 'image'); $a = $this->uniqueId; foreach ($model1->image as $image) { $image->saveAs($model1->rus2translit('upload/files/' . $a . '_' . $image->baseName . '.' . $image->extension)); $ipath = $model1->rus2translit($a . '_' . $image->baseName . '.' . $image->extension); Yii::$app->db->createCommand("INSERT INTO images (v_id, imagepath) VALUES ('" . $model->id . "', '" . $ipath . "')")->execute(); } $model1->save(false); return $this->redirect($url = Url::previous()); } //return $this->redirect(['view', 'id' => $model->id]); return $this->render('create', [ 'model' => $model, 'model1' => $model1, ]); } 

Here is the model responsible for the image:

 <?php namespace common\models; use Yii; /* * This is the model class for table "images". * * @property integer $v_id * @property string $imagepath * @property Vote[] $images */ class Images extends \yii\db\ActiveRecord { /** * @inheritdoc */ public $image=[]; public static function tableName() { return 'images'; } /** * @inheritdoc */ public function rules() { return [ [['v_id'], 'integer'], [['imagepath'], 'string', 'max' => 255], [['image'], 'file', 'skipOnEmpty' => true, 'extensions' => 'jpg, jpeg, png, gif', 'maxFiles' => 6], ]; } /** * @inheritdoc */ public function attributeLabels() { return [ 'v_id' => 'V ID', 'imagepath' => 'Imagepath', ]; } /** * @inheritdoc * @return \common\models\query\ImagesQuery the active query used by this AR class. */ public static function find() { return new \common\models\query\ImagesQuery(get_called_class()); } public function rus2translit($string) { $converter = array( 'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd', 'е' => 'e', 'ё' => 'e', 'ж' => 'zh', 'з' => 'z', 'и' => 'i', 'й' => 'y', 'к' => 'k', 'л' => 'l', 'м' => 'm', 'н' => 'n', 'о' => 'o', 'п' => 'p', 'р' => 'r', 'с' => 's', 'т' => 't', 'у' => 'u', 'ф' => 'f', 'х' => 'h', 'ц' => 'c', 'ч' => 'ch', 'ш' => 'sh', 'щ' => 'sch', 'ь' => '\'', 'ы' => 'y', 'ъ' => '\'', 'э' => 'e', 'ю' => 'yu', 'я' => 'ya', 'А' => 'A', 'Б' => 'B', 'В' => 'V', 'Г' => 'G', 'Д' => 'D', 'Е' => 'E', 'Ё' => 'E', 'Ж' => 'Zh', 'З' => 'Z', 'И' => 'I', 'Й' => 'Y', 'К' => 'K', 'Л' => 'L', 'М' => 'M', 'Н' => 'N', 'О' => 'O', 'П' => 'P', 'Р' => 'R', 'С' => 'S', 'Т' => 'T', 'У' => 'U', 'Ф' => 'F', 'Х' => 'H', 'Ц' => 'C', 'Ч' => 'Ch', 'Ш' => 'Sh', 'Щ' => 'Sch', 'Ь' => '\'', 'Ы' => 'Y', 'Ъ' => '\'', 'Э' => 'E', 'Ю' => 'Yu', 'Я' => 'Ya', '—' => '-', ); $string = strtr($string, $converter); return $string; } public function getImages() { return $this->hasMany(Vote::className(), ['vote_id' => 'id']); } } 

The error page contains the following:

in C: \ OpenServer \ domains \ poll.ua \ vendor \ yiisoft \ yii2 \ db \ mysql \ QueryBuilder.php

 if (empty($names) && $tableSchema !== null) { $columns = !empty($tableSchema->primaryKey) ? $tableSchema->primaryKey : reset($tableSchema->columns)->name; foreach ($columns as $name) { $names[] = $schema->quoteColumnName($name); $placeholders[] = 'DEFAULT'; } } 

here on foreach swears.

    1 answer 1

     $columns = !empty($tableSchema->primaryKey) ? $tableSchema->primaryKey : reset($tableSchema->columns)->name; 

    As far as I understand, the $ columns variable will either be the primaryKey (if it is not empty) or the name of the first column.

    Next, this variable (in which the column name is stored) is put in foreach. The foritch argument should have an array, and you have a string in it.

    Accordingly, it is necessary or to add

     $columns = [$columns]; 

    or remove foreach.

    • Yes you are right. Added another field Id and set him primary key. The error has gone. But in DB now paths to pictures and +1 field are written with empty values, but filled id (like autoincrement). Although I have another table, with almost similar functions. There are two fields in which the user ID and voting are written. So there I do not have a primary key, but foreach does not swear at anything. joxi.ru/5mdonRvcOVDLr1 - Alexey
    • It was in this line $model1->save(false); it is not needed at all. I deleted it and everything works as intended. No additional fields in the table. Magic :) - Alexey