X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=lib%2Fpq%2FGateway%2FTable%2FRelations.php;h=b96ae661a8b12bd1a256d4775535163c0a25dfed;hb=HEAD;hp=7ad5326a84931c84f7fe1c8e40bcc747ad48c913;hpb=3c8b32baaac62855e2c9f5bfdb5ede9685ce2b76;p=m6w6%2Fpq-gateway diff --git a/lib/pq/Gateway/Table/Relations.php b/lib/pq/Gateway/Table/Relations.php index 7ad5326..b96ae66 100644 --- a/lib/pq/Gateway/Table/Relations.php +++ b/lib/pq/Gateway/Table/Relations.php @@ -6,66 +6,119 @@ use \pq\Gateway\Table; const RELATION_SQL = <<getMetadataCache(); - if (!($this->references = $cache->get("$table#relations"))) { + if (!($this->references = $cache->get("$table:relations"))) { $table->getQueryExecutor()->execute( new \pq\Query\Writer(RELATION_SQL, array($table->getName())), function($result) use($table, $cache) { - $this->references = $result->map(array(0,1), array(1,2,3,4), \pq\Result::FETCH_OBJECT); - $cache->set("$table#relations", $this->references); + $rel = $result->map([1,2], null, \pq\Result::FETCH_ASSOC); + foreach ($rel as $ref) { + foreach ($ref as $table => $key) { + $reference = new Reference($key); + $this->references[$table][$reference->name] = $reference; + } + } + $cache->set("$table:relations", $this->references); } ); } } function __isset($r) { - return isset($this->references->$r); + return isset($this->references[$r]); } function __get($r) { - return $this->references->$r; + return $this->references[$r]; } function __set($r, $v) { - $this->references->$r = $v; + $this->references[$r] = $v; } function __unset($r){ - unset($this->references->$r); + unset($this->references[$r]); + } + + /** + * Get a reference to a table + * @param string $table + * @param string $ref + * @return \pq\Gateway\Table\Reference + */ + function getReference($table, $ref = null) { + if (isset($this->references[$table])) { + if (!strlen($ref)) { + return current($this->references[$table]); + } + if (isset($this->references[$table][$ref])) { + return $this->references[$table][$ref]; + } + } + } + + /** + * Implements \Countable + * @return int + */ + function count() { + return array_sum(array_map("count", $this->references)); + } + + /** + * Implements \IteratorAggregate + * @return \ArrayIterator + */ + function getIterator() { + return new \ArrayIterator($this->references); } }