Child pages
  • Create "kDBItem::shouldLoad" method [5.3.0-B1]
Skip to end of metadata
Go to start of metadata

The kDBItem class is used to operate with 1 record in the database table. Common approach for changing database data is this:

/** @var kDBItem $item */
$item = $this->Application->recallObject('sample-prefix', null, array('skip_autoload' => true));
$item->Load($item_id);
...
$item->Update();

Above code however has a side effect of always loading an object from database even in cases, when data in the database haven't changed since last call.

In the rRPI project this was solved by transforming above code into:

/** @var kDBItem $item */
$item = $this->Application->recallObject('sample-prefix', null, array('skip_autoload' => true));


if ( !$item->isLoaded() || $item->GetID() != $item_id ) {
	$item->Load($item_id);
}
 
...
$item-Update();

Main trick is in that IF statement condition that ensures that if item is the same, then don't attempt to reload it. 

Solution

I'm proposing to extract that IF condition into kDBItem::shouldLoad($id) method to ease the process of creating such constructs in code.

Related Tasks