Misiur
Misiur

Reputation: 5307

Doctrine 2 - Removing object from database, but keeping the entity data

Is there a way to remove a record from database, but keep the data in entity object? I need to be able to reinsert those detached entities later. Should I work directly on UnitOfWork? Thank you.

Upvotes: 0

Views: 92

Answers (1)

olaurendeau
olaurendeau

Reputation: 669

I'm not sure of what you are asking, but this looks like the basic behavior of doctrine

$student = new Student();
$student->setName("John doe");

$this->em->persist($student);
$this->em->flush();

$this->printEntity($student);
$this->em->remove($student);
$this->em->flush();
$this->printEntity($student);

This will print out :

Student - id : "1", name : "John doe"

Student - id : "", name : "John doe"

Row is deleted from database but your entity is still populated with others data.

Upvotes: 1

Related Questions