|
28 | 28 | #include "qgsproject.h"
|
29 | 29 | #include "qgstransactiongroup.h"
|
30 | 30 | #include "qgslogger.h"
|
| 31 | +#include "qgsvectorlayerutils.h" |
31 | 32 |
|
32 | 33 | #include <QHBoxLayout>
|
33 | 34 | #include <QLabel>
|
@@ -351,19 +352,28 @@ void QgsRelationEditorWidget::linkFeature()
|
351 | 352 | QgsFeature relatedFeature;
|
352 | 353 |
|
353 | 354 | QgsFeatureList newFeatures;
|
354 |
| - QgsFeature linkFeature( mRelation.referencingLayer()->fields() ); |
355 | 355 |
|
| 356 | + // Fields of the linking table |
| 357 | + const QgsFields fields = mRelation.referencingLayer()->fields(); |
| 358 | + |
| 359 | + // Expression context for the linking table |
| 360 | + QgsExpressionContext context = mRelation.referencingLayer()->createExpressionContext(); |
| 361 | + |
| 362 | + QgsAttributeMap linkAttributes; |
356 | 363 | Q_FOREACH ( const QgsRelation::FieldPair &fieldPair, mRelation.fieldPairs() )
|
357 | 364 | {
|
358 |
| - linkFeature.setAttribute( fieldPair.first, mFeature.attribute( fieldPair.second ) ); |
| 365 | + int index = fields.indexOf( fieldPair.first ); |
| 366 | + linkAttributes.insert( index, mFeature.attribute( fieldPair.second ) ); |
359 | 367 | }
|
360 | 368 |
|
361 | 369 | while ( it.nextFeature( relatedFeature ) )
|
362 | 370 | {
|
363 | 371 | Q_FOREACH ( const QgsRelation::FieldPair &fieldPair, mNmRelation.fieldPairs() )
|
364 | 372 | {
|
365 |
| - linkFeature.setAttribute( fieldPair.first, relatedFeature.attribute( fieldPair.second ) ); |
| 373 | + int index = fields.indexOf( fieldPair.first ); |
| 374 | + linkAttributes.insert( index, relatedFeature.attribute( fieldPair.second ) ); |
366 | 375 | }
|
| 376 | + const QgsFeature linkFeature = QgsVectorLayerUtils::createFeature( mRelation.referencingLayer(), QgsGeometry(), linkAttributes, &context ); |
367 | 377 |
|
368 | 378 | newFeatures << linkFeature;
|
369 | 379 | }
|
|
0 commit comments