Home php Invalid argument supplied for foreach ()

Invalid argument supplied for foreach ()

Author

Date

Category

What could be the reason for the error?

Invalid argument supplied for foreach ()

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

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

Here is the model responsible for the images:

& lt;? 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' = & gt; 255],
    [['image'], 'file', 'skipOnEmpty' = & gt; true, 'extensions' = & gt; 'jpg, jpeg, png, gif', 'maxFiles' = & gt; 6],
  ];
}
/ **
 * @inheritdoc
 * /
public function attributeLabels ()
{
  return [
    'v_id' = & gt; 'V ID',
    'imagepath' = & gt; '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' = & gt; 'a', 'b' = & gt; 'b', 'in' = & gt; 'v',
    'r' = & gt; 'g', 'd' = & gt; 'd', 'e' = & gt; 'e',
    'e' = & gt; 'e', 'g' = & gt; 'zh', 'h' = & gt; 'z',
    'and' = & gt; 'i', 'th' = & gt; 'y', 'k' = & gt; 'k',
    'l' = & gt; 'l', 'm' = & gt; 'm', 'n' = & gt; 'n',
    'o' = & gt; 'o', 'n' = & gt; 'p', 'p' = & gt; 'r',
    'c' = & gt; 's', 't' = & gt; 't', 'y' = & gt; 'u',
    'f' = & gt; 'f', 'x' = & gt; 'h', 'c' = & gt; 'c',
    'h' = & gt; 'ch', 'w' = & gt; 'sh', 'u' = & gt; 'sch',
    'b' = & gt; '\' ',' s' = & gt; 'y', 'b' = & gt; '\' ',
    'e' = & gt; 'e', 'u' = & gt; 'yu', 'i' = & gt; 'ya',
    'A' = & gt; 'A', 'B' = & gt; 'B', 'B' = & gt; 'V',
    'G' = & gt; 'G', 'D' = & gt; 'D', 'E' = & gt; 'E',
    'E' = & gt; 'E', 'F' = & gt; 'Zh', 'Z' = & gt; 'Z',
    'And' = & gt; 'I', 'Y' = & gt; 'Y', 'K' = & gt; 'K',
    'L' = & gt; 'L', 'M' = & gt; 'M', 'H' = & gt; 'N',
    'O' = & gt; 'O', 'P' = & gt; 'P', 'P' = & gt; 'R',
    'C' = & gt; 'S', 'T' = & gt; 'T', 'Y' = & gt; 'U',
    'F' = & gt; 'F', 'X' = & gt; 'H', 'C' = & gt; 'C',
    'H' = & gt; 'Ch', 'W' = & gt; 'Sh', 'U' = & gt; 'Sch',
'B' = & gt; '\' ',' S '= & gt; 'Y', 'b' = & gt; '\' ',
    'E' = & gt; 'E', 'Y' = & gt; 'Yu', 'I' = & gt; 'Ya',
    '-' = & gt; '-',
  );
  $ string = strtr ($ string, $ converter);
  return $ string;
}
public function getImages ()
{
  return $ this- & gt; hasMany (Vote :: className (), ['vote_id' = & gt; 'id']);
}
}

The error page states the following:

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

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

swears here in foreach.


Answer 1, authority 100%

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

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

Next, this variable (which stores the name of the column) is put into foreach. The argument of foricha must be an array, and you have a string in it.

Accordingly, add or add

$ columns = [$ columns];

or remove foreach.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions