How to Use MongoDB for Session Storage with Lithium

Step 1

Download this file to app/extensions/adapter/storage/session/Model.php

Step 2

Create a starter app/models/Sessions.php:

<?
namespace app\models;
class Sessions extends \lithium\data\Model {

}
?>

Step 3

Configure app/config/bootstrap/session.php to use the new extension:

Session::config(array(
 'cookie' => array('adapter' => 'Cookie', 'name' => $name),
 'default' => array('adapter' => 'Model', 'session.name' => $name, 'model' => 'Sessions')
 ));

Doing advanced MongoDB updates in Lithium

I wanted to change a nested array value without first loading an entire MongoDB document in Lithium, but the documentation wasn’t clear. One of the developers on the #li3 IRC channel pointed me in the right direction.

Since the “atomic” option is defaulted to true, the $set is implied. The following example is with a model called “Lesson”:

$conditions = array('_id' =>  $_id); //conditions for document(s) to change
$data = array(('page_list.123.title') => $title); //nested value I want to change
Lesson::update($data,$conditions);

I hope that points you in the right direction. It’s like the native MongoCollection::update but with the first two parameters switched. You can also include a third parameter for options. Soon the update method will be patched to allow for MongoDB upserts.