0

With Symfony, I want to use Cache Manager (FilesystemCache) for save my entities. I did this :

    $find = $this->entityManager->getRepository(User::class)
        ->getSingleResult([
            'whereId' => $id,
        ]);

    dump($find); // First dump()
    $this->cacheManager->set('user-1', $find);
    dump($this->cacheManager->get('user-1')); // Second dump()

First dump :

User {#457 ▼
    -id: 1
    -username: "contact@site.com"
    -email: "contact@site.com"
    -plainPassword: null
    -password: "$2y$13$sFSRu2lyDmrfFZP24vkeQOiaYJ5E2eo/kLlaIITgwTrwZunEBcO1q"
    -onlineAt: DateTime {#432 ▶}
    -changePasswordAt: null
    -roles: array:3 [▶]
    -registerToken: null
    -versions: PersistentCollection {#558 ▶}
    -deletedAt: null
    -deletedBy: null
}

Second dump :

User {#188 ▼
    -id: 1
    -username: "contact@site.com"
    -email: null
    -plainPassword: null
    -password: "$2y$13$sFSRu2lyDmrfFZP24vkeQOiaYJ5E2eo/kLlaIITgwTrwZunEBcO1q"
    -onlineAt: null
    -changePasswordAt: null
    -roles: []
    -registerToken: null
    -versions: null
    -deletedAt: null
    -deletedBy: null
}

Why my second dump() does not have all the informations ?

Gaylord.P
  • 1,539
  • 2
  • 24
  • 54

1 Answers1

1

Since you are trying to save an object in the cache it has to be Serializable to tell PHP which properties should be safed, and which not. For comparison you can look how FOSUserBundle does it.

With larger object graphs (objects and their relationships, such as your versions property) it's usually not feasible to have all the information stored in cache as this might cause memory-problems when serializing/unserializing the data. For this Symfony's UserProvider has a refreshUser() method which reloads the remaining data. So if you want to cache the data as part of your user management, I suggest you implement this method to make sure the remaining data is fetched whenever this method is called.

dbrumann
  • 16,803
  • 2
  • 42
  • 58