@@ -279,10 +279,10 @@ public function __get($var)
279
279
if (property_exists ($ this , $ var )) return $ this ->{$ var };
280
280
281
281
// If a special property method exists, then call it (again, instead of looking at the Model_Data object).
282
- if (method_exists ($ this , '_property_ ' .$ var )) return call_user_func (array ($ this , '_property_ ' .$ var ));
282
+ if (method_exists ($ this , '_property_ ' .$ var )) return $ this ->{ $ var } = call_user_func (array ($ this , '_property_ ' .$ var ));
283
283
284
284
// Nothing special set up, default to looking at the Model_Data object.
285
- return $ this ->_data ->{$ var };
285
+ return $ this ->{ $ var } = $ this -> _data ->{$ var };
286
286
}
287
287
288
288
public function __call ($ var , $ args )
@@ -325,9 +325,8 @@ public function jsonSerialize()
325
325
public function __sleep ()
326
326
{
327
327
$ properties = array ('id ' , 'table ' , 'database ' );
328
- if ($ this ->cache ) {
329
- $ properties [] = '_data ' ;
330
- }
328
+ if ($ this ->cache ) $ properties [] = '_data ' ;
329
+
331
330
return $ properties ;
332
331
}
333
332
@@ -359,11 +358,25 @@ final public function data($return_original = false)
359
358
}
360
359
}
361
360
361
+ // Swap out the Data object in this Model for an updated one (i.e. after doing an update)
362
362
final public function dataUpdate (Data $ db )
363
363
{
364
364
$ this ->_data = Data::updateCache ($ db );
365
+ $ this ->dataClearCache ();
366
+ }
367
+
368
+ // When updating data object, clear "cached" versions of column data saved in __get()
369
+ final public function dataClearCache ()
370
+ {
371
+ $ modelschema = $ this ->_data ->getModel ();
372
+ foreach ($ modelschema ['columns ' ] as $ column => $ type )
373
+ {
374
+ if ($ column != 'id ' ) unset($ this ->{$ column });
375
+ }
365
376
}
366
377
378
+ // Grab a clean version of the Data object based on the current state in the database.
379
+ // Mostly used for updating foreign key results after updates
367
380
final public function dataRefresh ()
368
381
{
369
382
list ($ data ) = Model::factoryData (array ('id ' => $ this ->id ), $ this ->table , $ this ->database );
@@ -373,11 +386,13 @@ final public function dataRefresh()
373
386
Data::updateCache ($ this ->_data );
374
387
375
388
// Call replacement constructor after storing in the cache list (to prevent recursion)
389
+ $ this ->dataClearCache ();
376
390
$ this ->_init ();
377
391
378
392
return $ this ;
379
393
}
380
394
395
+ // If true, Data object is preserved when serializing this object
381
396
public function cachable ($ bool = true )
382
397
{
383
398
$ this ->cache = $ bool ;
0 commit comments