0

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.

6
  • 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 Commented Aug 4, 2015 at 5:19
  • you mean i should setCountry and State outside the loop? Commented Aug 4, 2015 at 5:25
  • have you tried to put the $states and $c outside your loop? Commented Aug 4, 2015 at 5:27
  • yes i just tried the error is still the same.. Commented Aug 4, 2015 at 5:31
  • @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 Commented Aug 4, 2015 at 5:34

1 Answer 1

1

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();
Sign up to request clarification or add additional context in comments.

2 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.