Le 28/04/2016 17:31, dunaeth a écrit :
Hi,
I'm facing a small trouble with SF2 and doctrine, the former calling
Collection->clear method when submitting empty multiple entities
forms, the latter taking a snapshot when the clear method is called on
PersistentCollections.
Since I want to perform actions when there are modifications on some
entities, I need to know the changes implied by the collection clear
calls.
So I tried something, which is probably a non-sense doctrine wise and
prone to errors, by detaching the collection owner entity, fetching it
again from db, comparing collections and merging the detached entity
back in UoW in the onFlush event. Everything seems fine till UoW tries
to persist the entity. It could be summarized as :
|
$eventManager->addEventListener(Events::onFlush,function(OnFlushEventArgs$args){
$em =$args->getEntityManager();
$uow =$em->getUnitOfWork();
foreach($uow->getScheduledCollectionDeletions()as$collection){
if(!$collection->count()){
$owner =$collection->getOwner();
$uow->detach($owner);
$refOwner =$em->find(get_class($owner),$owner->getId());
// compare $collection and $refOwner's collection
$uow->merge($owner);
}
}
});
|
I know this is / seems to be tricky, confirmed by the PDO Error
Invalid parameter number : no parameters were bound, but I must find a
trick anyway.
You are misunderstand what ->merge() is doing. This is not putting the
$owner object back in the unit of work. It is returning a new instance
which is managed and in which the data of $owner have been merged. The
fact that you don't care about the return value of merge is a clear sign
that it is misused.
And the fact of detaching and merging entities during the onFlush event
is not supported AFAIK, as you are changing the unit of work too late in
the flushing process. This means that getting weird errors is not
unexpected.
--
Christophe | Stof
--
You received this message because you are subscribed to the Google Groups
"doctrine-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/doctrine-user.
For more options, visit https://groups.google.com/d/optout.