@@ -248,29 +248,36 @@ void FireNestedBeforeUpdateHooks(ResourcePipeline pipeline, EntityChildLayer lay
248248 IEnumerable uniqueEntities = node . UniqueEntities ;
249249 DependentType entityType = node . EntityType ;
250250
251- // fire the BeforeUpdateRelationship hook for o1
251+ // fire the BeforeUpdateRelationship hook for o2
252252 if ( nestedHookcontainer != null )
253253 {
254254 if ( uniqueEntities . Cast < IIdentifiable > ( ) . Any ( ) )
255255 {
256256 var relationships = node . RelationshipsToNextLayer . Select ( p => p . Attribute ) . ToArray ( ) ;
257257 var dbValues = LoadDbValues ( entityType , uniqueEntities , ResourceHook . BeforeUpdateRelationship , relationships ) ;
258- var resourcesByRelationship = CreateRelationshipHelper ( entityType , node . RelationshipsFromPreviousLayer . GetDependentEntities ( ) , dbValues ) ;
258+
259+ var dependentByPrevLayerRelationships = node . RelationshipsFromPreviousLayer . GetDependentEntities ( ) ;
260+ var principalsByCurrentLayerRelationships = dependentByPrevLayerRelationships . ToDictionary ( kvp => _graph . GetInverseRelationship ( kvp . Key ) , kvp => kvp . Value ) ;
261+
262+ var resourcesByRelationship = CreateRelationshipHelper ( entityType , principalsByCurrentLayerRelationships , dbValues ) ;
259263 var allowedIds = CallHook ( nestedHookcontainer , ResourceHook . BeforeUpdateRelationship , new object [ ] { GetIds ( uniqueEntities ) , resourcesByRelationship , pipeline } ) . Cast < string > ( ) ;
260264 var updated = GetAllowedEntities ( uniqueEntities , allowedIds ) ;
261265 node . UpdateUnique ( updated ) ;
262266 node . Reassign ( ) ;
263267 }
264268 }
265269
266- // fire the BeforeImplicitUpdateRelationship hook for o1
270+ // fire the BeforeImplicitUpdateRelationship hook for o1
267271 var implicitPrincipalTargets = node . RelationshipsFromPreviousLayer . GetPrincipalEntities ( ) ;
268272 if ( pipeline != ResourcePipeline . Post && implicitPrincipalTargets . Any ( ) )
269273 {
274+ // value in implicitPrincipalTargets is a1 here.
275+ // we need to load the current value in db, which is o1.
276+ // then we need to inverse the relationship attribute
270277 FireForAffectedImplicits ( entityType , implicitPrincipalTargets , pipeline , uniqueEntities ) ;
271278 }
272279
273- // fire the BeforeImplicitUpdateRelationship hook for a2
280+ // fire the BeforeImplicitUpdateRelationship hook for a2
274281 var dependentEntities = node . RelationshipsFromPreviousLayer . GetDependentEntities ( ) ;
275282 if ( dependentEntities . Any ( ) )
276283 {
@@ -290,7 +297,8 @@ void FireForAffectedImplicits(Type entityTypeToInclude, Dictionary<RelationshipA
290297 if ( container == null ) return ;
291298 var implicitAffected = _executorHelper . LoadImplicitlyAffected ( implicitsTarget , existingImplicitEntities ) ;
292299 if ( ! implicitAffected . Any ( ) ) return ;
293- var resourcesByRelationship = CreateRelationshipHelper ( entityTypeToInclude , implicitAffected ) ;
300+ var inverse = implicitAffected . ToDictionary ( kvp => _graph . GetInverseRelationship ( kvp . Key ) , kvp => kvp . Value ) ;
301+ var resourcesByRelationship = CreateRelationshipHelper ( entityTypeToInclude , inverse ) ;
294302 CallHook ( container , ResourceHook . BeforeImplicitUpdateRelationship , new object [ ] { resourcesByRelationship , pipeline , } ) ;
295303 }
296304
@@ -366,8 +374,8 @@ Dictionary<RelationshipAttribute, IEnumerable> ReplaceWithDbValues(Dictionary<Re
366374 {
367375 foreach ( var key in prevLayerRelationships . Keys . ToList ( ) )
368376 {
369- var replaced = prevLayerRelationships [ key ] . Cast < IIdentifiable > ( ) . Select ( entity => dbValues . Single ( dbEntity => dbEntity . StringId == entity . StringId ) ) . Cast ( key . DependentType ) ;
370- prevLayerRelationships [ key ] = TypeHelper . CreateHashSetFor ( key . DependentType , replaced ) ;
377+ var replaced = prevLayerRelationships [ key ] . Cast < IIdentifiable > ( ) . Select ( entity => dbValues . Single ( dbEntity => dbEntity . StringId == entity . StringId ) ) . Cast ( key . PrincipalType ) ;
378+ prevLayerRelationships [ key ] = TypeHelper . CreateHashSetFor ( key . PrincipalType , replaced ) ;
371379 }
372380 return prevLayerRelationships ;
373381 }
0 commit comments