🎨 Synchronize the related entries when the database creates a two-way relation https://github.com/siyuan-note/siyuan/issues/11250

This commit is contained in:
Daniel 2024-05-06 13:44:37 +08:00
parent ce47fd2ed4
commit bf4b9cf146
No known key found for this signature in database
GPG key ID: 86211BA83DF03017

View file

@ -1577,21 +1577,22 @@ func updateAttributeViewColRelation(operation *Operation) (err error) {
} }
} }
if !destAdded { if !destAdded && operation.IsTwoWay {
if operation.IsTwoWay { // 新建双向关联目标字段
name := strings.TrimSpace(operation.Name) name := strings.TrimSpace(operation.Name)
if "" == name { if "" == name {
name = srcAv.Name + " " + operation.Format name = srcAv.Name + " " + operation.Format
} }
destAv.KeyValues = append(destAv.KeyValues, &av.KeyValues{ destKeyValues := &av.KeyValues{
Key: &av.Key{ Key: &av.Key{
ID: operation.BackRelationKeyID, ID: operation.BackRelationKeyID,
Name: name, Name: name,
Type: av.KeyTypeRelation, Type: av.KeyTypeRelation,
Relation: &av.Relation{AvID: operation.AvID, IsTwoWay: operation.IsTwoWay, BackKeyID: operation.KeyID}, Relation: &av.Relation{AvID: operation.AvID, IsTwoWay: operation.IsTwoWay, BackKeyID: operation.KeyID},
}, },
}) }
destAv.KeyValues = append(destAv.KeyValues, destKeyValues)
for _, v := range destAv.Views { for _, v := range destAv.Views {
switch v.LayoutType { switch v.LayoutType {
@ -1599,6 +1600,24 @@ func updateAttributeViewColRelation(operation *Operation) (err error) {
v.Table.Columns = append(v.Table.Columns, &av.ViewTableColumn{ID: operation.BackRelationKeyID}) v.Table.Columns = append(v.Table.Columns, &av.ViewTableColumn{ID: operation.BackRelationKeyID})
} }
} }
now := time.Now().UnixMilli()
// 和现有值进行关联
for _, keyValues := range srcAv.KeyValues {
if keyValues.Key.ID != operation.KeyID {
continue
}
srcKeyValues := keyValues
for _, srcVal := range srcKeyValues.Values {
for _, blockID := range srcVal.Relation.BlockIDs {
destVal := &av.Value{ID: ast.NewNodeID(), KeyID: destKeyValues.Key.ID, BlockID: blockID, Type: keyValues.Key.Type, Relation: &av.ValueRelation{}}
destVal.Relation.BlockIDs = append(destVal.Relation.BlockIDs, srcVal.BlockID)
destVal.Relation.BlockIDs = gulu.Str.RemoveDuplicatedElem(destVal.Relation.BlockIDs)
destVal.SetUpdatedAt(now)
destKeyValues.Values = append(destKeyValues.Values, destVal)
}
}
} }
} }
@ -3195,10 +3214,10 @@ func UpdateAttributeViewCell(tx *Transaction, avID, keyID, rowID, cellID string,
val.SetUpdatedAt(now) val.SetUpdatedAt(now)
key, _ := attrView.GetKey(val.KeyID) key, _ := attrView.GetKey(val.KeyID)
if nil != key && av.KeyTypeRelation == key.Type && nil != key.Relation { if nil != key && av.KeyTypeRelation == key.Type && nil != key.Relation && key.Relation.IsTwoWay {
destAv, _ := av.ParseAttributeView(key.Relation.AvID) // 双向关联需要同时更新目标字段的值
if nil != destAv {
if key.Relation.IsTwoWay { if destAv, _ := av.ParseAttributeView(key.Relation.AvID); nil != destAv {
// relationChangeMode // relationChangeMode
// 0关联列值不变仅排序不影响目标值 // 0关联列值不变仅排序不影响目标值
// 1关联列值增加增加目标值 // 1关联列值增加增加目标值
@ -3254,7 +3273,6 @@ func UpdateAttributeViewCell(tx *Transaction, avID, keyID, rowID, cellID string,
av.SaveAttributeView(destAv) av.SaveAttributeView(destAv)
} }
} }
}
relatedAvIDs := av.GetSrcAvIDs(avID) relatedAvIDs := av.GetSrcAvIDs(avID)
for _, relatedAvID := range relatedAvIDs { for _, relatedAvID := range relatedAvIDs {