Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Respect default values in N:M relations
  • Loading branch information
m-kuhn committed Nov 7, 2017
1 parent 2eddb03 commit e5187b2
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/gui/qgsrelationeditorwidget.cpp
Expand Up @@ -28,6 +28,7 @@
#include "qgsproject.h"
#include "qgstransactiongroup.h"
#include "qgslogger.h"
#include "qgsvectorlayerutils.h"

#include <QHBoxLayout>
#include <QLabel>
Expand Down Expand Up @@ -351,19 +352,28 @@ void QgsRelationEditorWidget::linkFeature()
QgsFeature relatedFeature;

QgsFeatureList newFeatures;
QgsFeature linkFeature( mRelation.referencingLayer()->fields() );

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

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

QgsAttributeMap linkAttributes;
Q_FOREACH ( const QgsRelation::FieldPair &fieldPair, mRelation.fieldPairs() )
{
linkFeature.setAttribute( fieldPair.first, mFeature.attribute( fieldPair.second ) );
int index = fields.indexOf( fieldPair.first );
linkAttributes.insert( index, mFeature.attribute( fieldPair.second ) );
}

while ( it.nextFeature( relatedFeature ) )
{
Q_FOREACH ( const QgsRelation::FieldPair &fieldPair, mNmRelation.fieldPairs() )
{
linkFeature.setAttribute( fieldPair.first, relatedFeature.attribute( fieldPair.second ) );
int index = fields.indexOf( fieldPair.first );
linkAttributes.insert( index, relatedFeature.attribute( fieldPair.second ) );
}
const QgsFeature linkFeature = QgsVectorLayerUtils::createFeature( mRelation.referencingLayer(), QgsGeometry(), linkAttributes, &context );

newFeatures << linkFeature;
}
Expand Down

0 comments on commit e5187b2

Please sign in to comment.