Entities in the Session

There are several use-cases to save entities in the session, for example:

セッションにエンティティを保存するユースケースがいくつかあります。次に例を示します。

  1. User data

    ユーザーデータ

  2. Multi-step forms

    マルチステップフォーム

To achieve this with Doctrine you have to pay attention to some details to get this working.

Doctrine でこれを実現するには、これを機能させるためにいくつかの詳細に注意を払う必要があります。

Updating an entity

In Doctrine an entity objects has to be “managed” by an EntityManager to be updatable. Entities saved into the session are not managed in the next request anymore. This means that you have to update the entities with the stored session data after you fetch the entities from the EntityManager again.

Doctrine では、エンティティ オブジェクトは更新可能にするために EntityManager によって「管理」される必要があります。セッションに保存されたエンティティは、次のリクエストでは管理されなくなりました。これは、EntityManager からエンティティを再度取得した後、保存されたセッションデータでエンティティを更新する必要があることを意味します。

For a representative User object the code to get data from the session into a managed Doctrine object can look like these examples:

代表的な User オブジェクトの場合、セッションからマネージド Doctrine オブジェクトにデータを取得するコードは、次の例のようになります。

Working with scalars

In simpler applications there is no need to work with objects in sessions and you can use separate session elements.

単純なアプリケーションでは、セッションでオブジェクトを操作する必要はなく、個別のセッション要素を使用できます。

<?php
require_once 'bootstrap.php';

session_start();
if (isset($_SESSION['userId']) && is_int($_SESSION['userId'])) {
    $userId = $_SESSION['userId'];

    $em = GetEntityManager(); // creates an EntityManager
    $user = $em->find(User::class, $userId);

    $user->setValue($_SESSION['storedValue']);

    $em->flush();
}

Working with custom data transfer objects

If objects are needed, we discourage the storage of entity objects in the session. It’s preferable to use a DTO (data transfer object) instead and merge the DTO data later with the entity.

オブジェクトが必要な場合は、エンティティ オブジェクトをセッションに保存しないことをお勧めします。代わりに DTO (データ転送オブジェクト) を使用し、後で DTO データをエンティティとマージすることをお勧めします。

<?php
require_once 'bootstrap.php';

session_start();
if (isset($_SESSION['user']) && $_SESSION['user'] instanceof UserDto) {
    $userDto = $_SESSION['user'];

    $em = GetEntityManager(); // creates an EntityManager
    $userEntity = $em->find(User::class, $userDto->getId());

    $userEntity->populateFromDto($userDto);

    $em->flush();
}

Serializing entity into the session

Entities that are serialized into the session normally contain references to other entities as well. Think of the user entity has a reference to their articles, groups, photos or many other different entities. If you serialize this object into the session then you don’t want to serialize the related entities as well. This is why you shouldn’t serialize an entity and use only the needed values of it. This can happen with the help of a DTO.

通常、セッションにシリアル化されるエンティティには、他のエンティティへの参照も含まれます。ユーザー エンティティには、記事、グループ、写真、またはその他のさまざまなエンティティへの参照があると考えてください。このオブジェクトをセッションにシリアライズする場合、関連エンティティもシリアライズする必要はありません。これが、エンティティをシリアライズして必要な値だけを使用するべきではない理由です。これは、DTO の助けを借りて行うことができます。

<?php
require_once 'bootstrap.php';

$em = GetEntityManager(); // creates an EntityManager

$user = $em->find("User", 1);
$userDto = new UserDto($user->getId(), $user->getFirstName(), $user->getLastName());
// or "UserDto::createFrom($user);", but don't store an entity in a property. Only its values without relations.

$_SESSION['user'] = $userDto;

Table Of Contents

Previous topic

Advanced field value conversion using custom mapping types

カスタム マッピング タイプを使用した高度なフィールド値の変換

This Page

Fork me on GitHub