Reputation: 784
I have a Post
entity and PostType
.
Post entity have field $requireModeration
it means someone must check this post and approve it before it can be published.
When I'm editing post I want to compare old post value and new post value. And if value is changed I also want to change requireModeration
flag to true.
Can I do it through form events?
Something like this:
public function postSubmit(FormEvent $event)
{
$newPost = $event->getData();
$newContent = $post->getContent(); // return new contant of post
$oldPost = ... // here I want to get old post
$oldContent = $oldPost->getContent();
if($newContent != $oldContent) {
// ...
}
}
But unfortunately I can get only new, just sent data through FormEvent object.
Upvotes: 2
Views: 5256
Reputation: 69
public function postSubmit(FormEvent $event)
{
$newPost = $event->getData();
$newContent = $newPost->getContent();
$uow = $this->em->getUnitOfWork();
$oldPost = $uow->getOriginalEntityData($post);
$oldContent = $OriginalEntityData["content"];
if($newContent != $oldContent) {
// ...
}
}
Upvotes: 3
Reputation: 2694
You should use Doctrine's lifecycle events and UnitOfWork
for this instead of Form events (I assume that you use Doctrine
at your project).
Add preUpdate
listener to your Post
entity and do something like:
$uow = $em->getUnitOfWork();
$changeset = $uow->getEntityChangeSet($entity);
In $changeset
variable you will have fields list that was changed in the $entity
during current request with their old and new values.
Upvotes: 3