Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8120625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:12:22+00:00 2026-06-06T05:12:22+00:00

I want add new Feed item on entity persist and update. I write this

  • 0

I want add new Feed item on entity persist and update. I write this event listener (postUpdate is same):

public function postPersist(LifecycleEventArgs $args)
{
    $entity = $args->getEntity();
    $em = $args->getEntityManager();

    if ($entity instanceof FeedItemInterface) {
        $feed = new FeedEntity();
        $feed->setTitle($entity->getFeedTitle());
        $feed->setEntity($entity->getFeedEntityId());
        $feed->setType($entity->getFeedType());
        if($entity->isFeedTranslatable()) {
            $feed->getEnTranslation()->setTitle($entity->getFeedTitle('en'));
        }
        $em->persist($feed);
        $em->flush();
    }
}

But I got

Integrity constraint violation: 1062 Duplicate entry ’30-2′ for key
‘PRIMARY’

and in log a have two insertations:

INSERT INTO interview_scientificdirection (interview_id,
scientificdirection_id) VALUES (?, ?) ([30,2]) INSERT INTO
interview_scientificdirection (interview_id, scientificdirection_id)
VALUES (?, ?) ([30,2])

scientificdirection is Many to Many relationship table for entity what we want to persist.
In frontend application everything work fine, but in Sonata Admin I got this problem 🙁

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T05:12:23+00:00Added an answer on June 6, 2026 at 5:12 am

    If you need to persist additional objects, the postPersist or postUpdate handler in Doctrine is, sadly, not the right place to go. I struggled with the same problem today, as I needed to generate some message entries in that handler.

    The problem at this point is that the postPersist handler is called during the flush event, and not after. So you can’t persist additional objects here, as they are not getting flushed afterwards. Additionally, you can’t call flush during an postPersist handler, as this might lead to ducplicate entries (as you have experienced).

    One way to go is using the onFlush handler from doctrine, documented here: https://www.doctrine-project.org/projects/doctrine-orm/en/2.7/reference/events.html#onflush

    This is just problematic if you need the inserted ids of the database object, as the entity hasn’t yet been written to the database in that handler. If you don’t need those ids, you are fine with the onFlush event in doctrine.

    For me, the solution was a little different. I’m currently working on a symfony2 project, and needed the ids of the inserted database objects (for callbacks and updates later on).

    I created a new service in symfony2, which basically just acts like a queue for my messages. During the postPersist update, I just fill the entries in the queue. I have another handler registered on kernel.response, which then takes those entries and persists them to the database. (Something along the line of this: http://symfony.com/doc/current/cookbook/service_container/event_listener.html)

    I hope I don’t digress too much from the topic here, but as it is something I really struggled with, I hope that some people might find this useful.

    The service entries for this are:

     amq_messages_chain:
       class: Acme\StoreBundle\Listener\AmqMessagesChain
    
     amqflush:
       class: Acme\StoreBundle\Listener\AmqFlush
       arguments: [ @doctrine.orm.entity_manager, @amq_messages_chain, @logger ]
       tags:
         - { name: kernel.event_listener, event: kernel.response, method: onResponse, priority: 5 }
    
     doctrine.listener:
      class: Acme\StoreBundle\Listener\AmqListener
      arguments: [ @logger, @amq_messages_chain ]
      tags:
        - { name: doctrine.event_listener, event: postPersist }
        - { name: doctrine.event_listener, event: postUpdate }
        - { name: doctrine.event_listener, event: prePersist }
    

    You can’t use the doctrine.listener for this, as this leads to a circular dependency (as you need the entity manager for the service, but the entity manager needs the service….)

    That worked like a charm. If you need more info on that, don’t hesitate to ask, I’m glad to add some examples to this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I got this RSS feed which I want to add to a certain
I want to add new obj of JSON like: 128: { Msg: [{ me:
I've following Grid to display data, now when I want to add new record
I want to add a new tab page for every newly opened form. Example:
I want to add a new filter to the Project Explorer, to hide from
I want to add a new feature in a wxPython class. I also want
I want to add a new Stage called field to the default one (i
I want to add a new form to the edit customer page, so far
I have master branch of the project. Then I want to add a new
I want to be able to add new sections (via the 'add' link) and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.