Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix AbstractRelationEditorWidget not updating/inserting referenced layer
QgsAbstractRelationEditorWidget need to update/insert the referenced
layer field also when an nm relation is used. This was not the case
for addFeature and linkFeature methods.
  • Loading branch information
domi4484 authored and 3nids committed Mar 29, 2021
1 parent d856156 commit 061aeb8
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions src/gui/qgsabstractrelationeditorwidget.cpp
Expand Up @@ -213,6 +213,16 @@ void QgsAbstractRelationEditorWidget::addFeature( const QgsGeometry &geometry )

const QgsVectorLayerTools *vlTools = mEditorContext.vectorLayerTools();

// Fields of the linking table
const QgsFields fields = mRelation.referencingLayer()->fields();

// For generated relations insert the referenced layer field
if ( mRelation.type() == QgsRelation::Generated )
{
QgsPolymorphicRelation polyRel = mRelation.polymorphicRelation();
keyAttrs.insert( fields.indexFromName( polyRel.referencedLayerField() ), polyRel.layerRepresentation( mRelation.referencedLayer() ) );
}

if ( mNmRelation.isValid() )
{
// only normal relations support m:n relation
Expand All @@ -224,13 +234,10 @@ void QgsAbstractRelationEditorWidget::addFeature( const QgsGeometry &geometry )
if ( !vlTools->addFeature( mNmRelation.referencedLayer(), QgsAttributeMap(), geometry, &f ) )
return;

// Fields of the linking table
const QgsFields fields = mRelation.referencingLayer()->fields();

// Expression context for the linking table
QgsExpressionContext context = mRelation.referencingLayer()->createExpressionContext();

QgsAttributeMap linkAttributes;
QgsAttributeMap linkAttributes = keyAttrs;
const auto constFieldPairs = mRelation.fieldPairs();
for ( const QgsRelation::FieldPair &fieldPair : constFieldPairs )
{
Expand All @@ -250,13 +257,6 @@ void QgsAbstractRelationEditorWidget::addFeature( const QgsGeometry &geometry )
}
else
{
QgsFields fields = mRelation.referencingLayer()->fields();
if ( mRelation.type() == QgsRelation::Generated )
{
QgsPolymorphicRelation polyRel = mRelation.polymorphicRelation();
keyAttrs.insert( fields.indexFromName( polyRel.referencedLayerField() ), polyRel.layerRepresentation( mRelation.referencedLayer() ) );
}

const auto constFieldPairs = mRelation.fieldPairs();
for ( const QgsRelation::FieldPair &fieldPair : constFieldPairs )
{
Expand Down Expand Up @@ -440,6 +440,16 @@ void QgsAbstractRelationEditorWidget::onLinkFeatureDlgAccepted()
QgsExpressionContext context = mRelation.referencingLayer()->createExpressionContext();

QgsAttributeMap linkAttributes;

if ( mRelation.type() == QgsRelation::Generated )
{
QgsPolymorphicRelation polyRel = mRelation.polymorphicRelation();
Q_ASSERT( polyRel.isValid() );

linkAttributes.insert( fields.indexFromName( polyRel.referencedLayerField() ),
polyRel.layerRepresentation( mRelation.referencedLayer() ) );
}

const auto constFieldPairs = mRelation.fieldPairs();
for ( const QgsRelation::FieldPair &fieldPair : constFieldPairs )
{
Expand Down Expand Up @@ -486,7 +496,7 @@ void QgsAbstractRelationEditorWidget::onLinkFeatureDlgAccepted()
{
QgsPolymorphicRelation polyRel = mRelation.polymorphicRelation();

Q_ASSERT( mRelation.polymorphicRelation().isValid() );
Q_ASSERT( polyRel.isValid() );

mRelation.referencingLayer()->changeAttributeValue( fid,
referencingLayer->fields().indexFromName( polyRel.referencedLayerField() ),
Expand Down

0 comments on commit 061aeb8

Please sign in to comment.