datatables-bundle icon indicating copy to clipboard operation
datatables-bundle copied to clipboard

Doctrine ORMAdapter detach() causing duplicate records

Open samvaughton opened this issue 5 years ago • 8 comments

Hey,

Come across an issue where if I am doing doctrine flush operations in the same request as handling the callback for datatables it will duplicate all hydrated entities that are loaded.

The code in question causing this was added a few years back:

https://github.com/omines/datatables-bundle/commit/187bd14ee83488fce652e526e3efbf8491a3c58c

if (Query::HYDRATE_OBJECT === $this->hydrationMode) {
    $this->manager->detach($entity);
}

What was the reasoning behind it so I can understand context etc? If it's required then I can try and implement a workaround.

detach() on doctrine entities has always been a source of bugs if the behaviour is not fully understood. Even the doctrine documentation is incorrect on the behaviour of detach(). Please see https://github.com/doctrine/orm/issues/8007 as it wouldn't make sense for me to re-type it here.

tldr; the datatables bundle detaches any doctrine object that is has loaded, if a flush operation is performed it will duplicate the record. I am not sure if a persist is required as I have a few cascades and may have missed something.

Is the line referenced above required? It introduces potentially unwanted/unknown side effects if other doctrine operations are being performed within the same request.

If I am not being clear let me know, cheers.

samvaughton avatar Aug 15 '20 17:08 samvaughton

We detach the rows because otherwise your PHP blows up all the time when people try to paginate 500+ rows in a complex database with many eager joins, it's simply a memory preservation measure.

curry684 avatar Aug 24 '20 13:08 curry684

Copying @shades684 in on this, he's more into Doctrine than me.

curry684 avatar Aug 24 '20 13:08 curry684

Hi, The detach function will be remove in doctrine 3.0. I've got a lot of Depreciation notice for this.

Method Doctrine\ORM\EntityManager::detach() is deprecated and will be removed in Doctrine ORM 3.0.

ellmout avatar Sep 17 '20 15:09 ellmout

I don't mind removing it but we added it because in our initial use cases we actually ran into out-of-memory issues due to the entity manager blowing up.

We also don't formally support Doctrine 3.x yet (mostly for this reason).

curry684 avatar Sep 18 '20 12:09 curry684

Fixed in #198

curry684 avatar Nov 25 '20 00:11 curry684

Hi guys, so with the revert of fix 198, what do I do with the duplicate rows? Is there a way to fix this?

juaniAla avatar Jun 01 '21 12:06 juaniAla

Not fixed yet.

curry684 avatar Sep 21 '21 08:09 curry684

Hello everyone! Any news on given topic?

shumik avatar Jan 10 '22 06:01 shumik

Stale issue message

github-actions[bot] avatar Nov 23 '22 22:11 github-actions[bot]