Jessica Posted July 30, 2010 Share Posted July 30, 2010 ETA: Title said login, I meant logic. Not tackling login yet Hey guys. For fun I decided to try to make a web game using CakePHP. For now I'm completely ignoring the user login and just trying to build it as if one user is logged in. So the user has a Map they can see (it will be a 5x5 grid, with them in the center). When they load their map page, I want it to generate some random things onto the map (so placing them in random spots in the 5x5 grid). Then save that into the session so that the whole time they are logged in, those items are in those spots. Right now this is my maps_controller.php <?php class MapsController extends AppController { var $name = 'Maps'; function index() { $user = $this->Session->read('User'); $map = $this->Map->findById($user['User']['map_id']); $this->set('map', $map); } } ?> The logic I need to add is that after retrieving the Map (which is just an ID and name right now), to check if I have already generated the items. If not, do so, then save it into the session. THEN I need to set the whole thing into the view using $this->set(). Can someone help point me in the right direction? I am not sure where I'd put this logic...in the afterFind() maybe? Quote Link to comment Share on other sites More sharing options...
Zane Posted July 30, 2010 Share Posted July 30, 2010 I'm not very familiar with cakePHP, but does it serialize the objects before putting them into the SESSION? Because to me, that seems to be what your looking for. Quote Link to comment Share on other sites More sharing options...
Jessica Posted July 30, 2010 Author Share Posted July 30, 2010 Zanus, what I need to do is run some php code (a loop to generate random stuff basically) and ADD it to the object before it goes into the session. And only if it's not already there. Quote Link to comment Share on other sites More sharing options...
Jessica Posted July 30, 2010 Author Share Posted July 30, 2010 I decided to make a table called "Map Spots" and every hour will generate new items on the map, and it will be communal. So I added a relationship with Maps and Map Spots var $hasMany = array( 'MapSpot' => array( 'className' => 'MapSpot', 'foreignKey' => 'map_id', 'dependent' => false, 'conditions' => '', 'fields' => '', 'order' => '', 'limit' => '', 'offset' => '', 'exclusive' => '', 'finderQuery' => '', 'counterQuery' => '' ) ); And added recursive to my user so when the user is found, their map and all map spots are as well. <?php class User extends AppModel { var $name = 'User'; var $recursive = 2; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.