MCPcopy Create free account
hub / github.com/androrm/androrm / ForeignKeyField

Class ForeignKeyField

src/src/com/orm/androrm/field/ForeignKeyField.java:42–180  ·  view source on GitHub ↗

The foreign key field can be used to build a relation between two models. One model can create a link to another model by using a foreign key. The type parameter is used to determine the linked model type. @author Philipp Giese @param Type of the linked model.

Source from the content-addressed store, hash-verified

40 * @param <T> Type of the linked model.
41 */
42public class ForeignKeyField<T extends Model> extends DataField<T> implements Relation<T> {
43
44 private Class<T> mTarget;
45 private int mReference;
46 private boolean mOnDeleteCascade;
47
48 /**
49 * Initializes a new foreign key field. The target
50 * parameter is used to determine the class of the
51 * linked model in order to create the correct
52 * constraints.
53 *
54 * @param target Class of the linked model.
55 */
56 public ForeignKeyField(Class<T> target) {
57 mTarget = target;
58 mOnDeleteCascade = true;
59 }
60
61 /**
62 * By default foreign key fields have the option
63 * ON DELETE CASCADE set. This means if the model
64 * this field is pointing to will be deleted, also
65 * the referencing model will be deleted.
66 * <br /><br />
67 *
68 * By calling doNotCascade on the field you can
69 * prevent this behavior.
70 */
71 public void doNotCascade() {
72 mOnDeleteCascade = false;
73 }
74
75 /**
76 * As foreign keys may require database access in order to
77 * get their value, you have to use this function. The
78 * regular {@link DataField#get()} won't work at all times.
79 *
80 * @param context {@link Context} of the application.
81 *
82 * @return An instance of the references model or <code>null</code>
83 * if nothing could be found.
84 */
85 public T get(Context context) {
86 if(mValue == null) {
87 return Model.objects(context, mTarget).get(mReference);
88 }
89
90 return mValue;
91 }
92
93 /**
94 * Creates the foreign key constraint for the database.
95 *
96 * @param fieldName name of the field, that references the
97 * other table.
98 *
99 * @return The constraint.

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected