1919
2020namespace Doctrine \ODM \MongoDB ;
2121
22- use Doctrine \Common \Collections \ArrayCollection ;
2322use Doctrine \Common \Collections \Collection ;
2423use Doctrine \Common \EventManager ;
2524use Doctrine \Common \NotifyPropertyChanged ;
2625use Doctrine \Common \PropertyChangedListener ;
27- use Doctrine \MongoDB \GridFSFile ;
2826use Doctrine \ODM \MongoDB \ChangeSet \ChangeSetCalculator ;
27+ use Doctrine \ODM \MongoDB \ChangeSet \FieldChange ;
2928use Doctrine \ODM \MongoDB \ChangeSet \LegacyChangeSetCalculator ;
29+ use Doctrine \ODM \MongoDB \ChangeSet \ObjectChangeSet ;
3030use Doctrine \ODM \MongoDB \Hydrator \HydratorFactory ;
3131use Doctrine \ODM \MongoDB \Mapping \ClassMetadata ;
3232use Doctrine \ODM \MongoDB \PersistentCollection \PersistentCollectionInterface ;
3333use Doctrine \ODM \MongoDB \Persisters \PersistenceBuilder ;
3434use Doctrine \ODM \MongoDB \Proxy \Proxy ;
3535use Doctrine \ODM \MongoDB \Query \Query ;
36- use Doctrine \ODM \MongoDB \Types \Type ;
3736use Doctrine \ODM \MongoDB \Utility \CollectionHelper ;
3837use Doctrine \ODM \MongoDB \Utility \LifecycleEventManager ;
3938
@@ -582,25 +581,25 @@ private function computeSingleDocumentChangeSet($document)
582581 * Gets the changeset for a document.
583582 *
584583 * @param object $document
585- * @return array array('property' => array(0 => mixed |null, 1 => mixed|null))
584+ * @return ObjectChangeSet |null
586585 */
587586 public function getDocumentChangeSet ($ document )
588587 {
589588 $ oid = spl_object_hash ($ document );
590589 if (isset ($ this ->documentChangeSets [$ oid ])) {
591590 return $ this ->documentChangeSets [$ oid ];
592591 }
593- return array () ;
592+ return null ;
594593 }
595594
596595 /**
597596 * INTERNAL:
598597 * Sets the changeset for a document.
599598 *
600599 * @param object $document
601- * @param array $changeset
600+ * @param ObjectChangeSet $changeset
602601 */
603- public function setDocumentChangeSet ($ document , $ changeset )
602+ public function setDocumentChangeSet ($ document , ObjectChangeSet $ changeset )
604603 {
605604 $ this ->documentChangeSets [spl_object_hash ($ document )] = $ changeset ;
606605 }
@@ -670,15 +669,19 @@ private function computeOrRecomputeChangeSet(ClassMetadata $class, $document, $r
670669 if ($ isChangeTrackingNotify && ! $ recompute && isset ($ this ->documentChangeSets [$ oid ])) {
671670 $ changeSet = $ this ->documentChangeSets [$ oid ];
672671 } else {
673- $ changeSet = array ( );
672+ $ changeSet = new ObjectChangeSet ( $ document , [] );
674673 }
675674
676675 $ changeSet = $ this ->changeSetCalculator ->calculate ($ document , $ class , $ isNewDocument ? null : $ this ->originalDocumentData [$ oid ], $ changeSet );
677676
678- if ($ changeSet ) {
679- $ this ->documentChangeSets [$ oid ] = isset ($ this ->documentChangeSets [$ oid ])
680- ? $ changeSet + $ this ->documentChangeSets [$ oid ]
681- : $ changeSet ;
677+ if (count ($ changeSet )) {
678+ if (isset ($ this ->documentChangeSets [$ oid ])) {
679+ foreach ($ changeSet as $ field => $ change ) {
680+ $ this ->documentChangeSets [$ oid ][$ field ] = $ change ;
681+ }
682+ } else {
683+ $ this ->documentChangeSets [$ oid ] = $ changeSet ;
684+ }
682685
683686 $ this ->originalDocumentData [$ oid ] = $ this ->changeSetCalculator ->getDocumentActualData ($ document , $ class );
684687 if (! $ isNewDocument ) {
@@ -711,7 +714,10 @@ function ($assoc) { return empty($assoc['notSaved']); }
711714 $ oid2 = spl_object_hash ($ obj );
712715
713716 if (isset ($ this ->documentChangeSets [$ oid2 ])) {
714- $ this ->documentChangeSets [$ oid ][$ mapping ['fieldName ' ]] = array ($ value , $ value );
717+ if (! isset ($ this ->documentChangeSets [$ oid ])) {
718+ $ this ->documentChangeSets [$ oid ] = new ObjectChangeSet ($ document , []);
719+ }
720+ $ this ->documentChangeSets [$ oid ][$ mapping ['fieldName ' ]] = new FieldChange ($ value , $ value );
715721
716722 if ( ! $ isNewDocument ) {
717723 $ this ->scheduleForUpdate ($ document );
@@ -1034,7 +1040,7 @@ private function executeUpdates(ClassMetadata $class, array $documents, array $o
10341040 foreach ($ documents as $ oid => $ document ) {
10351041 $ this ->lifecycleEventManager ->preUpdate ($ class , $ document );
10361042
1037- if ( ! empty ($ this ->documentChangeSets [$ oid ]) || $ this ->hasScheduledCollections ($ document )) {
1043+ if (! empty ($ this ->documentChangeSets [$ oid ]) || $ this ->hasScheduledCollections ($ document )) {
10381044 $ persister ->update ($ document , $ options );
10391045 }
10401046
@@ -2698,7 +2704,7 @@ public function registerManaged($document, $id, array $data)
26982704 */
26992705 public function clearDocumentChangeSet ($ oid )
27002706 {
2701- $ this ->documentChangeSets [$ oid ] = array ( );
2707+ unset( $ this ->documentChangeSets [$ oid ]);
27022708 }
27032709
27042710 /* PropertyChangedListener implementation */
@@ -2721,7 +2727,10 @@ public function propertyChanged($document, $propertyName, $oldValue, $newValue)
27212727 }
27222728
27232729 // Update changeset and mark document for synchronization
2724- $ this ->documentChangeSets [$ oid ][$ propertyName ] = array ($ oldValue , $ newValue );
2730+ if (! isset ($ this ->documentChangeSets [$ oid ])) {
2731+ $ this ->documentChangeSets [$ oid ] = new ObjectChangeSet ($ document , []);
2732+ }
2733+ $ this ->documentChangeSets [$ oid ][$ propertyName ] = new FieldChange ($ oldValue , $ newValue );
27252734 if ( ! isset ($ this ->scheduledForDirtyCheck [$ class ->name ][$ oid ])) {
27262735 $ this ->scheduleForDirtyCheck ($ document );
27272736 }
0 commit comments