Skip to content

Instantly share code, notes, and snippets.

@mikemix
Last active November 11, 2022 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikemix/04a71de0f952a2cbc517c7bfb430f1a3 to your computer and use it in GitHub Desktop.
Save mikemix/04a71de0f952a2cbc517c7bfb430f1a3 to your computer and use it in GitHub Desktop.
<?php
$user = new User();
$user->setName('John Doe');
// put user to cache
$entityManager->persist($user);
$entityManager->flush();
$entityManager->clear();
unset($user);
// load user from the cache
// avoid database call
$user = $entityManager->find(User::class, 1);
// create a new post
$post = new Post();
$post->setTitle('Awesome title');
$user->addPost($post);
$entityManager->persist($post);
$entityManager->flush();
$entityManager->clear();
// load user posts from the cache
// avoid database call
foreach ($user->getPosts() as $post) {
echo $post->getTitle();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment