3
votes

I have a Game model, view and controller, and a newgame action which creates a new game record. That works fine.

I then want to make that form data available to main_game_view.ctp either in the session, or as a passed-in-array (but not visible as a URL parameter). I don't mind if it's done on the session, or passed-in, whichever is the more efficient.

I've tried various different combos of set this, write that, pass the other, but nothing's worked so far. Here's my code as it stands after my latest failure. The GamesController newgame action:

public function newgame() {
    if ($this->request->is('post')) {
        $this->Game->create();
        $this->Session->write('Game',$this->request->data);
        if ($this->Game->save($this->request->data)) {
            // Put the id of the game we just created into the session, into the id field of the Game array.

            $this->Session->write('Game.id',$this->Game->getLastInsertID());
            $this->redirect(array('action' => 'main_game_view'));
        } else {
          $this->Session->setFlash('There was a problem creating the game.');
        }
    }
}

Writing the game id to the session works perfectly, I can see that in main_game_view when I read() it. But no matter where I put the session write for the request data, I can't find it in the main_game_view.

Similarly, if I try to pass, well, anything into the redirect, I can't find it in either the main_game_view action, or the main_game_view view itself.

Currently my main_game_view action is just an empty function, but I couldn't find anything in there after trying to pass the data via redirect.

Here's the main_game_view.ctp:

<?php debug($this->viewVars); ?>

<p><?php echo 'Game id: ' . $this->Session->read('Game.id') ?></p>
<p><?php echo 'Game name: ' . $this->Session->read('Game.game_name') ?></p>

Game.id is fine, but there's nothing in Game.game_name (which is a valid field name from the model. All my attempts to pass in variables have also failed, the debug line has only ever shown: array().

This seems so simple, having followed the CakePHP blog tutorial and adapting it to create/edit/delete game instances, but obviously something hasn't quite sunk in...

1

1 Answers

2
votes

You dont have Game.game_new key in session just by writing: $this->Session->write('Game',$this->request->data); Obviously you have to do something like this in main_game_view:

<?php $game = $this->Session->read('Game'); ?>
<p><?php echo 'Game id: ' . $this->Session->read('Game.id') ?></p>
<p><?php echo 'Game name: ' . $game['Game']['game_name']; ?></p>