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.