XF 2.0 Saving multiple items through form submit

ludak

Active member
I am trying to add multiple items in the database using the form submit. I know I can pass the object individual to template and then change it and then save, and post that object. That works fine. It would be in the save ParameterBag and I can do it.

I am wondering what if I am about to open a overlay and create a list of items automatically. And then let the user make selection absed on the list. and click save.

So before hand I create number of rows (games) based on the option for that tournament.

Code:
for($i = 0; $i < $rows;$i++){

    $game = $this->em()->create('XX:Game');

    $game->game_id = $i+1;

    array_push($games, $game);

}

AND PASS THAT to the template... which renders this...

Code:
<xf:form action="{{ link('xx/games/save') }}" ajax="true" class="block">

    <div class="block-container">

            <div class="block-body">

                <xf:datalist data-xf-init="responsive-data-list">

                    <xf:datarow rowtype="header">

                        <xf:cell>{{ phrase('player_a') }}</xf:cell>

                        <xf:cell>{{ phrase('vs') }}</xf:cell>

                        <xf:cell>{{ phrase('player_b') }}</xf:cell>

                        <xf:cell>{{ phrase('result') }}</xf:cell>

                    </xf:datarow>

                    <xf:foreach loop="$games" value="$game">

                        <xf:datarow>

                            <xf:cell>

                                <xf:select name="player_a_id[{$game.game_id}]" value="{$game.player_a_id}">

                                    <xf:foreach loop="$players" value="$player">

                                        <xf:option value="{$player.player_id}">{$player.player_name}</xf:option>

                                    </xf:foreach>

                                </xf:select>

                            </xf:cell>

                            <xf:cell>vs.</xf:cell>

                            <xf:cell>

                               <xf:select name="player_b_id[{$game.game_id}]" value="{$game.player_b_id}">

                                    <xf:foreach loop="$players" value="$player">

                                        <xf:option value="{$player.player_id}">{$player.player_name}</xf:option>

                                    </xf:foreach>

                                </xf:select>

                            </xf:cell>

                            <xf:cell>

                                <xf:select name="resuly" value="{$game.result}">

                                    <xf:option value=""></xf:option>

                                    <xf:option value="1">1</xf:option>

                                    <xf:option value="x">X</xf:option>

                                    <xf:option value="2">2</xf:option>

                                </xf:select>

                            </xf:cell>

                        </xf:datarow>

                    </xf:foreach>

                </xf:datalist>

            </div>

        <xf:submitrow sticky="true" icon="save" />

    </div>

</xf:form>

Basically what I am asking is, this is gonna have a list of player a vs player b in dropdown. I will select which players are playing with who, and then, I will save it.

I want the RESULT to be editable, so that admin can go back to edit this and save the final result.

So basically I would want to iterate trough this form and when click save save the LIST of Entity Game. In the controller save action I would then iterate through that list and save the items in the database.

Is this possible like this?
 
@Chris D Could you help me with this please? I am trying to use the best practices when writing these add-on's. I can go around and use the <xf:js as I can tell that is something that can be done and the use the jquery post method to pass the object to the controller, but trying to get the best idea of how is this done. If there is a sample somewhere I would aprreciatee it.

I am getting much further then originally and already working on my second add-on :)

XENFORO RULES :)
 
Not sure why is this so complicated, I have searched though the docs and I cannot find anything about it.. Can anybody chime in?

This was somewhat asked before and never had an answer.

https://xenforo.com/community/threads/setting-the-action-on-a-form.132403/

its pretty much that I can see the values in the $_GET or $_POST based on what I set my method to be, but my ParameterBag is empty.

I want to when clicked save get the ArrayCollection of XX:Game entity. Is this even possible?

Thanks!!!
 
So I got somewhere by setting up the array passed in properly from the template...

so I have something like

Code:
name="games[round_id][$games.game_id]"
name="games[home_id][$games.game_id]"

and then I use method=get, so now I can in the back end in controller do something like this.

PHP:
$games = $this->filter('games', 'array');

Now this is all great, and based on some keys from array I can create Game Entity... but how do I set the properties on the object properly.

PHP:
      $games = $this->filter('games', 'array');

        foreach($games as $g){

            $result  = $g['result'];

            if ($game['game_id']){
                $game = $this->assertGameExists([$g['game_id'],$g['round_id']]);
            }else{
                $game = $this->em()->create('XX:Game');
            }
            
            $game->set('result', $result);

            $this->gameSaveProcess($game)->run();
        }

now if the $game is found in the database based oin the game_id round id... then its fine, but since i am using get how shuold I set the values in that object and save it in the database???

Tried this does not work
Code:
 $game->set('result', $result);
Somebody please help :)
 
ok So I figured this out now as well... need to do
Code:
 $game->save();
when using get.

Leaving the trail for other people to know when they come accross similar issues.
 
Back
Top Bottom