@@ -8,48 +8,43 @@ import '../store.dart';
8
8
import '../transaction.dart' ;
9
9
import 'info.dart' ;
10
10
11
- /// A lazily loaded `List` of target objects representing a to-many relation,
12
- /// a unidirectional link from a "source" entity to multiple objects of a
13
- /// "target" entity.
14
- ///
15
- /// It tracks changes (adds and removes) that can be later applied (persisted)
16
- /// to the database. This happens either when the source entity of this relation
17
- /// is put or using [applyToDb] . For some important details about applying
18
- /// changes, see the notes about relations of [Box.put] .
19
- ///
20
- /// The objects are loaded lazily on first access of this list, and then cached.
21
- /// Subsequent calls to any method, like [length] , do not query the database,
22
- /// even if the relation was changed elsewhere. To get the latest data [Box.get]
23
- /// the source object again.
24
- ///
25
- /// You can:
26
- /// - [add] new objects to the relation.
27
- /// - [removeAt] target objects from the relation at a specific list index.
28
- /// - [remove] target objects from the relation by an ID.
11
+ /// A to-many relation of an entity that references multiple objects of a "target" entity [EntityT] .
29
12
///
13
+ /// Example:
30
14
/// ```
15
+ /// @Entity()
31
16
/// class Student {
32
17
/// final teachers = ToMany<Teacher>();
33
18
/// }
19
+ /// ```
34
20
///
35
- /// // Example 1: create a relation
36
- /// final teacher1 = Teacher();
37
- /// final teacher2 = Teacher();
21
+ /// Implements the `List` interface and uses lazy initialization.
22
+ /// The target objects are only read from the database when the list is first accessed.
38
23
///
39
- /// final student1 = Student();
40
- /// student1.teachers.add(teacher1);
41
- /// student1.teachers.add(teacher2);
24
+ /// Tracks when target objects are added and removed. Common usage:
25
+ /// - [add] target objects to the relation.
26
+ /// - [remove] target objects from the relation.
27
+ /// - [removeAt] target objects at a specific index.
42
28
///
43
- /// final student2 = Student();
44
- /// student2.teachers.add(teacher2);
29
+ /// To apply (persist) the changes to the database, call [applyToDb] or put the object with the ToMany.
30
+ /// For important details, see the notes about relations of [Box.put] .
45
31
///
46
- /// // saves students as well as teachers in the database
47
- /// store.box<Student>().putMany([student1, student2]);
32
+ /// ```
33
+ /// // Example 1: add target objects to a relation
34
+ /// student.teachers.add(teacher1);
35
+ /// student.teachers.add(teacher2);
36
+ /// store.box<Student>().put(student);
48
37
///
49
- /// // Example 2: remove a relation
50
- /// student.teachers.removeAt(index)
51
- /// student.teachers.applyToDb(); // or store.box<Student>().put(student);
38
+ /// // Example 2: remove a target object from the relation
39
+ /// student.teachers.removeAt(index);
40
+ /// student.teachers.applyToDb();
41
+ /// // or store.box<Student>().put(student);
52
42
/// ```
43
+ ///
44
+ /// In the database, the target objects are referenced by their IDs, which are
45
+ /// persisted as part of the relation of the object with the ToMany.
46
+ ///
47
+ /// To get all objects with a ToMany that reference a target object, see [Backlink] .
53
48
class ToMany <EntityT > extends Object with ListMixin <EntityT > {
54
49
/// Store-related configuration attached to this.
55
50
///
@@ -92,6 +87,9 @@ class ToMany<EntityT> extends Object with ListMixin<EntityT> {
92
87
_items.length = newLength;
93
88
}
94
89
90
+ /// Gets the target object at the given index.
91
+ ///
92
+ /// [ToMany] uses lazy initialization, so on first access this will read the target objects from the database.
95
93
@override
96
94
EntityT operator [](int index) => _items[index];
97
95
@@ -109,6 +107,10 @@ class ToMany<EntityT> extends Object with ListMixin<EntityT> {
109
107
_track (element, 1 );
110
108
}
111
109
110
+ /// Prepares to add the given target object to this relation.
111
+ ///
112
+ /// To apply changes, call [applyToDb] or put the object with the ToMany.
113
+ /// For important details, see the notes about relations of [Box.put] .
112
114
@override
113
115
void add (EntityT element) {
114
116
ArgumentError .checkNotNull (element, 'element' );
@@ -121,6 +123,7 @@ class ToMany<EntityT> extends Object with ListMixin<EntityT> {
121
123
}
122
124
}
123
125
126
+ /// Like [add] , but for multiple target objects.
124
127
@override
125
128
void addAll (Iterable <EntityT > iterable) {
126
129
iterable.forEach (_track);
@@ -132,8 +135,11 @@ class ToMany<EntityT> extends Object with ListMixin<EntityT> {
132
135
}
133
136
}
134
137
135
- // note: to override, arg must be "Object", same as in the base class.
136
- @override
138
+ /// Prepares to remove the target object from this relation.
139
+ ///
140
+ /// To apply changes, call [applyToDb] or put the object with the ToMany.
141
+ /// For important details, see the notes about relations of [Box.put] .
142
+ @override // note: to override, arg must be "Object", same as in the base class.
137
143
bool remove (Object ? element) {
138
144
if (! _items.remove (element)) return false ;
139
145
if (element != null ) _track (element as EntityT , - 1 );
0 commit comments