XF 2.1 Possible to use formAction() inside a loop?

asprin

Active member
So I've this piece of code in my controller method that handles form submission....
PHP:
protected function fooSaveProcess(\Asprin\FB\Entity\Foo $foo)
{
    $formData = $this->filter('selections', 'array');
    
   
    /*
    count($formData) --> this is printing "2"
    implode(",", $formData) --> this is printing "5,6"
    so these two statements prove that there are two distinct values in the posted array
    */
     
    /* now I'm attempting to loop through the array and create 2 records in the db */
    foreach($formData as $selectionId)
    {
        $input = array();
        $form = $this->formAction();
        $input['column_name'] = $selectionId;
        $form->basicEntitySave($foo, $input);
    }
   
    return $form;
}

This results in only 1 row getting created into the db table and the other is always ignored. I tried putting $form = $this->formAction(); outside the for loop and it still behaved the same way.

What am I missing here?
 
Last edited:
Solution
Oh damn! I figured it out. I was using the same instance of $foo inside each iteration of the loop 🤦‍♂️

Code:
public function actionAdd()
{
    //$foo = $this->em()->create('Asprin\FB:Foo'); Nope, do not keep this here
  
    $formData = $this->filter('selections', 'array');
    if(is_array($formData) && count($formData) > 0)
    {
        foreach($formData as $selectionId)
        {
            $foo = $this->em()->create('Asprin\FB:Foo'); // yep, this is where it should go
            $this->fooSaveProcess($foo, $selectionId)->run;
        }
    }
}
I'm not at my system right now but I've a feeling I might have to call fooSaveProcess() inside the for loop instead of using the for loop inside it.
 
You should be trying to make this go through a service instead of using form action.
My knowledge on Services is limited at the moment. Hence, I'm going by what I saw in Kier's video tutorials. It could just be me but I didn't find much documentation on it too other than a rough mention here and there.
 
I'm not at my system right now but I've a feeling I might have to call fooSaveProcess() inside the for loop instead of using the for loop inside it.
Nope, this doesn't work too. Again only a single row in inserted even though array contains 2 distinct values.

Code:
public function actionAdd()
{
    $foo = $this->em()->create('Asprin\FB:Foo');
   
    $formData = $this->filter('selections', 'array');
    if(is_array($formData) && count($formData) > 0)
    {
        foreach($formData as $selectionId)
        {
            $this->fooSaveProcess($foo, $selectionId)->run;
        }
    }
}

protected function fooSaveProcess(\Asprin\FB\Entity\Foo $foo, $selectionId)
{  
    $input = array();
    $form = $this->formAction();
    $input['column_name'] = $selectionId;
    $form->basicEntitySave($foo, $input);  
 
    return $form;
}

Any ideas anyone?
 
Last edited:
Oh damn! I figured it out. I was using the same instance of $foo inside each iteration of the loop 🤦‍♂️

Code:
public function actionAdd()
{
    //$foo = $this->em()->create('Asprin\FB:Foo'); Nope, do not keep this here
  
    $formData = $this->filter('selections', 'array');
    if(is_array($formData) && count($formData) > 0)
    {
        foreach($formData as $selectionId)
        {
            $foo = $this->em()->create('Asprin\FB:Foo'); // yep, this is where it should go
            $this->fooSaveProcess($foo, $selectionId)->run;
        }
    }
}
 
Solution
Top Bottom