@@ -232,21 +232,15 @@ Q_OUTOFLINE_TEMPLATE RowList<T> Query<T>::toList(int count)
232232
233233 levels.append (data);
234234 };
235+
236+ // Always add the reference to the query's base table as first item
237+ add_table (0 , d->database ->model ().tableByName (d->tableName ));
235238 for (int i = 0 ; i < d->relations .count (); ++i) {
236239 RelationModel *rel = d->relations [i];
237240 add_table (i, rel->masterTable );
238241 add_table (i, rel->slaveTable );
239242 }
240243
241- if (!importedTables.count ()) {
242- LevelData data;
243- data.table = d->database ->model ().tableByName (d->tableName );
244- data.keyFiledname = d->tableName + " ." + data.table ->primaryKey ();
245- data.lastKeyValue = QVariant ();
246-
247- levels.append (data);
248- }
249-
250244 QVector<bool > checked;
251245 checked.reserve (levels.count ());
252246 for (int i = 0 ; i < levels.count (); ++i)
@@ -290,6 +284,7 @@ Q_OUTOFLINE_TEMPLATE RowList<T> Query<T>::toList(int count)
290284 // create table row
291285 Row<Table> row;
292286 if (data.table ->className () == d->className ) {
287+ // create a row for the current table
293288 row = Nut::create<T>();
294289#ifdef NUT_SHARED_POINTER
295290 returnList.append (row.objectCast <T>());
@@ -299,6 +294,7 @@ Q_OUTOFLINE_TEMPLATE RowList<T> Query<T>::toList(int count)
299294 d->tableSet ->add (row);
300295
301296 } else {
297+ // create row for a related table
302298 Table *table;
303299 const QMetaObject *childMetaObject
304300 = QMetaType::metaObjectForType (data.table ->typeId ());
@@ -308,6 +304,20 @@ Q_OUTOFLINE_TEMPLATE RowList<T> Query<T>::toList(int count)
308304 qFatal (" Could not create instance of %s" ,
309305 qPrintable (data.table ->name ()));
310306 row = createFrom (table);
307+ if (levels[0 ].lastRow ) {
308+ // assign the current row (belongs to a joined table) to the query's base table
309+ foreach (RelationModel *rel, d->relations ) {
310+ if (rel->slaveTable ->className () == levels[0 ].table ->className ()
311+ && rel->masterTable ->className () == data.table ->className ()) {
312+ // relation found -> assign the row to the query's base table proper field
313+ QString propertyName = " _" + rel->localProperty ;
314+ levels[0 ].lastRow .data ()->setProperty (
315+ propertyName.toUtf8 ().constData (),
316+ QVariant::fromValue (row));
317+ break ;
318+ }
319+ }
320+ }
311321 }
312322
313323 QList<FieldModel*> childFields = data.table ->fields ();
0 commit comments