Skip to content
Permalink
Browse files

N:M Relations, support for linking table with default values

  • Loading branch information
m-kuhn committed Nov 14, 2017
1 parent 30eec2b commit 0c35dde461a94c41e482f37259f8fed7db61791c
Showing with 19 additions and 4 deletions.
  1. +19 −4 src/gui/qgsrelationeditorwidget.cpp
@@ -306,12 +306,27 @@ void QgsRelationEditorWidget::addFeature()
QgsFeature f;
if ( vlTools->addFeature( mNmRelation.referencedLayer(), QgsAttributeMap(), QgsGeometry(), &f ) )
{
QgsFeature flink( mRelation.referencingLayer()->fields() ); // Linking feature
// Fields of the linking table
const QgsFields fields = mRelation.referencingLayer()->fields();

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

flink.setAttribute( mRelation.fieldPairs().at( 0 ).first, mFeature.attribute( mRelation.fieldPairs().at( 0 ).second ) );
flink.setAttribute( mNmRelation.referencingFields().at( 0 ), f.attribute( mNmRelation.referencedFields().at( 0 ) ) );
QgsAttributeMap linkAttributes;
Q_FOREACH ( const QgsRelation::FieldPair &fieldPair, mRelation.fieldPairs() )
{
int index = fields.indexOf( fieldPair.first );
linkAttributes.insert( index, mFeature.attribute( fieldPair.second ) );
}

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

mRelation.referencingLayer()->addFeature( flink );
mRelation.referencingLayer()->addFeature( linkFeature );

updateUi();
}

0 comments on commit 0c35dde

Please sign in to comment.
You can’t perform that action at this time.