Hello to all. There is a form for adding a project (a project is a separate model), there are several textarea fields in it, for example, "List of sites" (a site is a separate model, the connection between a project and a site is one-to-many, that is, many sites are tied to one project ), in each such field with a new line are written lines. And then when I save the model, I assign POST data to the model, validate, and then try to save this data. And when I start saving data on sites, I get this error during validation - "mb_strlen () expects parameter 1 to be string, object given". Part of the code from the controller:

if(isset($_POST['Project'])){ $model->attributes=$_POST['Project']; if($model->validate()){ $model->save(); // преобразовываем в массив набор строк $competitors = explode(PHP_EOL,$model->competitors); foreach($competitors as $competitor){ $competitor = new Competitor(); $competitor->competitor_domen = $competitor;//$competitor - type - string $competitor->project_id = $model->id; $competitor->save();// вот здесь возникает ошибка - mb_strlen() expects parameter 1 to be string, object given } 

Help to understand what is wrong and how to solve it?

  • but it is clearly said that you are trying to find out the length of the object, but the length of the string is necessary. - Smash


1 answer 1

 $competitor->competitor_domen = $competitor;//$competitor - type - string 

it is assigned an object, not a string, as defined by the line above.

Just in case, if nothing happens, try changing the block from foreach to this:

 foreach($competitors as $competitor_){ $competitor = new Competitor(); $competitor->competitor_domen = $competitor_;//$competitor_ - type - string $competitor->project_id = $model->id; $competitor->save();// вот здесь возникает ошибка - mb_strlen() expects parameter 1 to be string, object given } 
  • Try to insert some arbitrary name, for example, "test" instead of $competitor - Smash
  • So what then to do? How can I solve this issue? - juniorspecialistphp
  • but it already depends on your global task, if there is a class Competitor , then it should obviously have some fields, like $competior->name . Review the class itself. - Smash
  • I tried it - it works if I replace it with some kind of string. Then I decided to explicitly specify the data type for $ competitor - as a string, but that did not help either. - juniorspecialistphp
  • The error occurs neither in the save method, and then when you assign an object, the pro_var_dump 'data is then. Every single variable and see where you have an object. - Smash