ab74d4060b23bdd49b7d795aa8b29101b2486cb7
3 namespace pq\Gateway\Table
;
7 const RELATION_SQL
= <<<SQL
9 substring(att1.attname from '^.*(?=_'||att2.attname||'$)') as "id"
10 ,cl1.relname as "foreignTable"
11 ,att1.attname as "foreignColumn"
12 ,cl2.relname as "referencedTable"
13 ,att2.attname as "referencedColumn"
24 and co.conrelid = cl1.oid
25 and co.conkey[1] = att1.attnum and cl1.oid = att1.attrelid
26 and co.confrelid = cl2.oid
27 and co.confkey[1] = att2.attnum and cl2.oid = att2.attrelid
34 * A foreighn key implementation
41 protected $references;
43 function __construct(Table
$table) {
44 $cache = $table->getMetadataCache();
45 if (!($this->references
= $cache->get("$table#relations"))) {
46 $table->getQueryExecutor()->execute(
47 new \pq\Query\
Writer(RELATION_SQL
, array($table->getName())),
48 function($result) use($table, $cache) {
49 $this->references
= $result->map(array(0,1), array(2,3,4), \pq\Result
::FETCH_OBJECT
);
50 $cache->set("$table#relations", $this->references
);
56 function __isset($r) {
57 return isset($this->references
->$r);
61 return $this->references
->$r;
64 function __set($r, $v) {
65 $this->references
->$r = $v;
69 unset($this->references
->$r);