987c9ae2781d1995048ab65037a188e94c08e2e5
[mdref/mdref-pq-gateway] / hasRelation.md
1 # bool pq\Gateway\Table::hasRelation(string $name[, string $table = NULL])
2
3 Check whether a relation to another table exists with the specified name.
4 See pq\Gateway\Table\Relations
5
6 > ***NOTE:***
7 The relation name is the column name of the foreign key with the column name of the referenced column cut off the end.
8
9 ## Params:
10
11 * string $name
12 The name of the relation.
13 * Optional string $table = NULL
14 The bare table name, if the relation has another name (e.g. because of multiple foreign keys to the same table).
15
16 ## Returns:
17
18 * bool, whether the specified relation exists.
19
20 ## Example:
21
22 <?php
23
24 use pq\Gateway\Table;
25
26 $conn = new pq\Connection;
27 $conn->exec("
28 drop table if exists account cascade;
29 drop table if exists email cascade;
30 drop table if exists reftable cascade;
31
32 create table account (
33 id serial primary key
34 );
35
36 create table email (
37 id serial primary key,
38 account_id integer references account(id),
39 email text
40 );
41
42 create table reftable (
43 id serial primary key,
44 my_account integer references account(id),
45 account_id integer references account(id),
46 second_account_id integer references account(id),
47 email integer references email(id)
48 );
49 ");
50
51 $fgn_table = new Table("reftable");
52 var_dump($fgn_table->hasRelation("my_account"));
53
54 ?>
55
56 Yields:
57
58 TRUE