Browse Source

jfinal 3.6

James 7 years ago
parent
commit
cd5c02277e

+ 13 - 2
src/main/java/com/jfinal/plugin/activerecord/CPI.java

@@ -25,6 +25,7 @@ import java.util.Set;
 /**
  * Cross Package Invoking pattern for package activerecord.
  */
+@SuppressWarnings({"unchecked", "rawtypes"})
 public abstract class CPI {
 	
 	/**
@@ -32,16 +33,26 @@ public abstract class CPI {
 	 * @param model the model extends from class Model
 	 * @return the attributes map of the model
 	 */
-	@SuppressWarnings({"unchecked", "rawtypes"})
 	public static final Map<String, Object> getAttrs(Model model) {
 		return model._getAttrs();
 	}
 	
-	@SuppressWarnings({"unchecked", "rawtypes"})
 	public static final Set<String> getModifyFlag(Model model) {
 		return model._getModifyFlag();
 	}
 	
+	public static final Table getTable(Model model) {
+		return model._getTable();
+	}
+	
+	public static final Config getConfig(Model model) {
+		return model._getConfig();
+	}
+	
+	public static final Class<? extends Model> getUsefulClass(Model model) {
+		return model._getUsefulClass();
+	}
+	
 	public static <T> List<T> query(Connection conn, String sql, Object... paras) throws SQLException {
 		return Db.query(DbKit.config, conn, sql, paras);
 	}

+ 25 - 0
src/main/java/com/jfinal/plugin/activerecord/Model.java

@@ -243,6 +243,31 @@ public abstract class Model<M extends Model> implements Serializable {
 	}
 	
 	/**
+	 * 如果 attrOrNot 是表中的字段则调用 set(...) 放入数据
+	 * 否则调用 put(...) 放入数据
+	 */
+	public M setOrPut(String attrOrNot, Object value) {
+		Table table = _getTable();
+		if (table != null && table.hasColumnLabel(attrOrNot)) {	
+			_getModifyFlag().add(attrOrNot);	// Add modify flag, update() need this flag.
+		}
+		
+		attrs.put(attrOrNot, value);
+		return (M)this;
+	}
+	
+	public M _setOrPut(Map<String, Object> map) {
+		for (Entry<String, Object> e : map.entrySet()) {
+			setOrPut(e.getKey(), e.getValue());
+		}
+		return (M)this;
+	}
+	
+	public M _setOrPut(Model model) {
+		return _setOrPut(model._getAttrs());
+	}
+	
+	/**
 	 * Put map to the model without check attribute name.
 	 */
 	public M put(Map<String, Object> map) {