Skip to content

Commit eca4ddd

Browse files
committed
优化db外键关联
1 parent 84e103a commit eca4ddd

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

library/src/com/lidroid/xutils/DbUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ public static class DaoConfig {
580580
private String dbDir;
581581

582582
public DaoConfig(Context context) {
583-
this.context = context;
583+
this.context = context.getApplicationContext();
584584
}
585585

586586
public Context getContext() {

library/src/com/lidroid/xutils/db/table/Foreign.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,15 @@ public Object getColumnValue(Object entity) {
107107
Column column = TableUtils.getColumnOrId(foreignEntityType, foreignColumnName);
108108
columnValue = column.getColumnValue(foreignEntities.get(0));
109109

110+
// 仅自动关联外键
110111
Table table = this.getTable();
111-
if (table != null && columnValue == null && column instanceof Id) {
112-
table.db.saveOrUpdateAll(foreignEntities);
112+
if (table != null && column instanceof Id) {
113+
for (Object foreignObj : foreignEntities) {
114+
Object idValue = column.getColumnValue(foreignObj);
115+
if (idValue == null) {
116+
table.db.saveOrUpdate(foreignObj);
117+
}
118+
}
113119
}
114120

115121
columnValue = column.getColumnValue(foreignEntities.get(0));

0 commit comments

Comments
 (0)