Using the Database for Sessions in CakePHP

31st August 2010 CakePHP CakePHP 1.3

Just a quick tip, I've been playing with the new 1.3.3 version of Cake recently and was trying to get the database sessions up and running but had difficultly finding the schema for the sessions table without using the command line to generate the table.

Here's the raw SQL that can be used to create the table. I'm sure it'll be useful in the future for me so I thought I'd share.

CREATE TABLE `cake_sessions` (
  `id` varchar(255) NOT NULL DEFAULT '',
  `data` text,
  `expires` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
);

Now you just need to modify the core.php file so that the database is used for Sessions instead of the default:

Configure::write('Session.save', 'database');

Update

To create the Sessions table from the console simply navigate to the /cake/console directory in the command line and run:

php cake.php schema create Sessions

Back to Home Page

Comments

Ramon Marco Navarro (09/09/2010 - 20:45)

There is also an index column. The sessions table is described in the file app/config/schema/sessions.php

James (09/09/2010 - 22:24)

@Ramon: Thanks but the index on the 'id' field is already in there with:

PRIMARY KEY (`id`)

Matt (12/10/2010 - 16:31)

Just a quick tip - if you have a very high traffic site you might want to ensure the table type in mysql is innodb - myisam will lock the whole table on writes and updates which can lead to problems with backlogged queries, where as innodb supports row level and page level locking which supports concurrent read and write requests with out (generally) causing locking issues.

If you server has innodb support (and most should) you can change the table type with: (obviously it would be wise to take your site offline and backup the table first)

alter table cake_sessions engine=innodb;

Add Your Comment

Sites I've Created

DSP Chartered Surveyors Stoneylane Aberdeen Angus

Recently Watched Films

Mr Brooks Rec Pathology Diary of the Dead

TV Shows

The Shield Flight of the Conchords

Hosting

I've been using Dreamhost for my hosting for over 2 years and it's pretty good for what you pay. If you're signing up how about showing me some love and use this link so I get a referal bonus to help pay my server costs.

Site by James Fairhurst © 2008-2012, all rights reserved and all that malarky