php 在会话中存储对象 Symfony 2

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11016819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:34:53  来源:igfitidea点击:

Store objects in sessions Symfony 2

phpsessionsymfony

提问by Ale?

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.

我正在用 Symfony 2 编写一个小型电子商店应用程序,我需要某种方式在会话中存储用户的购物车。我认为使用数据库不是一个好主意。

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

该应用程序将使用ProductCategoryShoppingCart等实体,其中 Product 和 Category 保存在数据库中,用户将在他们的 ShoppingCart 中选择产品。

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

我找到了NativeSessionStorage类,它应该将实体保存到会话中。但是没有书面的应用程序实现过程。

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

我是在控制器中还是在单独的类 ShoppingCart 中使用它?你能给我一个简短的NativeSessionStorage用法示例吗?

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?

目标不是将所有产品 ID 保存到 cookie 中。目标是在服务器端的应用程序内存中只保存一个篮子(装满产品)的参考,并为用户分配适当的篮子。这甚至可以在 PHP 中做到这一点吗?

EDIT2:

编辑2

Is a better solution to use a service?

使用服务是更好的解决方案吗?

回答by Sense

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");

$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");

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

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

这是基于Session类的使用,简单易懂。常用定义:

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

另外,看看其他存储数据的方法:Multiple SessionStorage

回答by Mun Mun Das

You can make your entity Serializableand 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.

您可以使您的实体可序列化并序列化实体对象并保存到会话,然后使用unserialize(). 有一个警告,因为 db Doctrine2 中存在的实体会将检索到的/未序列化的实体标记为分离的。$em->merge($entity);在这种情况下你必须打电话。

回答by Ivan Kvasnica

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

您可以使用 Symfony 将整个对象保存到会话中。只需使用(在控制器中):

$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.

注意:对象需要可序列化。否则,PHP 在 start_session() 函数上加载会话时会崩溃。

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

只需通过添加 serialize() 和 unserialize() 方法来实现 \Serializable 接口,如下所示:

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)

来源:http : //blog.ikvasnica.com/entry/storing-objects-into-a-session-in-symfony(我关于这个主题的博文)