Stack Exchange
Array to String conversion error in Symfony2
I have a form from which i am fetching the data as array in controller from $request->get()  method and want to submit that data to database..
This is the array i am getting when i print the post variable:
Array
(

[country] => Array
    (
        [0] => 24
        [1] => 4
    )

[state] => Array
    (
        [0] => Cuando Cubango
        [1] => Badakhshan
    )

[activity] => Array
    (
        [0] => 3
        [1] => 5
    )

[activity_0] => Array
    (
        [0] => 3
    )

)

I want them to insert in database in this format:
country state            activity
24      Cuando Cubango   3  
24      Cuando Cubango   5
4       Badakhshan       3

What i have tried so far is this:
$active = $request->get('activity');
if (!empty($active)) {
    foreach ($active as $activity) {
        $states = $request->get('state');
        $c = $request->get('country');
        $Bidactivity = $active;
        foreach ($states as $state) {
            foreach ($Bidactivity as $orgact) {
                $activity_to_db = new Activity();
                $activity_to_db->setActivityid($active);
                $activity_to_db->setState($states);
                $activity_to_db->setBidOrganiser($organiserDetails);
                $activity_to_db->setCountry($c);
                $em->persist($activity_to_db);
                $em->flush();
            }
        }
    }
}

But i am getting the array to string conversion error. Please guide.

your setCountry takes array as a parameter ? if not then you are passing country array $c to a function which needs a single value not array thats why you are getting this error its a guess because you have not posted definition of Activity

you mean i should setCountry and State outside the loop?

have you tried to put the $states and $c outside your loop?

yes  i just tried the error is still the same..

@Geetika read my comment again if a function needs single value then pass single value your post data contains array for each parameter you need to loop through each parameters data

include your entity metadata please.

You get this error, because you pass an array to every field of your activity:
  $activity_to_db->setActivityid($active); /* is this [activity] => Array
(
    [0] => 3
    [1] => 5
)*/
  $activity_to_db->setState($states); /* is this [state] => Array
(
    [0] => Cuando Cubango
    [1] => Badakhshan
)*/
  $activity_to_db->setBidOrganiser($organiserDetails); // doesn't occure, so whats that
  $activity_to_db->setCountry($c); /* is this [country] => Array
(
    [0] => 24
    [1] => 4
)*/

So anywhere there is the error.
Loop between the array with the highest number of elements as $active = $request->get('activity'); 
Try the smt like 
$states = $request->get('state');
 $c = $request->get('country');
 if (!empty($active)) {
      foreach ($c as $index=> $country) {
        foreach ($states as $state) {
           foreach ($active as $activity) {
            $activity_to_db = new Activity();
            $activity_to_db->setActivityid($activity);
            $activity_to_db->setState($state);
            $activity_to_db->setCountry($country);
            $em->persist($activity_to_db);
          }
        }
      }
 }
 $em->flush();


This only is inserting state and country in db not the activities....also the first country and state only not all selected ones

ok edited the code for you. Hope I got it right. Basically it saving for each country every state with every activity
