Update related Entity in doctrine / symfony

I have two entities: Article and ArticleComment. Each Article can have multiple ArticleComments. These entities get displayed inside an Angular App. The article is on the left and the comments are on the right. Below the Comments there is a counter which displays the number of comments. This comment counter is a virtual field inside my Article entity, using lifecyclecallbacks the amount of comments gets updated on PostUpdate, PostPersist and PostLoad:

/**
 * @ORMPostUpdate
 * @ORMPostPersist
 * @ORMPostLoad
 */
 public function updateArticles(LifecycleEventArgs $args): void {
   foreach($this->articleComments as $comment) {
     $this->articleCommentsTotal += $comment->getNumber();
   }
 } 

Adding a new comment works fine, but the counter only gets updated after the page is reloaded. Is there a way to tell symfony/doctrine to update the related entity as well, when there is an update on one entity?

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *