table = WBClass::create('WBDatasource_Table');
$this->config = WBClass::create('WBConfig');
$this->config->load('table');
}
public function ls($table)
{
echo "list: $table
";
$t = $this->config->get($table);
$foreign = array();
if(isset($t['foreign']) && !empty($t['foreign']) && isset($t['primary'])) {
// accept strings as well as arrays
if (!is_array($t['foreign'])) {
$t['foreign'] = array($t['foreign']);
}
// select column of foreign key relation
foreach ($t['foreign'] as $f) {
$c = $this->config->get($f . '/primary');
if (empty($c)) {
continue;
}
$foreign[$f] = $c;
// init container for preload ids
if (!is_array(self::$preload[$f])) {
self::$preload[$f] = array();
}
}
}
echo 'Foreign
' . var_export($foreign, true) . "\n"; $list = $this->table->get($table); $p = $this->table->getIdentifier($table); foreach ($list as $l) { if ($p) { self::$cache[$table][$p] = $l; } if (empty($foreign)) { continue; } foreach ($foreign as $t => $k) { if (!isset($l[$k])) { continue; } if (isset(self::$cache[$t][$l[$k]])) { continue; } self::$preload[$t][$l[$k]] = true; } } echo '
' . var_export(self::$preload, true) . "\n"; return $list; } public function get($table, $id) { if (isset(self::$cache[$table][$id])) { return self::$cache[$table][$id]; } if (isset(self::$preload[$table][$id])) { $this->preload($table); return self::$cache[$table][$id]; } $d = $this->table->get($table, $id); self::$cache[$table][$id] = $data; return $d; } protected function preload($table) { if (!isset(self::$preload[$table])) { return; } $ids = self::$preload[$table]; unset(self::$preload[$table]); if (!is_array($ids)) { // this should never happen return; } $ids = array_keys($ids); $primary = $this->table->getIdentifier($table); $clause = array(); $clause[] = array( 'field' => $primary, 'value' => $ids, 'relation' => 'in' ); $list = $this->table->get($table, null, null, $clause); foreach ($list as $l) { self::$cache[$table][$l[$primary]] = $l; } } } /* $test = new tableForeignPreload(); $gerd = $test->get('user', 1); $files = $test->ls('vfsfile'); */ $files = $table->get('vfsfile'); echo "count files " . count($files) . "
' . var_export($gerd, true) . "\n"; ?>