// Creating a new entity object $user = new User(); $user->setName('John Doe'); $user->setEmail('john@example.com'); // Getting the ObjectManager from the Doctrine entity manager $objectManager = $entityManager->getObjectManager(); // Persisting the new entity $objectManager->persist($user); // Flushing changes to the database $objectManager->flush();
// Getting an existing entity $user = $entityManager->getRepository(User::class)->findOneBy(['id' => 1]); if ($user) { // Updating entity property $user->setName('Jane Doe'); // Getting ObjectManager instance $objectManager = $entityManager->getObjectManager(); // Persisting the updated entity $objectManager->persist($user); // Flushing changes to the database $objectManager->flush(); }In this example, we first get an existing `User` entity with an ID of 1 from the repository. We then update the entity's name property and persist it using the `persist()` method. Finally, we flush the changes to the database. Overall, the `ObjectManager` interface provides a common set of methods for managing objects in a persistence layer, which can be used with different libraries that implement the interface.