@@ -63,7 +63,11 @@ void QgsVectorLayerJoinBuffer::cacheJoinLayer( QgsVectorJoinInfo& joinInfo )
6363 QgsVectorLayer* cacheLayer = dynamic_cast <QgsVectorLayer*>( QgsMapLayerRegistry::instance ()->mapLayer ( joinInfo.joinLayerId ) );
6464 if ( cacheLayer )
6565 {
66- int joinFieldIndex = cacheLayer->pendingFields ().indexFromName ( joinInfo.joinFieldName );
66+ int joinFieldIndex;
67+ if ( joinInfo.joinFieldName .isEmpty () )
68+ joinFieldIndex = joinInfo.joinFieldIndex ; // for compatibility with 1.x
69+ else
70+ joinFieldIndex = cacheLayer->pendingFields ().indexFromName ( joinInfo.joinFieldName );
6771
6872 joinInfo.cachedAttributes .clear ();
6973
@@ -88,15 +92,17 @@ void QgsVectorLayerJoinBuffer::updateFields( QgsFields& fields )
8892 continue ;
8993 }
9094
91- joinIt->tmpTargetField = fields.indexFromName ( joinIt->targetFieldName );
92-
9395 const QgsFields& joinFields = joinLayer->pendingFields ();
94- joinIt->tmpJoinField = joinFields.indexFromName ( joinIt->joinFieldName );
96+ QString joinFieldName;
97+ if ( joinIt->joinFieldName .isEmpty () && joinIt->joinFieldIndex >= 0 && joinIt->joinFieldIndex < joinFields.count () )
98+ joinFieldName = joinFields.field ( joinIt->joinFieldIndex ).name (); // for compatibility with 1.x
99+ else
100+ joinFieldName = joinIt->joinFieldName ;
95101
96102 for ( int idx = 0 ; idx < joinFields.count (); ++idx )
97103 {
98104 // skip the join field to avoid double field names (fields often have the same name)
99- if ( joinFields[idx].name () != joinIt-> joinFieldName )
105+ if ( joinFields[idx].name () != joinFieldName )
100106 {
101107 QgsField f = joinFields[idx];
102108 f.setName ( joinLayer->name () + " _" + f.name () );
@@ -124,9 +130,18 @@ void QgsVectorLayerJoinBuffer::writeXml( QDomNode& layer_node, QDomDocument& doc
124130 for ( ; joinIt != mVectorJoins .constEnd (); ++joinIt )
125131 {
126132 QDomElement joinElem = document.createElement ( " join" );
127- joinElem.setAttribute ( " targetFieldName" , joinIt->targetFieldName );
133+
134+ if ( joinIt->targetFieldName .isEmpty () )
135+ joinElem.setAttribute ( " targetField" , joinIt->targetFieldIndex ); // for compatibility with 1.x
136+ else
137+ joinElem.setAttribute ( " targetFieldName" , joinIt->targetFieldName );
138+
128139 joinElem.setAttribute ( " joinLayerId" , joinIt->joinLayerId );
129- joinElem.setAttribute ( " joinFieldName" , joinIt->joinFieldName );
140+ if ( joinIt->joinFieldName .isEmpty () )
141+ joinElem.setAttribute ( " joinField" , joinIt->joinFieldIndex ); // for compatibility with 1.x
142+ else
143+ joinElem.setAttribute ( " joinFieldName" , joinIt->joinFieldName );
144+
130145 joinElem.setAttribute ( " memoryCache" , !joinIt->cachedAttributes .isEmpty () );
131146 vectorJoinsElem.appendChild ( joinElem );
132147 }
@@ -143,10 +158,14 @@ void QgsVectorLayerJoinBuffer::readXml( const QDomNode& layer_node )
143158 {
144159 QDomElement infoElem = joinList.at ( i ).toElement ();
145160 QgsVectorJoinInfo info;
146- info.joinFieldName = infoElem.attribute ( " joinFieldName" ); // TODO[MD]: compatibility with 1.x?
161+ info.joinFieldName = infoElem.attribute ( " joinFieldName" );
147162 info.joinLayerId = infoElem.attribute ( " joinLayerId" );
148- info.targetFieldName = infoElem.attribute ( " targetFieldName" ); // TODO[MD]: compatibility with 1.x?
163+ info.targetFieldName = infoElem.attribute ( " targetFieldName" );
149164 info.memoryCache = infoElem.attribute ( " memoryCache" ).toInt ();
165+
166+ info.joinFieldIndex = infoElem.attribute ( " joinField" ).toInt (); // for compatibility with 1.x
167+ info.targetFieldIndex = infoElem.attribute ( " targetField" ).toInt (); // for compatibility with 1.x
168+
150169 addJoin ( info );
151170 }
152171 }
0 commit comments