@@ -248,29 +248,36 @@ void FireNestedBeforeUpdateHooks(ResourcePipeline pipeline, EntityChildLayer lay
248
248
IEnumerable uniqueEntities = node . UniqueEntities ;
249
249
DependentType entityType = node . EntityType ;
250
250
251
- // fire the BeforeUpdateRelationship hook for o1
251
+ // fire the BeforeUpdateRelationship hook for o2
252
252
if ( nestedHookcontainer != null )
253
253
{
254
254
if ( uniqueEntities . Cast < IIdentifiable > ( ) . Any ( ) )
255
255
{
256
256
var relationships = node . RelationshipsToNextLayer . Select ( p => p . Attribute ) . ToArray ( ) ;
257
257
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 ) ;
259
263
var allowedIds = CallHook ( nestedHookcontainer , ResourceHook . BeforeUpdateRelationship , new object [ ] { GetIds ( uniqueEntities ) , resourcesByRelationship , pipeline } ) . Cast < string > ( ) ;
260
264
var updated = GetAllowedEntities ( uniqueEntities , allowedIds ) ;
261
265
node . UpdateUnique ( updated ) ;
262
266
node . Reassign ( ) ;
263
267
}
264
268
}
265
269
266
- // fire the BeforeImplicitUpdateRelationship hook for o1
270
+ // fire the BeforeImplicitUpdateRelationship hook for o1
267
271
var implicitPrincipalTargets = node . RelationshipsFromPreviousLayer . GetPrincipalEntities ( ) ;
268
272
if ( pipeline != ResourcePipeline . Post && implicitPrincipalTargets . Any ( ) )
269
273
{
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
270
277
FireForAffectedImplicits ( entityType , implicitPrincipalTargets , pipeline , uniqueEntities ) ;
271
278
}
272
279
273
- // fire the BeforeImplicitUpdateRelationship hook for a2
280
+ // fire the BeforeImplicitUpdateRelationship hook for a2
274
281
var dependentEntities = node . RelationshipsFromPreviousLayer . GetDependentEntities ( ) ;
275
282
if ( dependentEntities . Any ( ) )
276
283
{
@@ -290,7 +297,8 @@ void FireForAffectedImplicits(Type entityTypeToInclude, Dictionary<RelationshipA
290
297
if ( container == null ) return ;
291
298
var implicitAffected = _executorHelper . LoadImplicitlyAffected ( implicitsTarget , existingImplicitEntities ) ;
292
299
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 ) ;
294
302
CallHook ( container , ResourceHook . BeforeImplicitUpdateRelationship , new object [ ] { resourcesByRelationship , pipeline , } ) ;
295
303
}
296
304
@@ -366,8 +374,8 @@ Dictionary<RelationshipAttribute, IEnumerable> ReplaceWithDbValues(Dictionary<Re
366
374
{
367
375
foreach ( var key in prevLayerRelationships . Keys . ToList ( ) )
368
376
{
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 ) ;
371
379
}
372
380
return prevLayerRelationships ;
373
381
}
0 commit comments