Skip to content

Commit 0c35dde

Browse files
committedNov 14, 2017
N:M Relations, support for linking table with default values
1 parent 30eec2b commit 0c35dde

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed
 

‎src/gui/qgsrelationeditorwidget.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -306,12 +306,27 @@ void QgsRelationEditorWidget::addFeature()
306306
QgsFeature f;
307307
if ( vlTools->addFeature( mNmRelation.referencedLayer(), QgsAttributeMap(), QgsGeometry(), &f ) )
308308
{
309-
QgsFeature flink( mRelation.referencingLayer()->fields() ); // Linking feature
309+
// Fields of the linking table
310+
const QgsFields fields = mRelation.referencingLayer()->fields();
311+
312+
// Expression context for the linking table
313+
QgsExpressionContext context = mRelation.referencingLayer()->createExpressionContext();
310314

311-
flink.setAttribute( mRelation.fieldPairs().at( 0 ).first, mFeature.attribute( mRelation.fieldPairs().at( 0 ).second ) );
312-
flink.setAttribute( mNmRelation.referencingFields().at( 0 ), f.attribute( mNmRelation.referencedFields().at( 0 ) ) );
315+
QgsAttributeMap linkAttributes;
316+
Q_FOREACH ( const QgsRelation::FieldPair &fieldPair, mRelation.fieldPairs() )
317+
{
318+
int index = fields.indexOf( fieldPair.first );
319+
linkAttributes.insert( index, mFeature.attribute( fieldPair.second ) );
320+
}
321+
322+
Q_FOREACH ( const QgsRelation::FieldPair &fieldPair, mNmRelation.fieldPairs() )
323+
{
324+
int index = fields.indexOf( fieldPair.first );
325+
linkAttributes.insert( index, f.attribute( fieldPair.second ) );
326+
}
327+
QgsFeature linkFeature = QgsVectorLayerUtils::createFeature( mRelation.referencingLayer(), QgsGeometry(), linkAttributes, &context );
313328

314-
mRelation.referencingLayer()->addFeature( flink );
329+
mRelation.referencingLayer()->addFeature( linkFeature );
315330

316331
updateUi();
317332
}

0 commit comments

Comments
 (0)
Please sign in to comment.