Skip to content
This repository was archived by the owner on May 12, 2022. It is now read-only.

Commit 8b10979

Browse files
author
Nik Barham
committed
Reduce function calls by saving data accessed from the Data object.
- Also, functionality to clear this "cached" data when the Data object is updated.
1 parent 0c00220 commit 8b10979

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

src/Automatorm/Orm/Model.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,10 @@ public function __get($var)
279279
if (property_exists($this, $var)) return $this->{$var};
280280

281281
// 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));
283283

284284
// Nothing special set up, default to looking at the Model_Data object.
285-
return $this->_data->{$var};
285+
return $this->{$var} = $this->_data->{$var};
286286
}
287287

288288
public function __call($var, $args)
@@ -325,9 +325,8 @@ public function jsonSerialize()
325325
public function __sleep()
326326
{
327327
$properties = array('id', 'table', 'database');
328-
if ($this->cache) {
329-
$properties[] = '_data';
330-
}
328+
if ($this->cache) $properties[] = '_data';
329+
331330
return $properties;
332331
}
333332

@@ -359,11 +358,25 @@ final public function data($return_original = false)
359358
}
360359
}
361360

361+
// Swap out the Data object in this Model for an updated one (i.e. after doing an update)
362362
final public function dataUpdate(Data $db)
363363
{
364364
$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+
}
365376
}
366377

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
367380
final public function dataRefresh()
368381
{
369382
list($data) = Model::factoryData(array('id' => $this->id), $this->table, $this->database);
@@ -373,11 +386,13 @@ final public function dataRefresh()
373386
Data::updateCache($this->_data);
374387

375388
// Call replacement constructor after storing in the cache list (to prevent recursion)
389+
$this->dataClearCache();
376390
$this->_init();
377391

378392
return $this;
379393
}
380394

395+
// If true, Data object is preserved when serializing this object
381396
public function cachable($bool = true)
382397
{
383398
$this->cache = $bool;

0 commit comments

Comments
 (0)