mdref.json config
[mdref/mdref-pq-gateway] / pq-gateway / pq / Gateway / Table / getRelations.md
1 # pq\Gateway\Table\Relations pq\Gateway\Table::getRelations()
2
3 Get the relations (by foreign key) of this table.
4
5 ## Params:
6
7 None.
8
9 ## Returns:
10
11 * pq\Gateway\Table\Relations, the table's foreign keys.
12
13 ## Example:
14
15 <?php
16
17 use pq\Gateway\Table;
18
19 $conn = new pq\Connection;
20 $conn->exec("
21 -- drop table if exists account cascade;
22 -- create table account (
23 -- id uuid default uuid_generate_v4() primary key,
24 -- password char(60),
25 -- name varchar(68)
26 -- );
27
28 -- drop table if exists account_email cascade;
29 -- create table account_email (
30 -- account_id uuid not null references account(id) on delete cascade,
31 -- email varchar(255) not null unique,
32 -- primary key (account_id, email)
33 -- );
34
35 drop table if exists reftable cascade;
36 create table reftable (
37 id serial primary key,
38 my_account uuid references account(id),
39 account_id uuid references account(id),
40 second_account_id uuid references account(id),
41 email varchar(255) not null references account_email(email)
42 );
43 ");
44
45 $fgn_table = new Table("reftable");
46 var_dump($fgn_table->getRelations());
47
48 ?>
49
50 Yields:
51
52 object(pq\Gateway\Table\Relations)#7 (1) {
53 ["references":protected]=>
54 array(2) {
55 ["account"]=>
56 array(3) {
57 ["my_account"]=>
58 object(pq\Gateway\Table\Reference)#14 (5) {
59 ["name"]=>
60 string(10) "my_account"
61 ["foreignTable"]=>
62 string(8) "reftable"
63 ["foreignColumns"]=>
64 array(1) {
65 [0]=>
66 string(10) "my_account"
67 }
68 ["referencedTable"]=>
69 string(7) "account"
70 ["referencedColumns"]=>
71 array(1) {
72 [0]=>
73 string(2) "id"
74 }
75 }
76 ["account"]=>
77 object(pq\Gateway\Table\Reference)#15 (5) {
78 ["name"]=>
79 string(7) "account"
80 ["foreignTable"]=>
81 string(8) "reftable"
82 ["foreignColumns"]=>
83 array(1) {
84 [0]=>
85 string(10) "account_id"
86 }
87 ["referencedTable"]=>
88 string(7) "account"
89 ["referencedColumns"]=>
90 array(1) {
91 [0]=>
92 string(2) "id"
93 }
94 }
95 ["second_account"]=>
96 object(pq\Gateway\Table\Reference)#16 (5) {
97 ["name"]=>
98 string(14) "second_account"
99 ["foreignTable"]=>
100 string(8) "reftable"
101 ["foreignColumns"]=>
102 array(1) {
103 [0]=>
104 string(17) "second_account_id"
105 }
106 ["referencedTable"]=>
107 string(7) "account"
108 ["referencedColumns"]=>
109 array(1) {
110 [0]=>
111 string(2) "id"
112 }
113 }
114 }
115 ["account_email"]=>
116 array(1) {
117 ["email"]=>
118 object(pq\Gateway\Table\Reference)#17 (5) {
119 ["name"]=>
120 string(5) "email"
121 ["foreignTable"]=>
122 string(8) "reftable"
123 ["foreignColumns"]=>
124 array(1) {
125 [0]=>
126 string(5) "email"
127 }
128 ["referencedTable"]=>
129 string(13) "account_email"
130 ["referencedColumns"]=>
131 array(1) {
132 [0]=>
133 string(5) "email"
134 }
135 }
136 }
137 }
138 }
139