Skip to content

Commit

Permalink
N:M Relations, support for linking table with default values
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Nov 14, 2017
1 parent 30eec2b commit 0c35dde
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/gui/qgsrelationeditorwidget.cpp
Expand Up @@ -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();
}
Expand Down

0 comments on commit 0c35dde

Please sign in to comment.