@@ -87,6 +87,29 @@ public function __construct(int $id, int $slotid, DateTimeImmutable $date, int $
87
87
$ this ->slot = null ;
88
88
}
89
89
90
+ /**
91
+ * Mark the object as freshly created and sets the new ID
92
+ * @param int $id the new ID after insertint into the DB
93
+ * @param slot $slot the cached slot object
94
+ */
95
+ public function set_fresh (int $ id , ?slot $ slot ) {
96
+ assert ($ this ->id === 0 );
97
+ assert ($ id !== 0 );
98
+ $ this ->id = $ id ;
99
+ if (!is_null ($ slot )) {
100
+ $ this ->set_slot ($ slot );
101
+ }
102
+ }
103
+
104
+ /**
105
+ * sets the cached slot object (mainly for deduplicating DB requests)
106
+ * @param slot $slot the cached slot object
107
+ */
108
+ public function set_slot (slot $ slot ) {
109
+ assert ($ this ->slotid === $ slot ->id );
110
+ $ this ->slot = $ slot ;
111
+ }
112
+
90
113
/**
91
114
* Returns the associated slot.
92
115
*
@@ -100,22 +123,6 @@ public function get_slot(): slot {
100
123
return $ this ->slot ;
101
124
}
102
125
103
- /**
104
- * Prepares data for the DB endpoint.
105
- *
106
- * @return object a representation of this reservation and its data
107
- */
108
- public function prepare_for_db (): object {
109
- $ obj = new \stdClass ();
110
-
111
- $ obj ->slotid = $ this ->slotid ;
112
- $ obj ->date = $ this ->date ;
113
- $ obj ->userid = $ this ->userid ;
114
- $ obj ->reserverid = $ this ->reserverid ;
115
-
116
- return $ obj ;
117
- }
118
-
119
126
/**
120
127
* Calculates the exact time and date this reservation is supposed to start
121
128
*
@@ -144,6 +151,27 @@ public function get_datetime_end(): DateTimeImmutable {
144
151
return $ this ->datetime_end ;
145
152
}
146
153
154
+ /**
155
+ * Prepares data for the DB endpoint.
156
+ * doesn't set ID if it's 0
157
+ *
158
+ * @return object a representation of this reservation and its data
159
+ */
160
+ public function prepare_for_db (): object {
161
+ $ obj = new \stdClass ();
162
+
163
+ $ obj ->slotid = $ this ->slotid ;
164
+ $ obj ->date = $ this ->date ;
165
+ $ obj ->userid = $ this ->userid ;
166
+ $ obj ->reserverid = $ this ->reserverid ;
167
+
168
+ if ($ this ->id !== 0 ) {
169
+ $ obj ->id = $ this ->id ;
170
+ }
171
+
172
+ return $ obj ;
173
+ }
174
+
147
175
/**
148
176
* Prepares data for the API endpoint.
149
177
*
0 commit comments