mirror of
https://github.com/doctrine/orm.git
synced 2026-03-24 06:52:09 +01:00
DDC-3696: flushing traversable objects #4538
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @doctrinebot on GitHub (Apr 17, 2015).
Originally assigned to: @beberlei on GitHub.
Jira issue originally created by user mw-orm:
Hi,
https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/UnitOfWork.php#L331
it would be easier if you could the method "flush" pass a traversable object. I've implement this behavior in my decorated entity manager. In my opinion this should be an official supported behavior.
The new code should be inside of Orm\UnitOfWork::commit($entity = null) and looks like the following code:
By the way, the foreach part can be simplified to one line. This can be made possible by array_walk.
You can use:
Now, the new code in one piece:
Thanks in advance. :-)
@doctrinebot commented on GitHub (Apr 17, 2015):
Comment created by @ocramius:
Two things:
flush(), as it was exploited for purposes it wasn't designed for.@doctrinebot commented on GitHub (Apr 17, 2015):
Comment created by mw-orm:
1.Is that really planned? I guess this is a helpful feature and makes not a difference if you flush a small collection of entities before you flush the remaining entities without the parameter.
This simple senseless code shows you what you can do to test this behavior.
@doctrinebot commented on GitHub (Apr 17, 2015):
Comment created by @ocramius:
You can implement the
Traversableinterface (as iterator or iteratoraggregate) on any entity.As for "planned", it obviously won't happen in 2.x.
We are mainly interested in finding better ways to speed up
flush()operations rather than delegating transactional boundaries knowledge to the user.Anyway, so far,
flush($entities)has been more of a source of bugs rather than an actual useful API.@doctrinebot commented on GitHub (Apr 18, 2015):
Comment created by mw-orm:
The Traversable interface serves to detect whether a class is traversable using foreach. The flush parameter surrenders this parameter one-to-one to $this->unitOfWork->commit($entity).
What I want to do is to hand over an traversable object to the method EntityManager::flush with a collection of entities instead of an single entity object. It's also possible to pass a collection of entities as array. But it's not possible to pass a collection of entities as an object, because when passing an object UnitOfWork::commit assumes that it's an entity. To implement the Traversable interface on any entity will not help and is not the same intention. Because an Traversable object it means, that is a collection of entities like an array.
In my opinion there are not objections that speak against it to support traversable object (SplObjectStorage, ArrayIterator, ArrayObject).
There are two ways to do that.
The first option would be:
The second option would be:
By the way: the SplObjectStorage object has the benefit that every entity is present only once.