@@ -4952,20 +4952,7 @@ public function updateDocument(string $collection, string $id, Document $documen
49524952 }
49534953 $ createdAt = $ document ->getCreatedAt ();
49544954
4955- // Extract operators from the document before merging
4956- $ documentArray = $ document ->getArrayCopy ();
4957- $ extracted = Operator::extractOperators ($ documentArray );
4958- $ operators = $ extracted ['operators ' ];
4959- $ updates = $ extracted ['updates ' ];
4960-
4961- $ operatorValidator = new OperatorValidator ($ collection , $ old );
4962- foreach ($ operators as $ attribute => $ operator ) {
4963- if (!$ operatorValidator ->isValid ($ operator )) {
4964- throw new StructureException ($ operatorValidator ->getDescription ());
4965- }
4966- }
4967-
4968- $ document = \array_merge ($ old ->getArrayCopy (), $ updates );
4955+ $ document = \array_merge ($ old ->getArrayCopy (), $ document ->getArrayCopy ());
49694956 $ document ['$collection ' ] = $ old ->getAttribute ('$collection ' ); // Make sure user doesn't switch collection ID
49704957 $ document ['$createdAt ' ] = ($ createdAt === null || !$ this ->preserveDates ) ? $ old ->getCreatedAt () : $ createdAt ;
49714958
@@ -4989,8 +4976,11 @@ public function updateDocument(string $collection, string $id, Document $documen
49894976 $ relationships [$ relationship ->getAttribute ('key ' )] = $ relationship ;
49904977 }
49914978
4992- if (!empty ($ operators )) {
4993- $ shouldUpdate = true ;
4979+ foreach ($ document as $ key => $ value ) {
4980+ if (Operator::isOperator ($ value )) {
4981+ $ shouldUpdate = true ;
4982+ break ;
4983+ }
49944984 }
49954985
49964986 // Compare if the document has any changes
@@ -5110,7 +5100,8 @@ public function updateDocument(string $collection, string $id, Document $documen
51105100 $ this ->adapter ->getIdAttributeType (),
51115101 $ this ->adapter ->getMinDateTime (),
51125102 $ this ->adapter ->getMaxDateTime (),
5113- $ this ->adapter ->getSupportForAttributes ()
5103+ $ this ->adapter ->getSupportForAttributes (),
5104+ $ old
51145105 );
51155106 if (!$ structureValidator ->isValid ($ document )) { // Make sure updated structure still apply collection rules (if any)
51165107 throw new StructureException ($ structureValidator ->getDescription ());
@@ -5120,22 +5111,24 @@ public function updateDocument(string $collection, string $id, Document $documen
51205111 $ document = $ this ->silent (fn () => $ this ->updateDocumentRelationships ($ collection , $ old , $ document ));
51215112 }
51225113
5123-
51245114 $ document = $ this ->adapter ->castingBefore ($ collection , $ document );
51255115
5126- // Re-add operators to document for adapter processing
5127- foreach ($ operators as $ key => $ operator ) {
5128- $ document ->setAttribute ($ key , $ operator );
5129- }
5130-
51315116 $ this ->adapter ->updateDocument ($ collection , $ id , $ document , $ skipPermissionsUpdate );
51325117
51335118 $ document = $ this ->adapter ->castingAfter ($ collection , $ document );
51345119
51355120 $ this ->purgeCachedDocument ($ collection ->getId (), $ id );
51365121
51375122 // If operators were used, refetch document to get computed values
5138- if (!empty ($ operators )) {
5123+ $ hasOperators = false ;
5124+ foreach ($ document ->getArrayCopy () as $ value ) {
5125+ if (Operator::isOperator ($ value )) {
5126+ $ hasOperators = true ;
5127+ break ;
5128+ }
5129+ }
5130+
5131+ if ($ hasOperators ) {
51395132 $ refetched = $ this ->refetchDocuments ($ collection , [$ document ]);
51405133 $ document = $ refetched [0 ];
51415134 }
@@ -5258,24 +5251,17 @@ public function updateDocuments(
52585251 applyDefaults: false
52595252 );
52605253
5261- // Separate operators from regular updates for validation
5262- $ extracted = Operator::extractOperators ($ updates ->getArrayCopy ());
5263- $ operators = $ extracted ['operators ' ];
5264- $ regularUpdates = $ extracted ['updates ' ];
5265-
5266- // Only validate regular updates, not operators
5267- if (!empty ($ regularUpdates )) {
5268- $ validator = new PartialStructure (
5269- $ collection ,
5270- $ this ->adapter ->getIdAttributeType (),
5271- $ this ->adapter ->getMinDateTime (),
5272- $ this ->adapter ->getMaxDateTime (),
5273- $ this ->adapter ->getSupportForAttributes ()
5274- );
5254+ $ validator = new PartialStructure (
5255+ $ collection ,
5256+ $ this ->adapter ->getIdAttributeType (),
5257+ $ this ->adapter ->getMinDateTime (),
5258+ $ this ->adapter ->getMaxDateTime (),
5259+ $ this ->adapter ->getSupportForAttributes (),
5260+ null // No old document available in bulk updates
5261+ );
52755262
5276- if (!$ validator ->isValid (new Document ($ regularUpdates ))) {
5277- throw new StructureException ($ validator ->getDescription ());
5278- }
5263+ if (!$ validator ->isValid ($ updates )) {
5264+ throw new StructureException ($ validator ->getDescription ());
52795265 }
52805266
52815267 $ originalLimit = $ limit ;
@@ -5311,17 +5297,8 @@ public function updateDocuments(
53115297 $ currentPermissions = $ updates ->getPermissions ();
53125298 sort ($ currentPermissions );
53135299
5314- $ this ->withTransaction (function () use ($ collection , $ updates , &$ batch , $ currentPermissions, $ operators ) {
5300+ $ this ->withTransaction (function () use ($ collection , $ updates , &$ batch , $ currentPermissions ) {
53155301 foreach ($ batch as $ index => $ document ) {
5316- if (!empty ($ operators )) {
5317- $ operatorValidator = new OperatorValidator ($ collection , $ document );
5318- foreach ($ operators as $ attribute => $ operator ) {
5319- if (!$ operatorValidator ->isValid ($ operator )) {
5320- throw new StructureException ($ operatorValidator ->getDescription ());
5321- }
5322- }
5323- }
5324-
53255302 $ skipPermissionsUpdate = true ;
53265303
53275304 if ($ updates ->offsetExists ('$permissions ' )) {
@@ -5369,7 +5346,15 @@ public function updateDocuments(
53695346
53705347 $ updates = $ this ->adapter ->castingBefore ($ collection , $ updates );
53715348
5372- if (!empty ($ operators )) {
5349+ $ hasOperators = false ;
5350+ foreach ($ updates ->getArrayCopy () as $ value ) {
5351+ if (Operator::isOperator ($ value )) {
5352+ $ hasOperators = true ;
5353+ break ;
5354+ }
5355+ }
5356+
5357+ if ($ hasOperators ) {
53735358 $ batch = $ this ->refetchDocuments ($ collection , $ batch );
53745359 }
53755360
@@ -6035,45 +6020,19 @@ public function upsertDocumentsWithIncrease(
60356020 }
60366021 }
60376022
6038- // Extract operators for validation
6039- $ documentArray = $ document ->getArrayCopy ();
6040- $ extracted = Operator::extractOperators ($ documentArray );
6041- $ operators = $ extracted ['operators ' ];
6042- $ regularUpdates = $ extracted ['updates ' ];
6043-
6044- $ operatorValidator = new OperatorValidator ($ collection , $ old ->isEmpty () ? null : $ old );
6045- foreach ($ operators as $ attribute => $ operator ) {
6046- if (!$ operatorValidator ->isValid ($ operator )) {
6047- throw new StructureException ($ operatorValidator ->getDescription ());
6048- }
6049- }
6050-
6051- // Create a temporary document with only regular updates for encoding and validation
6052- $ tempDocument = new Document ($ regularUpdates );
6053- $ tempDocument ->setAttribute ('$id ' , $ document ->getId ());
6054- $ tempDocument ->setAttribute ('$collection ' , $ document ->getAttribute ('$collection ' ));
6055- $ tempDocument ->setAttribute ('$createdAt ' , $ document ->getAttribute ('$createdAt ' ));
6056- $ tempDocument ->setAttribute ('$updatedAt ' , $ document ->getAttribute ('$updatedAt ' ));
6057- $ tempDocument ->setAttribute ('$permissions ' , $ document ->getAttribute ('$permissions ' ));
6058- if ($ this ->adapter ->getSharedTables ()) {
6059- $ tempDocument ->setAttribute ('$tenant ' , $ document ->getAttribute ('$tenant ' ));
6060- }
6061-
6062- $ encodedTemp = $ this ->encode ($ collection , $ tempDocument );
6063-
60646023 $ validator = new Structure (
60656024 $ collection ,
60666025 $ this ->adapter ->getIdAttributeType (),
60676026 $ this ->adapter ->getMinDateTime (),
60686027 $ this ->adapter ->getMaxDateTime (),
6069- $ this ->adapter ->getSupportForAttributes ()
6028+ $ this ->adapter ->getSupportForAttributes (),
6029+ $ old ->isEmpty () ? null : $ old
60706030 );
60716031
6072- if (!$ validator ->isValid ($ encodedTemp )) {
6032+ if (!$ validator ->isValid ($ document )) {
60736033 throw new StructureException ($ validator ->getDescription ());
60746034 }
60756035
6076- // Now encode the full document with operators for the adapter
60776036 $ document = $ this ->encode ($ collection , $ document );
60786037
60796038 if (!$ old ->isEmpty ()) {
0 commit comments