Questions & Answers

How insert an array of data in one query into the database

I have an array where each element contains data and an array. I save it in db with double foreach.

Сan i prepare an array of data and insert it in one request? I'm not confused much because my $user object in the $user->org_id field refers to the record I just inserted.

foreach ($xmlObject as $org)
            $service = new PostService();
            $org1 = new Organization();
            $org1->name = $org->attributes()['Name'];
            foreach ($org as $item) {
                $user = new User();
                $user->first_name = $item->attributes()['firstname'];
                $user->last_name = $item->attributes()['lastname'];
                $user->org_id = $org1->id;

Organizations Shema


Users Shema


xml Data

 <org Name="FFF Company">
  <user firstname="Sara" lastname="Freser" />
  <user firstname="Mary" lastname="Popins" />
 <org Name="SweetDream">
  <user firstname="Vin" lastname="Diz"  />
2023-01-18 23:30:15
Please post your schema and sample data
2023-01-18 23:30:15
@RohitGupta yeah sorry ,it looks like this
2023-01-18 23:30:15
btw, you are reassigning $org immediately in the foreach loop and you are iterating $org which looks to be a Model instance, this will just iterate the public properties of the object
Answers(0) :