13

I am writing a small e-shop application with Symfony 2 and I need some way to store the user's shopping cart in a session. I think using a database is not a good idea.

The application will use entities like Product, Category, ShoppingCart where Product and Category are persisted into the database and users will be choosing products into their ShoppingCart.

I have found NativeSessionStorage class which is supposed to save an entity into a session. But there is no written process of implementation into an application.

Do I use this in the controller or in a separated class ShoppingCart? Could you give me a short example of NativeSessionStorage usage?

EDIT: The question was not set correctly:

The goal is not to save all product ids into a cookie. The goal is to save only a reference for basket (filled with products) in application memory on server-side and assign proper basket to user. Is this even possible to do this in PHP?

EDIT2:

Is a better solution to use a service?

Carrie Kendall
  • 11,124
  • 5
  • 61
  • 81
Aleš
  • 783
  • 3
  • 7
  • 16
  • It's generally a bad idea because the storage is very limited and you have no way to know if storing it was successfull. I would rather save it to a temporary table or database, maybe using something like redis or mongodb and just store a key to the shopping cart in the session. – Sgoettschkes Jun 13 '12 at 14:18

3 Answers3

24

Don't know if this way is the better way to store your data temporary. You can use this to instantiate a session object :

$session = $this->get("session");

Don't forget the 'use' in your controller :

use Symfony\Component\HttpFoundation\Session;

Then, the session starts automatically when you want to set a variable like :

$session->set("product name","computer");

This is based on the use of the Session class, easy to understand. Commonly definitions :

get(string $name, mixed $default = null)

Returns an attribute.
    
set(string $name, mixed $value)

Sets an attribute.

has(string $name)

Checks if an attribute is defined.

Also, take a look to the other ways to store your data : Multiple SessionStorage

UPDATED LINK: Session usage: https://symfony.com/doc/current/session.html

Sense
  • 1,096
  • 8
  • 20
  • 1
    This only explains how to use the session service. As the question specified that entity instances need to be stored in the session the answers by Mun Mun Das and Ivan Kvasnica below, whereby the entities stored are made serializable, need to be used too. – Adambean Dec 06 '17 at 09:36
  • 1
    The link to the docs of the Session class is broken. A new link is: https://symfony.com/doc/current/session.html – SDwarfs Aug 10 '23 at 10:31
14

You can make your entity Serializable and serialize the entity object and save to session and then retrieve in other page using unserialize(). There is one caveat, for an entity that exists in the db Doctrine2 will mark the retrieved/unserialized entity as detached. You have to call $em->merge($entity); in this case.

Mun Mun Das
  • 14,992
  • 2
  • 44
  • 43
6

You can save the whole object into a session with Symfony. Just use (in a controller):

$this->get('session')->set('session_name', $object);

Beware: the object needs to be serializable. Otherwise, PHP crashes when loading the session on the start_session() function.

Just implement the \Serializable interface by adding serialize() and unserialize() method, like this:

public function serialize()
{
    return serialize(
        [
            $this->property1,
            $this->property2,
        ]
    );
}

public function unserialize($serialized)
{
    $data = unserialize($serialized);
    list(
        $this->property1, 
        $this->property2,
    ) = $data;
}

Source: http://blog.ikvasnica.com/entry/storing-objects-into-a-session-in-symfony (my blogpost on this topic)

7ochem
  • 2,183
  • 1
  • 34
  • 42
Ivan Kvasnica
  • 776
  • 5
  • 13